RSA加密工具类(非对称加密算法)
转载
import com.jfinal.log.Log;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
public abstract class ToolRSA {
private static final Log log = Log.getLog(ToolRSA.class);
public static final String KEY_ALGORITHM = "RSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
* RSA密钥长度 默认1024位, 密钥长度必须是64的倍数, 范围在512至65536位之间。
private static final int KEY_SIZE = 512;
public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
public static byte[] getPrivateKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
public static byte[] getPublicKey(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
public static Map<String, Object> initKey() throws Exception {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(KEY_SIZE);
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
Map<String, Object> keyMap = new HashMap<String, Object>(2);
keyMap.put(PUBLIC_KEY, publicKey);
keyMap.put(PRIVATE_KEY, privateKey);
public static String getPublicKeyStr(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PUBLIC_KEY);
return Base64.encodeBase64String(key.getEncoded());
public static String getPrivateKeyStr(Map<String, Object> keyMap) throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return Base64.encodeBase64String(key.getEncoded());
public static byte[] getPublicKeyByte(String publicKeyStr) {
return Base64.decodeBase64(publicKeyStr);
public static byte[] getPrivateKeyByte(String privateKeyStr) {
return Base64.decodeBase64(privateKeyStr);
public static void main(String[] args) {
String dataStr = "今天天气很好";
Map<String, Object> keyMap = initKey();//初始化公钥和私钥
byte[] publicKey = getPublicKey(keyMap);
byte[] privateKey = getPrivateKey(keyMap);
//利用base64生成String字符串的公钥和私钥
String publicKeyStr = Base64.encodeBase64String(publicKey);//生成字符串公钥
byte[] publicKey2 = Base64.decodeBase64(publicKeyStr);//base64还原了公钥publicKey
String privateKeyStr = Base64.encodeBase64String(privateKey);//生成字符串私钥
byte[] privateKey2 = Base64.decodeBase64(privateKeyStr);//base64还原了私钥privateKey
String gStr = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALni7Ge9AHPZKSIAaMmB5dh/3852ZyKWPQIe0VRU3xnymOI7+3XX3tXMMiH7xOYLG7YucaEPm9gUJYS8PFhOGU8CAwEAAQ==";
String pStr = "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAueLsZ70Ac9kpIgBoyYHl2H/fznZnIpY9Ah7RVFTfGfKY4jv7ddfe1cwyIfvE5gsbti5xoQ+b2BQlhLw8WE4ZTwIDAQABAkEAl/U4+wkXwJyrMn+RKgHFqrnGdNp4L18QGpak20LSaztwDRUgDhYzGh6HEDhQNB+UG5v+RV2ZYCPuQvUlUjWImQIhAOxoXBGTbw8MbylCur7jOu/N0kEa8TfwDixLShWw64ozAiEAyUqztCQtaT8ZUd9DAl4oOJVucEoLwr5TdRH14RCoUHUCIFakbNHYOgTG5tq1ZSHev9PxmBRaXnvE1DzxnwZSIBE3AiEAvkGVSRdItMmnLPNRatOEQLc9A3V5WEB8sZKgTHJqJGECIHP5aL2buJBVbBMgn8f5wD11TPBKW1ZU9XZrCjGM0Hiw";
byte[] beforData = encryptByPublicKey(dataStr.getBytes(), publicKey);
log.info(new String(beforData));
byte[] afterData = decryptByPrivateKey(beforData, privateKey);
log.info(new String(afterData));
String password = "k4UUA7/bME/EgFUdNE+syvPX88rm2uFJonOP+7ekqbW0Vrz7lUA2zpw3wP+yKlPgc9xGXJKorBvYQ4F8n6EwXw==";//前台传入
String username = "IxpcxKe9D6w6aeNVwQAYtL6btjueTK44fmIt4yCq+Rlc3VxxvJLEKI814LkhvQTalduSKr3NMSAn5Wdl7hUc1Q==";//前台传入
String privateKeyStr = "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAueLsZ70Ac9kpIgBoyYHl2H/fznZnIpY9Ah7RVFTfGfKY4jv7ddfe1cwyIfvE5gsbti5xoQ+b2BQlhLw8WE4ZTwIDAQABAkEAl/U4+wkXwJyrMn+RKgHFqrnGdNp4L18QGpak20LSaztwDRUgDhYzGh6HEDhQNB+UG5v+RV2ZYCPuQvUlUjWImQIhAOxoXBGTbw8MbylCur7jOu/N0kEa8TfwDixLShWw64ozAiEAyUqztCQtaT8ZUd9DAl4oOJVucEoLwr5TdRH14RCoUHUCIFakbNHYOgTG5tq1ZSHev9PxmBRaXnvE1DzxnwZSIBE3AiEAvkGVSRdItMmnLPNRatOEQLc9A3V5WEB8sZKgTHJqJGECIHP5aL2buJBVbBMgn8f5wD11TPBKW1ZU9XZrCjGM0Hiw";
byte[] privateKey = Base64.decodeBase64(privateKeyStr);//base64还原了私钥privateKey
byte[] password2 = decryptByPrivateKey(Base64.decodeBase64(password), privateKey);
log.info(new String(password2));//fxgk@321
byte[] username2 = decryptByPrivateKey(Base64.decodeBase64(username), privateKey);
log.info(new String(username2));//P31170774
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
下载地址:http://travistidwell.com/jsencrypt/
var username = $("#username").val();
var password = $("#password").val();
var publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALni7Ge9AHPZKSIAaMmB5dh/3852ZyKWPQIe0VRU3xnymOI7+3XX3tXMMiH7xOYLG7YucaEPm9gUJYS8PFhOGU8CAwEAAQ==";
var encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKey);
password = encrypt.encrypt(password);
var encrypt = new JSEncrypt();
encrypt.setPublicKey(publicKey);
username = encrypt.encrypt(username);
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。