数字签名的原理以下所示, 灰色的 原文件、公钥、加密后的摘要 是要发送给对方的。html
对方验证的过程是 用原文件生成摘要1, 同时用公钥解密 加密后的摘要 获得 摘要2, 而后对比摘要1 和 摘要2 是否相同。java
咱们使用keytool 生成的keystore 文件里存储了私钥和公钥信息,因此因此签名apk时 须要提供 keystore 文件。打包工具集里的jarsigner、 signapk 等工具会提取其中的私钥、公钥来使用。git
公钥存在CERT.RSA 证书中,存放于apk 的 META-INFO/CERT.RSA .算法
(原文:http://www.educity.cn/wenda/124033.html)
keytool -genkey -alias test -keyalg RSA -keystore c:/key.store
生成keyStore
RSA是一个既能用于数据加密也能用于数字签名的算法。
DSA(Digital Signature Algorithm,数字签名算法,用做数字签名标准的一部分),它是另外一种公开密钥算法,它不能用做加密,只用做数字签名。DSA使用公开密钥,为接受者验证数据的完整性和数据发送者的身份。
提取证书:
经过keytool命令咱们能够很轻松的提取证书.
证书包括主体信息,公钥.
keytool -export -alias 别名 -keystore 文件名 -file 证书名称
可是咱们没法经过KEYTOOL工具来提取私钥的..咱们只能经过java的 KeyStore 类getEntry() 或者getKey()来提取私钥.
读取keyStore文件:工具
char[] password = "password".toCharArray();
java.io.FileInputStream fis = new java.io.FileInputStream("c:/server/server_keystore");
//从指定的输入流中加载此 KeyStore
ks.load(fis, password);
//keystore 中的每一项都用“别名”字符串标识。
//使用指定保护参数获取指定别名的 keystore Entry。
//KeyStore.PrivateKeyEntry 保存 PrivateKey 和相应证书链的 KeyStore 项。
//方法1.
KeyStore.PrivateKeyEntry pkEntry = (KeyStore.PrivateKeyEntry) ks.getEntry( "keystore别名",
new KeyStore.PasswordProtection(password) );
// 返回与给定别名相关联的密钥
//方法2.
PrivateKey key = (PrivateKey) ks.getKey("ser", password);
加密
怎么来验证提取的私钥是否正确呢 (由于公钥私钥必须成对出现,咱们能够经过证书提取去公钥,而后用公钥加密,使用刚刚得到的私钥解密)
提取证书的方法:
keytool -export -alias 别名 -keystore 文件名 -file 证书名称
spa
//经过证书,获取公钥
CertificateFactory cf = CertificateFactory.getInstance("X.509");
FileInputStream in = new FileInputStream("C:\\server\\server.cer");
//生成一个证书对象并使用从输入流 inStream 中读取的数据对它进行初始化。
Certificate c = cf.generateCertificate(in);
PublicKey publicKey = c.getPublicKey();
//经过下面这段代码提取的私钥是否正确
String before = "asdf";
byte[] plainText = before.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// 用公钥进行加密,返回一个字节流
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(plainText);
// 用私钥进行解密,返回一个字节流
cipher.init(Cipher.DECRYPT_MODE, myPrivateKey);
byte[] newPlainText = cipher.doFinal(cipherText);
// 若是newPlainText 和 before 相同的话,那么私钥和公钥是配对的
System.out.println(new String(newPlainText, "UTF-8"));
code