Android签名公私密钥对转换为keystore

如果要在Eclipse中调试Android源码中非test key签名的程序(也就是使用platform, media or sharedkey签名的程序),需要把Android源码中的公私钥对(build/target/product/security)转换为Eclipse能够使用的keystore。

转换步骤如下:

0.把build/target/product/security下面的某对需要转换的key拷贝到一个你的工作目录

(下面以shared key为例:shared.pk8 & shared.x509.pem)

1. 把pkcs8格式的私钥转换为pkcs12格式:

$ openssl pkcs8 -in shared.pk8 -inform DER -outform PEM-out shared.priv.pem -nocrypt

2.生成pkcs12格式的密钥文件:

$ openssl pkcs12 -export -in shared.x509.pem -inkey shared.priv.pem -outshared.pk12 -name androiddebugkey

(注:此过程中需要输入密码:android)

3.生成keystore:

$ keytool -importkeystore -deststorepass android -destkeypass android-destkeystore debug.keystore -srckeystore shared.pk12 -srcstoretype PKCS12-srcstorepass android -alias androiddebugkey

至此,已经生成keystore:debug.keystore

在Eclipse的Windows/Preferences/Android/Build中设置“Custom debugkeystore“为刚才生成的keystore即可。

对于其它类型的key,步骤相同。

 

源文档 <http://blog.csdn.net/edisonlg/article/details/7857242

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当将密钥存储在Android Keystore中时,以下是一些示例代码: 1. 生成AES密钥: ```java KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); keyGenerator.init(new KeyGenParameterSpec.Builder(keyAlias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) .setBlockModes(KeyProperties.BLOCK_MODE_CBC) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7) .setUserAuthenticationRequired(true) // 需要用户验证 .build()); SecretKey secretKey = keyGenerator.generateKey(); ``` 2. 加载KeyStore: ```java KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore"); keyStore.load(null); ``` 3. 生成并存储密钥对: ```java KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore"); keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(keyAlias, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT) .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1) .setUserAuthenticationRequired(true) // 需要用户验证 .build()); KeyPair keyPair = keyPairGenerator.generateKeyPair(); ``` 4. 使用密钥: ```java PrivateKey privateKey = (PrivateKey) keyStore.getKey(keyAlias, null); PublicKey publicKey = keyStore.getCertificate(keyAlias).getPublicKey(); // 使用私钥进行加密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] encryptedData = cipher.doFinal(data); // 使用公钥进行解密 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] decryptedData = cipher.doFinal(encryptedData); ``` 请注意,以上代码仅为示例,您需要根据自己的需求进行修改和适配。确保密钥别名(keyAlias)是唯一且安全的,并在必要时处理异常。此外,代码中的用户验证(User Authentication)部分可以根据您的需求进行调整。 希望这些代码能够帮助您在Android应用程序中将密钥存储在Android Keystore中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值