接下來咱們將介紹另外一種簡單適用的防止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進行解密
解密對咱們來講沒有多大的用途,只是瞭解一下。