Android簽名相關知識整理

技術小黑屋發表於2016-03-06

不止一次有用到Android簽名相關的知識,每次都幾乎從零開始在Google上搜尋找,不想在繼續這樣了,找了個時間好好整理了一下自己用到的一些碎片知識,於是乎放到這裡,一是備忘,二是幫助別人。

從APK檔案中獲取簽名資訊

使用方法

keytool -list -printcert -jarfile your_apk_file

輸出資訊

  • 簽名Owner,Issuer等資訊
  • 簽名的fingerprints,如md5及sha1等值
  • 簽名有效期等資訊

示例效果

16:29 $ keytool -list -printcert -jarfile akoi_1.2.apk
Signer #1:

Signature:

Owner: CN=Andrew Wallace, OU=droidyue.com, O=droidyue.com, L=Beijing, ST=Beijing, C=86
Issuer: CN=Andrew Wallace, OU=droidyue.com, O=droidyue.com, L=Beijing, ST=Beijing, C=86
Serial number: 11a8a4a3
Valid from: Tue Feb 10 18:07:43 CST 2015 until: Sun Jun 13 18:07:43 CST 3013
Certificate fingerprints:
   MD5:  46:C5:BE:EF:B5:C9:00:E1:FA:42:50:50:57:54:CA:15
   SHA1: C1:14:5D:0A:C2:BF:F6:06:43:20:AE:2C:07:12:97:58:C2:1B:39:D1
   SHA256: 0E:88:7D:C2:4C:D6:84:A7:58:D4:24:1E:9D:38:F9:05:98:1E:B2:A2:D7:CB:0F:81:74:60:5B:38:89:FF:21:1C
   Signature algorithm name: SHA256withRSA
   Version: 3

從簽名檔案中獲取簽名資訊

使用方法

keytool -list -v -keystore your_kestore_file

注意,上述命令執行後,會提示輸入密碼,其實輸入錯誤也沒有關係,不影響結果。

輸出資訊

  • 簽名Owner,Issuer等資訊
  • 簽名的fingerprints,如md5及sha1等值
  • 簽名有效期等資訊

示例效果

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

Alias name: droidyue.com
Creation date: Feb 10, 2015
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
Owner: CN=Andrew Wallace, OU=droidyue.com, O=droidyue.com, L=Beijing, ST=Beijing, C=86
Issuer: CN=Andrew Wallace, OU=droidyue.com, O=droidyue.com, L=Beijing, ST=Beijing, C=86
Serial number: 11a8a4a3
Valid from: Tue Feb 10 18:07:43 CST 2015 until: Sun Jun 13 18:07:43 CST 3013
Certificate fingerprints:
   MD5:  46:C5:BE:EF:B5:C9:00:E1:FA:42:50:50:57:54:CA:15
   SHA1: C1:14:5D:0A:C2:BF:F6:06:43:20:AE:2C:07:12:97:58:C2:1B:39:D1
   SHA256: 0E:88:7D:C2:4C:D6:84:A7:58:D4:24:1E:9D:38:F9:05:98:1E:B2:A2:D7:CB:0F:81:74:60:5B:38:89:FF:21:1C
   Signature algorithm name: SHA256withRSA
   Version: 3

重新簽名APK

在沒有原始碼情況下,我們就能對apk進行更換籤名。

指令碼

使用方法

bash signapk.sh your_apk_file your_keystore_file keystore_pass keystore_alias

示例效果

16:57 $ bash signapk.sh weixin6313android740.apk ~/Documents/baidu_disk/百度雲同步盤/droidapp/mykiki 123456 droidyue.com
param1 weixin6313android740.apk
param2 /Users/androidyue/Documents/droidapp/mykiki
param3 123456
param4 droidyue.com
deleting: META-INF/MANIFEST.MF
deleting: META-INF/DROIDYUE.SF
deleting: META-INF/DROIDYUE.RSA
   adding: META-INF/MANIFEST.MF
   adding: META-INF/DROIDYUE.SF
   adding: META-INF/DROIDYUE.RSA
......
Verification succesful

生成的檔案會放在當前目錄,其檔名相對輸入檔案,增加了signed_字首,比如對weixin6313android740.apk進行上述操作得到的輸出檔案是signed_weixin6313android740.apk

Gradle build生成簽名APK

想要在執行gradle build時生成指定簽名的apk,需要在build.gradle中如下修改

android {

    signingConfigs {
        release {
            storeFile file("myrelease.keystore")
            storePassword "********"
            keyAlias "******"
            keyPassword "******"
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

相關文章