标签:input cee 没有 led 签名 rac build ant char
在Java安全体系中,签名属于JAAS模块,加解密属于JCE模块。No installed provider supports this key: sun.security.provider.DSAPublicKeyImpl
。public class RSAEntry {
public static void main(String[] args) {
try {
//1.从密钥库中取私钥
KeyStore ks = KeyStore.getInstance("JKS");
FileInputStream ksfis = new FileInputStream("examplestanstore2");
BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
// open keystore and get private key
// alias is ‘signLeal‘, kpasswd/spasswd is ‘vagrant‘
ks.load(ksbufin, "vagrant".toCharArray());
PrivateKey prikey = (PrivateKey) ks.getKey("signLegal", "vagrant".toCharArray());
//2.根据命令行参数取公钥
FileInputStream certfis = new FileInputStream(args[0]);
java.security.cert.CertificateFactory cf =
java.security.cert.CertificateFactory.getInstance("X.509");
java.security.cert.Certificate cert = cf.generateCertificate(certfis);
PublicKey pubKey = cert.getPublicKey();
//3.使用公钥进行加密
String data = "测试数据";
//构建加密解密类
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);//设置为加密模式
byte[] jmdata = cipher.doFinal(data.getBytes());
//打印加密后数据
System.out.println(bytesToHexString(jmdata));
//改为解密模式进行解密
cipher.init(Cipher.DECRYPT_MODE, prikey);//会用私钥解密
jmdata = cipher.doFinal(jmdata);
System.out.println(new String(jmdata));
}catch (Exception e) {
e.printStackTrace();
}
}
//这个方法用于把二进制转换成ASCII字符串。
public static String bytesToHexString(byte[] bytes) {
if (bytes == null)
return "null!";
int len = bytes.length;
StringBuilder ret = new StringBuilder(2 * len);
for (int i = 0; i < len; ++i) {
int b = 0xF & bytes[(i)] >> 4;
ret.append("0123456789abcdef".charAt(b));
b = 0xF & bytes[(i)];
ret.append("0123456789abcdef".charAt(b));
}
return ret.toString();
}
}
编译,并运行
$ javac RSAEntry.java
$ java RSAEntry StanSmith.crt
8fceea48e34fdc786bde05459f3366714b650ff04f4e81e52eca139d8ee0b4acbcad019cd496de3589765894b2d5f4a2af38914af614d9e9b73e551ae01830cd6f49505685d7e527e3adc2b7a2a75608068627c0a12b338d3c743a5de2af2de327a0de14b548604e5c8905747aef077852ecfd2eb4a134ca0f3a56b23db8ae4beb07add5ba3725ab3ee0ffa7481494856144ba5004a329cfe2c43078f0cd95aebcbbfc6c1894efafacac90615e549cb8432c125d912a5e54ce4884f633f3e96bd7b61c1d538e38713716367f7ec6f5ca01288e6d96ad9e3d6515147369144390e1d002b1beaf5797966e3b498cc7def754816c99456ef380b3a83366a44415f6
测试数据
本文展示的算法是一种非对称算法,计算较慢。在SSL中,非对称算法用于客户端和服务器之间交换对称加密的一次性密钥。客户端将一个随机数用服务器的公钥加密发给服务器,如果服务器持有私钥,就能解开密文获得随机数(这个随机数就是对称算法的密钥)。有了对称算法密钥,双方就可以用对称加密进行安全通信了。
标签:input cee 没有 led 签名 rac build ant char
原文地址:http://blog.51cto.com/14158311/2350467