把Android源码中的密码对转换为keystore的方法

来源:互联网 发布:eclipse管理数据库 编辑:程序博客网 时间:2024/05/14 14:57

如果要在Eclipse中调试Android源码中非test key签名的程序(也就是使用platform, media or shared key签名的程序),需要把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 -out shared.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 debug keystore“为刚才生成的keystore即可。

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

一、前言
goolge为我们提供了4个标准的key,以签名测试程序:
testkey -- a generic key for packages that do not otherwise specify a key.
platform -- a test key for packages that are part of the core platform.
shared -- a test key for things that are shared in the home/contacts process.
media -- a test key for packages that are part of the media/download system.
它们位于Android源码的以下目录
android\build\target\product\security
注意,这些key只是用于工程版的Android系统.在编译时android源码时,使用eng选项即表示编译生成工程版的Android系统,
而使用user选项时表示编译用户版(即正式版)的Android系统。
二、使用key生成keystore文件
使用google的标准key生成keystore文件。我们需要2个工具,openssl  (cryptography and SSL/TLS toolkit) and keytool
openssl是Linux上的一个工具,keytool则是JDK的一个工具,它位于JDK\bin目录。在此假设你已经把JDK\bin添加到了window的path环境变量中。因为一般用户使用的是window系统,
所以我这里把Cygwin安装在window上来模拟Linux环境,在Cygwin上使用opnssl.
关于Cygwi详细内容请参考cygwin安装详解》。
2.1、命令行方式
在这里我将以platform为例进行讲解。
通过“Cygwin Terminal”的进入Cygwin的命令行,然后切换到key所在的目录。即security目录.
输入以下命令:
openssl pkcs8 -inform DER -nocrypt -in platform.pk8 -out platform.pem
执行该命令,将在目录下生成platform.pem文件
接着输入以下命令:
openssl pkcs12 -export -in  platform.x509.pem -out platform.p12 -inkey  platform.pem -password pass:android -name androiddebugkey
执行该命令,将在目录下生成platform.p12文件,它本质上应该就是一个数字证书。
进入DOS命令行,切换到key所在的目录。即security
然后输入命令:
keytool -importkeystore -deststorepass android -destkeystore ./platform.jks -srckeystore ./platform.p12 -srcstoretype PKCS12 -srcstorepassandroid
执行该命令,将在目录下生成platform.jks文件,它就是我们需要的keystore文件。它的后缀本身是没有关系。如果你更喜欢其后缀名为keystore。
上面的命令改成这样就行了。
keytool -importkeystore -deststorepass android -destkeystore ./platform.keystore -srckeystore ./platform.p12 -srcstoretype PKCS12 -srcstorepass android
另外,我方便我们可以使用批处理来进行处理。
通过“Cygwin Terminal”的进入Cygwin的命令行,然后切换到key所在的目录。即security目录.
使用sh ./export.sh命令运行export.sh文件.
export.sh文件如下:
#!/bin/sh

FILES=`find . -name "*.pk8"`
cat > generateKeystore.bat << END
rem autogenerated file
END

for FILE in $FILES 
do
FILE_NAME=`echo $FILE | awk -F.pk8 '{print $1}'`
if [ -f ${FILE_NAME}.pem ] 
then
    echo "file ${FILE_NAME}.pem exists"
else
`openssl pkcs8 -inform DER -nocrypt -in ${FILE} -out ${FILE_NAME}.pem`
fi

`openssl pkcs12 -export -in ${FILE_NAME}.x509.pem -out ${FILE_NAME}.p12 -inkey ${FILE_NAME}.pem -password pass:android -name androiddebugkey`
cat >> generateKeystore.bat << END
keytool -importkeystore -deststorepass android -destkeystore ${FILE_NAME}.jks -srckeystore ${FILE_NAME}.p12 -srcstoretype PKCS12 -srcstorepass android
END

done
在运行完成后,将在当前目录看到若干*pem文件,*.p12文件及一个generateKeystore.bat文件,window中直接双击运行它。这时你将看到生成的*.jks文件,它们就是我们需要得keystore文件。
generateKeystore.bat文件如下:
rem autogenerated file
keytool -importkeystore -deststorepass android -destkeystore ./media.jks -srckeystore ./media.p12 -srcstoretype PKCS12 -srcstorepass android
keytool -importkeystore -deststorepass android -destkeystore ./platform.jks -srckeystore ./platform.p12 -srcstoretype PKCS12 -srcstorepass android
keytool -importkeystore -deststorepass android -destkeystore ./shared.jks -srckeystore ./shared.p12 -srcstoretype PKCS12 -srcstorepass android
keytool -importkeystore -deststorepass android -destkeystore ./testkey.jks -srckeystore ./testkey.p12 -srcstoretype PKCS12 -srcstorepass android
2.2、Keytool-IUI界面化方式。
Keytool-IUI is Cryptography GUI 工具. It allows create, manage keys and certificates, encrypt and decrypt files.
可以在这里下载它http://code.google.com/p/keytool-iui/downloads/detail?name=ktl241sta.jar&can=2&q=

1 0