keystore生成 linux_使用系统签名生成debug.keystore | 学步园

本文翻译整理自一俄罗斯同事的文档

一、前言

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) andkeytool。

openssl是Linux上的一个工具,keytool则是JDK的一个工具,它位于JDK\bin目录。在此假设你已经把JDK\bin添加到了window的path环境变量中。因为一般用户使用的是window系统,

所以我这里把Cygwin安装在window上来模拟Linux环境,在Cygwin上使用opnssl.

关于Cygwi的详细内容请参考《》。

2.1、命令行方式

在这里我将以platform为例进行讲解。

通过“Cygwin Terminal”的进入Cygwin的命令行,然后切换到key所在的目录。即security目录.

输入以下命令:

opensslpkcs8-inform DER -nocrypt -inplatform.pk8-outplatform.pem

执行该命令,将在目录下生成platform.pem文件

接着输入以下命令:

opensslpkcs12-export -inplatform.x509.pem-outplatform.p12-inkeyplatform.pem-passwordpass:android-nameandroiddebugkey

执行该命令,将在目录下生成platform.p12文件,它本质上应该就是一个数字证书。

进入DOS命令行,切换到key所在的目录。即security

然后输入命令:

keytool-importkeystore-deststorepassandroid-destkeystore./platform.jks-srckeystore./platform.p12-srcstoretype

PKCS12 -srcstorepassandroid

执行该命令,将在目录下生成platform.jks文件,它就是我们需要的keystore文件。它的后缀本身是没有关系。如果你更喜欢其后缀名为keystore。

上面的命令改成这样就行了。

keytool-importkeystore-deststorepassandroid-destkeystore./platform.keystore-srckeystore./platform.p12-srcstoretype

PKCS12 -srcstorepassandroid

另外,我方便我们可以使用批处理来进行处理。

通过“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-IUIis Cryptography GUI 工具. It allows create, manage keys and certificates, encrypt and decrypt files.

在使用Keytool-IUI前,我们还是需要使用opensll来先生成.pem文件。

三、命令行中使用keystore对APK签名

关于此请参考《

四、查看APK使用的数字签名

解压APK文件,对其META-INF目录下的CERT.RSA文件,执行以下命令就可以到它的数字签名的详细情况

keytool-printcert -fileCERT.RSA

另外在Android源码,可以通过应用程序Android.mk文件的LOCAL_CERTIFICATE查看它使用的哪种key。

比如:LOCAL_CERTIFICATE := platform

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值