一、偽加密技術原理
我們知道android apk本質上是zip格式的壓縮包,我們將android應用程序的后綴.apk改為.zip就可以用解壓軟件輕松的將android應用程序解壓縮。在日常生活或者工作中,我們通常為了保護我們自己的文件在進行壓縮式都會進行加密處理。這樣的方法對于android apk同樣適用。原理很簡單,在zip的文件格式中有一個位用來標示該zip壓縮文件中的文件是否被加密,我們只要找到該標志位將其置1就可以實現我們的目的。而android的包安裝服務(PackageManagerService)在進行apk安裝時并不關心這個加密位(暫時我們就這么叫它吧)可以進行正常的安裝并且也不會影響apk的運行。
二、zip文件格式
zip的文件格式通常有三個部分組成:壓縮文件源數據、壓縮目錄源數據、目錄結束標識。這三個部分中和我們說的加密位有關的是壓縮目錄源數據部分,我們接下來詳細介紹這一部分。
壓縮目錄源數據部分記錄著所有的壓縮目錄源數據。其結構如下:
Central directory file essay-header | ? | ||
Offset | Bytes | Description[18] | 譯 |
0 | 4 | Central directory file essay-header signature =0x02014b50 | 核心目錄文件essay-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 essay-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 essay-header. This allows software reading the central directory to locate the position of the file inside the ZIP file. | 本地文件essay-header的相對位移。 |
46 | n | File name | 目錄文件名 |
46+n | m | Extra field | 擴展域 |
46+n+m | k | File comment | 文件注釋內容 ? |
該結構中的General purpose bit flag部分的第0位如果置1,標識該壓縮包被加密;置為0標識該壓縮包沒有被加密。
三舉例:
- ?先給一個APK文件加密。
- 用16進制編輯器打開它,搜索50 4B 01 02 注意大小端格式。
- 修改這個紅框之后偏移為5的字段01-->00。
- ?你會發現對應的AndroidManifest.xml就沒有加密啦。