平方X 发表于 2014-12-27 22:29:46

如何创建自己的证书文件,如何为apk以及zip文件签名

转自http://bbs.angeeks.com/thread-1836955-1-1.html
原文链接:How to Sign Android APK or Zip Files
准备知识:
    当你要发布一个软件或是自制的ROM时,你就需要一个使用了私钥的证书来为.apk或.zip文件进行签名。Android系统使用证书来识别软件作者和软件之间所建立的认证关系。做这个事情最经典的方式就是用keytool创建证书,然后使用jarsigner进行签名。但是本教程则会提供一个对于大多数人来说更为容易的方法,那就是使用一个名为SignApk.jar的工具。
    SignApk.jar是一个已包含在Android平台源码包中的工具,你可以在本贴的附件中下载。如果要使用SignApk.jar,你需要创建一个带有对应证书/公钥的私钥。而你可以使用Openssl来创建私钥/公钥对。在Unix/Linux系统中使用Openssl相对来说比较容易。对于Windows用户,你可以在本贴附件或此链接中下载Windows版本的Openssl。


如何使用OpenSSL创建私钥/公钥对(也就通常说的证书文件,有误勿怪)(Windows版本)
[*]下载附件中的openssl-0.9.8k_WIN32.zip
[*]将下载到的压缩包解压到你电脑上的任意位置(例如:C:\OpenSSL)
[*]在OpenSSL\bin文件夹下按顺序输入(使用CMD命令行工具,其中第2步会需要你输入一些信息,见图):
1、openssl genrsa -out key.pem 1024
2、openssl req -new -key key.pem -config C:\OpenSSL\openssl.cnf -out request.pem
3、openssl x509 -req -days 9999 -in request.pem -signkey key.pem -out certificate.pem
4、openssl pkcs8 -topk8 -outform DER -in key.pem -inform PEM -out key.pk8 -nocrypt   



    http://bbs.angeeks.com/data/attachment/forum/month_1112/1112122047ba8109db4343a86a.jpg
果你的recovery不具备签名校验切换功能,那么用你自己生成的证书文件来给ROM签名,会提示签名校验失败。因为目前几乎所有的第三方recovery中所带的私钥都是testkey的。(附件中的GoAPK.zip里面所包含的证书文件已更新为testkey)
如何使用SignApk.jar来为一个apk或zip文件签名:
[*]下载附件中的GoAPK.zip
[*]将下载到的压缩包解压到你电脑上的任意位置(例如:C:\GoAPK)
[*]如果你的电脑上还没有安装JAVA环境,请下载并进行安装。
[*](如果你创建了自己的私钥/公钥对)复制certificate.pem和key.pk8到你解压得到的GoAPK文件夹中
[*]使用CMD命令行工具cd到GoAPK文件夹,然后输入:(对于我提供的包,只需要将要签名的文件放在GoAPK文件夹中,然后拖至对应的批处理文件上即可)
java -jar signapk.jar certificate.pem key.pk8 your-app.apkyour-signed-app.apk
或是
java -jar signapk.jar certificate.pem key.pk8 your-update.zip your-signed-update.zip


注意:
如果你不想要去创建属于你自己的私钥/公钥对,你可以直接使用我在GoAPK.zip中所提供的。

How to Sign Android APK or Zip FilesMay 21, 2010 by Lorensius Londa 51 Comments


When publishing an application or a custom romyou need to sign the .apk or .zip files with a certificate using a private key. The Android system uses the certificate to identify the author of an application and establish trust relationship between applications. The classic way of doing this was to use keytool then sign it withjarsigner. In this tutorial i’ll explain an alternative method which is relatively easy to use for most peopleusing a tool called SignApk.jar.SignApk.jar is a tool included with the Android platform source bundle, you can download it fromhere. To use SignApk.jar you have to create a private key with it’s corresponding certificate/public key. To create private/public key pair, you can use Openssl. Openssl is relatively easy to use under unix/linux system. For Windows user, you can download Windows version of Openssl here.How to create private/public key pair using openssl (windows version)

[*]Download openssl package from link given above
[*]Extract it anywhere on your drive (eg. C:\openssl)
[*]Within openssl directory type (use cmd tool):
- openssl genrsa -out key.pem 1024
- openssl req -new -key key.pem -out request.pem
- openssl x509 -req -days 9999 -in request.pem -signkey key.pem -out certificate.pem
- openssl pkcs8 -topk8 -outform DER -in key.pem -inform PEM -out key.pk8 -nocrypthttp://londatiga.net/images/signapk/openssl.jpgHow to sign apk or zip files using SignApk.jar:

[*]Download SignApk.rar from link given above
[*]Extract itanywhere on your drive (eg. c:\SignApk)
[*]If you don’t have java installed, downloadand install it.
[*]Copy certificate.pem and key.pk8 into your extracted SignApk directory
[*]Within SignApk directory type:
java -jar signapk.jar certificate.pem key.pk8 your-app.apkyour-signed-app.apk

ORjava -jar signapk.jar certificate.pem key.pk8 your-update.zip your-signed-update.zip
Note:If you don’t want to create your own public/private key pair, you can use test key included in SignApk.rar.Reference:android-dls.com


页: [1]
查看完整版本: 如何创建自己的证书文件,如何为apk以及zip文件签名