android/iOS備案簽名

一. android簽名

1. 生成新的密鑰庫(kù)文件

  • -keystore ~/your-release-key.jks: ??????要指定密鑰庫(kù)your-release-key.jks的路徑和名稱。
  • -keyalg RSA:指定密鑰算法,這里使用 RSA。
  • -keysize 2048:指定密鑰的大小。
  • -validity 10000:指定密鑰的有效期,以天為單位(10000 天約為 27 年)。
  • -alias your-key-alias:為你的密鑰指定一個(gè)別名(需要記住,后面?zhèn)浒覆榭垂€和md5指紋需要用到)。
  • 命令:
    keytool -genkey -v -keystore ~/your-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias your-key-alias
    

2.配置gradle構(gòu)建文件

  • 打開(kāi)android/app/build.gradle,進(jìn)行如下配置:
  • your-key-alias替換你的密鑰別名
  • your-key-password替換密鑰的密碼
  • /path/to/your-release-key.jks替換為你的密鑰庫(kù)文件的實(shí)際路徑。
  • your-keystore-password替換為密鑰庫(kù)的密碼。
    android {
      ...
      signingConfigs {
          release {
              keyAlias 'your-key-alias'
              keyPassword 'your-key-password'
              storeFile file('/path/to/your-release-key.jks')
              storePassword 'your-keystore-password'
          }
      }
      buildTypes {
          release {
              signingConfig signingConfigs.release
              minifyEnabled false
              shrinkResources false
              // 注意:如果需要開(kāi)啟代碼混淆,請(qǐng)?jiān)O(shè)置 minifyEnabled 為 true,并配置 proguard-rules.pro
              // proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
          }
      }
    }
    

3.另一種方式:使用key.properties文件管理密鑰和別名

??????這個(gè)和上面是一回事,只不過(guò)多了個(gè)key.properties管理上面的參數(shù),不容易丟失忘記

  • 創(chuàng)建JKS文件,命名為my-release-key.jks,放到android/app/路徑下
  • 創(chuàng)建一個(gè)key.properties文件,放到路徑android/下,內(nèi)容大概如下:
    #你的keystore密碼
    storePassword=123456
    #你的key密碼
    keyPassword=123456
    #你的key別名
    keyAlias=keys
    #`JKS文件`相對(duì)于`build.gradle`的路徑,同層路徑,就填文件名`my-release-key.jks`
    storeFile=my-release-key.jks
    
  • android/app/build.gradle相關(guān)的內(nèi)容如下:
    plugins {
       id "com.android.application"
       id "kotlin-android"
       id "dev.flutter.flutter-gradle-plugin"
       id 'com.google.gms.google-services'
       id "com.google.firebase.crashlytics"
    }
    
     //加載key.properties文件
    def keystoreProperties = new Properties()
    def keystorePropertiesFile = rootProject.file('key.properties')
    if (keystorePropertiesFile.exists()) {
        keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
    }
    
    
    android {
    
        //...省略其他配置
        kotlinOptions {
        //...省略其他配置
        }
    
        defaultConfig {
        //...省略其他配置
        }
       signingConfigs {
            release {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
                storePassword keystoreProperties['storePassword']
                // if (project.hasProperty('keyProperties')) {
                //     def keyProperties = new Properties()
                //     file('key.properties').withInputStream { keyProperties.load(it) }
                //     storeFile = file(keyProperties['storeFile'])
                //     storePassword = keyProperties['storePassword']
                //     keyAlias = keyProperties['keyAlias']
                //     keyPassword = keyProperties['keyPassword']
                // }
            }
        }
        buildTypes {
            release {
                signingConfig signingConfigs.release
                minifyEnabled true // 如果需要代碼混淆,設(shè)置為 true
                shrinkResources true // 如果需要資源壓縮,設(shè)置為 true
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
     }
    
  • 如果是build.gradle.kts相關(guān)內(nèi)容如下:
    plugins {
       id("com.android.application")
       id("kotlin-android")
       id("dev.flutter.flutter-gradle-plugin")
    }
    
    import java.util.Properties
    val keystoreProperties = Properties()
    val keystorePropertiesFile = rootProject.file("key.properties")
    if (keystorePropertiesFile.exists()) {
       keystoreProperties.load(keystorePropertiesFile.inputStream())
    }
    
    android {
       compileOptions {
         //...省略其他配置
       }
    
       kotlinOptions {
         //...省略其他配置
       }
    
       defaultConfig {
          //...省略其他配置
       }
    
       signingConfigs {
           create("release") {
               keyAlias = keystoreProperties["keyAlias"] as String?
               keyPassword = keystoreProperties["keyPassword"] as String?
               storeFile = keystoreProperties["storeFile"]?.let { file(it as String) }
               storePassword = keystoreProperties["storePassword"] as String?
           }
       }
    
       buildTypes {
           getByName("release") {
               signingConfig = signingConfigs.getByName("release")
               isMinifyEnabled = true
               isShrinkResources = true
               proguardFiles(
                   getDefaultProguardFile("proguard-android-optimize.txt"),
                   "proguard-rules.pro"
               )
           }
       }
    }
    

4. 驗(yàn)證簽名

  • 輸出 Verified 說(shuō)明 APK 已正確簽名:
    apksigner verify build/app/outputs/flutter-apk/app-release.apk
    

5. 注意事項(xiàng)

  • 密鑰管理:保管你的密鑰庫(kù)文件和密碼。如果丟失密鑰,將無(wú)法更新應(yīng)用。
  • 安全性:不要將密鑰庫(kù)文件和密碼直接硬編碼到項(xiàng)目中??梢钥紤]使用環(huán)境變量或安全存儲(chǔ)工具。



二.android備案相關(guān)命令

  • 需求信息:

    • 包名bundle ID
    • 公鑰
    • 證書(shū)MD5指紋(32長(zhǎng)度的16進(jìn)制數(shù)字)
  • 查看公鑰:

    • -alias: 指定的別名,比如keys
    • your-release-key.jks: 文件your-release-key.jks的路徑,比如xx項(xiàng)目/android/app/my-release-key.jks
    cd xx項(xiàng)目/android/
    keytool -exportcert -alias "keys" -keystore "app/my-release-key.jks" | openssl x509 -inform DER -subject_hash
    
  • 查看密鑰庫(kù)的內(nèi)容,(有幾個(gè)證書(shū),可以在這里看)

    keytool -list -v -keystore "your-release-key.jks"
    

由于 MD5 已經(jīng)被認(rèn)為不安全,因此現(xiàn)代工具和方法可能默認(rèn)不顯示 MD5 指紋。如果確實(shí)需要查看 MD5 指紋,你可以通過(guò)導(dǎo)出證書(shū)并使用 openssl 手動(dòng)計(jì)算來(lái)實(shí)現(xiàn)。這種方法不僅適用于 .jks 文件,還適用于其他證書(shū)管理系統(tǒng)

  • 使用openssl提取MD5指紋:
    • 導(dǎo)出.cer證書(shū):
      keytool -exportcert -alias "your-key-alias" -keystore "your-release-key.jks" -file exported_cert.cer
      
    • 或者導(dǎo)出.crt證書(shū):
      keytool -exportcert -alias "your-key-alias" -keystore "your-release-key.jks" -file exported_cert.crt
      
    • 或者導(dǎo)出.pem格式(openssl工具可能無(wú)法識(shí)別上面的證書(shū)格式):
      -rfc:使用 PEM 格式導(dǎo)出證書(shū)(這是文本格式,可以直接查看和使用)。
      keytool -exportcert -alias "your-key-alias" -keystore "your-release-key.jks"  -rfc -file exported_cert.pem
      
    • 讀取證書(shū),并計(jì)算MD5指紋:
      openssl x509 -in exported_cert.crt -noout -fingerprint -md5
      

3.iOS備案相關(guān)

  • 需求信息:
    • 包名bundle ID
    • 公鑰
    • sha1指紋(32長(zhǎng)度的16進(jìn)制數(shù)字)
  • 在關(guān)鍵文件.cer,這上面都有。
  • 問(wèn)題:
    • 本來(lái)老的macos系統(tǒng)很好找上面的資料。新系統(tǒng)把鑰匙串訪問(wèn)給隱藏了。
      雖然finder->程序->實(shí)用工具->鑰匙串訪問(wèn),這條路徑可以看到,但是我的不知道為什么,始終打不開(kāi)!
    • 偏偏又沒(méi)有登陸開(kāi)發(fā)者的密碼,下載不了.cer文件
  • 解決辦法:
    • Xcode->Settings->Account->找到打包需要備案的app的開(kāi)發(fā)者賬號(hào)->Manage Certificates->找到發(fā)布證書(shū)iOS Distribution Certificates->右鍵導(dǎo)出Export Certificate
    • 導(dǎo)出時(shí):選擇保存路徑->隨便輸入密碼123456->驗(yàn)證密碼:123456->保存文件的名字:1111->得到路徑下的文件:1111.p12
    • 1111.p12轉(zhuǎn)成.pem文件:
      cd 1111.p12的保存路徑
      
      openssl pkcs12 -in 1111.p12 -out cert.pem -nodes
      
      • 輸入之前的密碼123456回車
      • 報(bào)錯(cuò):
        tucici@tucicideMacBook-Pro Desktop % openssl pkcs12 -in Untitled.p12 -  out cert.pem -nodes -legacy
        Enter Import Password:
        tucici@tucicideMacBook-Pro Desktop % openssl pkcs12 -in Untitled.p12 - out cert.pem -nodes    
        Enter Import Password:
        Error outputting keys and certificates
        C09DBA49F87F0000:error:0308010C:digital envelope   routines:inner_evp_generic_fetch:unsupported:crypto/evp/evp_fetch.c:355:Glo bal default library context, Algorithm (RC2-40-CBC : 0), Properties ()
        
      • 原因:這個(gè)錯(cuò)誤表明 OpenSSL 3.0+ 版本默認(rèn)禁用了 RC2-40-CBC 這種較弱的加密算法
      • 更正命令:
        openssl pkcs12 -in 1111.p12 -out cert.pem -nodes -legacy
        
    • 檢查cert.pem是否轉(zhuǎn)換正確:
      cat cert.pem
      
    • cert.pem轉(zhuǎn)換成.cer文件:
      openssl x509 -in cert.pem -out cert.cer -outform der
      
    • 同路徑下,就可以看到cert.cer文件了。
  • 查看公共密鑰:
    openssl x509 -inform der -in cert.cer -pubkey -noout
    
  • 查看SHA1 指紋
    openssl x509 -inform der -in cert.cer -fingerprint -sha1 -noout
    
  • 查看MD5指紋:
    openssl x509 -inform der -in cert.cer -fingerprint -md5 -noout
    

相關(guān)命令

  • 1111.p12->cert.pem:
    openssl pkcs12 -in 1111.p12 -out cert.pem -nodes -legacy
    
  • cert.pem->cert.cer:
    openssl x509 -in cert.pem -out cert.cer -outform der
    
  • cert.pem->cert.der:
    openssl x509 -in cert.pem -outform der -out cert.der
    
  • 查看公共密鑰:
    openssl x509 -inform der -in cert.cer -pubkey -noout
    
  • 查看SHA1 指紋
    openssl x509 -inform der -in cert.cer -fingerprint -sha1 -noout
    
  • 查看MD5指紋:
    openssl x509 -inform der -in cert.cer -fingerprint -md5 -noout
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容