接下来咱们将介绍另外一种简单适用的防止apk反编译的技术-对apk进行伪加密。php
1、伪加密技术原理python
咱们知道android apk本质上是zip格式的压缩包,咱们将android应用程序的后缀.apk改成.zip就能够用解压软件轻松的将android应用程序解压缩。在平常生活或者工做中,咱们一般为了保护咱们本身的文件在进行压缩式都会进行加密处理。这样的方法对于android apk一样适用。原理很简单,在zip的文件格式中有一个位用来标示该zip压缩文件中的文件是否被加密,咱们只要找到该标志位将其置1就能够实现咱们的目的。而android的包安装服务(PackageManagerService)在进行apk安装时并不关心这个加密位(暂时咱们就这么叫它吧)能够进行正常的安装而且也不会影响apk的运行。android
2、zip文件格式算法
zip的文件格式一般有三个部分组成:压缩文件源数据、压缩目录源数据、目录结束标识。这三个部分中和咱们说的加密位有关的是压缩目录源数据部分,咱们接下来详细介绍这一部分。app
压缩目录源数据部分记录着全部的压缩目录源数据。其结构以下:ide
Central directory file header加密 |
|||
Offsetspa |
Bytes.net |
Description[18]orm |
译 |
0 |
4 |
核心目录文件header标识=(0x02014b50) |
|
4 |
2 |
Version made by |
压缩所用的pkware版本 |
6 |
2 |
Version needed to extract (minimum) |
解压所需pkware的最低版本 |
8 |
2 |
General purpose bit flag |
通用位标记 |
10 |
2 |
Compression method |
压缩方法 |
12 |
2 |
File last modification time |
文件最后修改时间 |
14 |
2 |
File last modification date |
文件最后修改日期 |
16 |
4 |
CRC-32 |
CRC-32算法 |
20 |
4 |
Compressed size |
压缩后大小 |
24 |
4 |
Uncompressed size |
未压缩的大小 |
28 |
2 |
File name length (n) |
文件名长度 |
30 |
2 |
Extra field length (m) |
扩展域长度 |
32 |
2 |
File comment length (k) |
文件注释长度 |
34 |
2 |
Disk number where file starts |
文件开始位置的磁盘编号 |
36 |
2 |
Internal file attributes |
内部文件属性 |
38 |
4 |
External file attributes |
外部文件属性 |
42 |
4 |
Relative offset of local file header. This is the number of bytes between the start of the first disk on which the file occurs, and the start of the local file header. This allows software reading the central directory to locate the position of the file inside the ZIP file. |
本地文件header的相对位移。 |
46 |
n |
File name |
目录文件名 |
46+n |
m |
Extra field |
扩展域 |
46+n+m |
k |
File comment |
文件注释内容 |
该结构中的General purpose bit flag部分的第0位若是置1,标识该压缩包被加密;置为0标识该压缩包没有被加密。
3、具体实施
咱们能够利用ZipCenOp.jar这个jar包对apk进行加密和解密操做(也有用python实现这个操做的这里咱们不作介绍)。
(1) 对apk进行加密
加密后,咱们用解压缩软件进行解密会看以下的提示信息:
用apktool进行反编译会提示以下的错误信息:
加密后apk是能够正常的安装和运行的。
(2) 对apk进行解密
解密对咱们来讲没有多大的用途,只是了解一下。