RSA算法
基于jdk的RSA算法有两种非对称的加解密方式
1.公钥加密,私钥解密
/**
* 公钥加密
* @param originStr
* originStr 需要加密的原始字符串
* @return 经过公钥加密并base64编码的字符串
*/
public static String publicKeyCrypto(String originStr){
String resultStr = "";
try {
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(originStr.getBytes());
System.out.println("公钥加密后base64编码:" + Base64.encodeBase64String(result));
resultStr = Base64.encodeBase64String(result);
}catch (Exception e){
resultStr ="公钥加密出错";
}finally {
return resultStr;
}
}
/**
* 私钥解密
* @param publicKeyCryptoStr
* publicKeyCryptoStr 经过公钥加密并base64编码的字符串
* @return 未加密的原始字符串
*/
public static String privateKeyDecrypto(String publicKeyCryptoStr){
byte [] originStrBytes = Base64.decodeBase64(publicKeyCryptoStr.getBytes());
String resultStr = "";
try {
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
Cipher cipher = Cipher.getInstance(RSA);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(originStrBytes);
System.out.println("解密得到的字符串:" + new String(result));
resultStr = new String(result);
}catch (Exception e){
resultStr ="私钥解密出错";
}finally {
return resultStr;
}
}
2.私钥加密,公钥解密
/**
* 私钥加密
* @param originStr
* originStr 需要加密的原始字符串
* @return 经过私钥加密并base64编码的字符串
*/
public static String privateKeyCrypto(String originStr){
String resultStr = "";
try {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(originStr.getBytes());
System.out.println("加密结果经过base64编码:" + Base64.encodeBase64String(result));
resultStr = Base64.encodeBase64String(result);
}catch (Exception e){
resultStr ="私钥加密出错";
}finally {
return resultStr;
}
}
/**
* 公钥解密
* @param privateKeyCryptoStr
* privateKeyCryptoStr 经过私钥加密后并base64编码的字符串
* @return 未加密的原始字符串
*/
public static String publicKeyDecrypto(String privateKeyCryptoStr){
byte [] originStrBytes = Base64.decodeBase64(privateKeyCryptoStr.getBytes());
String resultStr = "";
try {
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
Cipher cipher = Cipher.getInstance(RSA);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] result = cipher.doFinal(originStrBytes);
System.out.println("公钥解密后:" + new String(result));
resultStr = new String(result);
}catch (Exception e){
resultStr ="公钥解密出错";
}finally {
return resultStr;
}
}
公钥私钥对的获取,主要是获得公钥私钥的字节数组
KeyPairGenerator senderKeyPairGenerator = KeyPairGenerator.getInstance(RSA);
senderKeyPairGenerator.initialize(512);
KeyPair keyPair = senderKeyPairGenerator.generateKeyPair();
byte [] privateKeyBytes = keyPair.getPrivate().getEncoded();
byte [] publicKeyBytes = keyPair.getPublic().getEncoded();