如何以编程方式而不是使用 Android Studio 创建 Android 签名证书(密钥库 JKS 文件)?
How to create Android Signing Certificate (keystore JKS file) programmatically instead of using Android Studio?
我想在不使用 Android Studio 的情况下以编程方式创建 jks 文件(密钥库)文件(Java/Kotlin JVM 上的一些脚本)。可能吗?
我有一些代码来创建这个文件,但是如何将所有需要的数据填充到那个键中?
fun createJksFile() {
val keyFilePassword = "test"
val keyAlias = "test"
val keyPassword = "test"
val keyValidity = "100"
val CertificateFirstandLastName = "Test"
val CertificateOrganizationalUnit = "Test"
val CertificateOrganization = "Test"
val CertificateCityorLocality = "US"
val CertificateStateorProvince = "US"
val CertificateCountryCode = "US"
val ks = KeyStore.getInstance(KeyStore.getDefaultType())
val password = keyFilePassword.toCharArray()
ks.load(null, password)
// TODO: how to set all needed data?
FileOutputStream(File(projectDir, "_test_key.jks")).use { fos ->
ks.store(fos, password)
}
}
我决定使用这个解决方案
val keystoreCommand = "keytool -genkey -noprompt \n" +
"-alias ${keyStore.keyAlias} \n" +
"-dname \"CN=${keyStore.certificateFirstandLastName}, OU=${keyStore.certificateOrganizationalUnit}, O=${keyStore.certificateOrganization}, L=${keyStore.certificateCityorLocality}, S=${keyStore.certificateStateorProvince}, C=${keyStore.certificateCountryCode}\" \n" +
"-keystore \"C:\Users\Desktop\keystore.jks\" \n" +
"-storepass ${keyStore.keyFilePassword} \n" +
"-keypass ${keyStore.keyPassword}"
Runtime.getRuntime().exec(keystoreCommand).apply {
waitFor()
}
data class KeyStore(
val keyFilePassword: String,
val keyAlias: String,
val keyPassword: String,
val keyValidity: String,
val certificateFirstandLastName: String,
val certificateOrganizationalUnit: String,
val certificateOrganization: String,
val certificateCityorLocality: String,
val certificateStateorProvince: String,
val certificateCountryCode: String
)
虽然我们可以不用 keytool 来完成,但是需要处理很多代码
我想在不使用 Android Studio 的情况下以编程方式创建 jks 文件(密钥库)文件(Java/Kotlin JVM 上的一些脚本)。可能吗?
我有一些代码来创建这个文件,但是如何将所有需要的数据填充到那个键中?
fun createJksFile() {
val keyFilePassword = "test"
val keyAlias = "test"
val keyPassword = "test"
val keyValidity = "100"
val CertificateFirstandLastName = "Test"
val CertificateOrganizationalUnit = "Test"
val CertificateOrganization = "Test"
val CertificateCityorLocality = "US"
val CertificateStateorProvince = "US"
val CertificateCountryCode = "US"
val ks = KeyStore.getInstance(KeyStore.getDefaultType())
val password = keyFilePassword.toCharArray()
ks.load(null, password)
// TODO: how to set all needed data?
FileOutputStream(File(projectDir, "_test_key.jks")).use { fos ->
ks.store(fos, password)
}
}
我决定使用这个解决方案
val keystoreCommand = "keytool -genkey -noprompt \n" +
"-alias ${keyStore.keyAlias} \n" +
"-dname \"CN=${keyStore.certificateFirstandLastName}, OU=${keyStore.certificateOrganizationalUnit}, O=${keyStore.certificateOrganization}, L=${keyStore.certificateCityorLocality}, S=${keyStore.certificateStateorProvince}, C=${keyStore.certificateCountryCode}\" \n" +
"-keystore \"C:\Users\Desktop\keystore.jks\" \n" +
"-storepass ${keyStore.keyFilePassword} \n" +
"-keypass ${keyStore.keyPassword}"
Runtime.getRuntime().exec(keystoreCommand).apply {
waitFor()
}
data class KeyStore(
val keyFilePassword: String,
val keyAlias: String,
val keyPassword: String,
val keyValidity: String,
val certificateFirstandLastName: String,
val certificateOrganizationalUnit: String,
val certificateOrganization: String,
val certificateCityorLocality: String,
val certificateStateorProvince: String,
val certificateCountryCode: String
)
虽然我们可以不用 keytool 来完成,但是需要处理很多代码