RSA作为HTTPS协议中最为核心的加密/解密算法,其原理却很简单,很容易理解。RSA的密钥越长,就越难破解。目前被破解的最长RSA密钥是768位二进制。也就是说,长度超过768位的密钥,还无法破解(至少没有人公开宣布)。因此可以认为,1024位的RSA密钥基本安全,2048位的密钥及其安全。
package org.collect.test.rsa;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import java.io.IOException;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RSAUtil {
/**
* 获取RSA公私钥匙对
*/
private static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); //512、1024、2048
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
/**
* 获取公钥(base64编码)
*/
private static String getPublicKey(KeyPair keyPair) {
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return Base64Tool.byteToBase64(bytes);
}
/**
* 获取私钥(Base64编码)
*/
private static String getPrivateKey(KeyPair keyPair) {
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return Base64Tool.byteToBase64(bytes);
}
public static String[] genKeyPair() throws Exception {
KeyPair keyPair = getKeyPair();
String[] keyPairArr = new String[2];
keyPairArr[0] = getPublicKey(keyPair);
keyPairArr[1] = getPrivateKey(keyPair);
return keyPairArr;
}
/**
* 将Base64编码后的公钥转换成PublicKey对象
*/
public static PublicKey string2PublicKey(String pubStr) throws Exception {
byte[] keyBytes = Base64Tool.base64ToByte(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
/**
* 将Base64编码后的私钥转换成PrivateKey对象
*/
public static PrivateKey string2PrivateKey(String priStr) throws Exception {
byte[] keyBytes = Base64Tool.base64ToByte(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
/**
* 公钥加密
*/
public static String publicEncrypt(String content, String publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, RSAUtil.string2PublicKey(publicKey));
byte[] byteEncrypt = cipher.doFinal(content.getBytes("utf-8"));
String msg = Base64Tool.byteToBase64(byteEncrypt);
return msg;
}
/**
* 私钥解密
*/
public static String privateDecrypt(String contentBase64, String privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, RSAUtil.string2PrivateKey(privateKey));
byte[] bytesDecrypt = cipher.doFinal(Base64Tool.base64ToByte(contentBase64));
String msg = new String(bytesDecrypt, "utf-8");
return msg;
}
}
package org.collect.test.rsa;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;
public class Base64Tool {
/**
* 字节数组转Base64编码
*/
public static String byteToBase64(byte[] bytes) {
BASE64Encoder encoder = new BASE64Encoder();
// return encoder.encode(bytes);
return new String(Base64.encodeBase64(bytes));
}
/**
* Base64编码转字节数组
*/
public static byte[] base64ToByte(String base64Key) throws IOException {
BASE64Decoder base64Decoder = new BASE64Decoder();
// return base64Decoder.decodeBuffer(base64Key);
return Base64.decodeBase64(base64Key);
}
}
package org.collect.test.rsa;
public class TestRSA {
public static void main(String args[]) throws Exception {
String[] keyPairArr = RSAUtil.genKeyPair();
System.out.println("公钥: " + keyPairArr[0]);
System.out.println();
//
System.out.println("私钥: " + keyPairArr[1]);
System.out.println();
//
String string = "勇敢行sfsdf3择发生的方式5345353@#¥%……&*():“《》?@#$%^&()<>?:";
String msg = RSAUtil.publicEncrypt(string, keyPairArr[0]);
System.out.println("加密后内容: " + msg);
System.out.println();
//
String mms = RSAUtil.privateDecrypt(msg, keyPairArr[1]);
System.out.println("解密后内容: " + mms);
System.out.println();
}
}