RSA应用场景: 登录, 验证等敏感信息的加密
package com.vxdata.common.utils;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
/**
* RSA 加解密工具类
*/
public class RSAUtils {
/**
* 生成的公钥(给前端)
*/
public static final String PUBLICKEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdp5B9njBBrJqpI8WgQJXkE7FcZ5QcRv+lASyAxR342KvBwaL+m/LaZJNd8XgWZKhntDsfUSD+Pai47Lhr0ioMzCacpybhqrwZJto8gPLWMsWOVHkvq9Kn02T9EswzcNNxNH7uA5/7tnBy1urp4gChLJzlkygBsrtNkBeo58JZGQIDAQAB";
/**
* 生成的私钥
*/
public static final String PRIVATEKEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJ2nkH2eMEGsmqkjxaBAleQTsVxnlBxG/6UBLIDFHfjYq8HBov6b8tpkk13xeBZkqGe0Ox9RIP49qLjsuGvSKgzMJpynJuGqvBkm2jyA8tYyxY5UeS+r0qfTZP0SzDNw03E0fu4Dn/u2cHLW6uniAKEsnOWTKAGyu02QF6jnwlkZAgMBAAECgYEAgH8a3cgZPO4aANIPOdZqVUNXxCEILyQPKcYTJ8GB9FllZL8Wwr8I6cgf9OnjHx1bxWq45YWShqZq5o7KodbiPdNOGbM13Vb5CUArDdaoJiuWKFKBsMmkK7pLqLg4kGJe2eae3niQZt+MRsOCBALN/wTtUMZKa9J8oEQFDYVUCaECQQDbId7ccFMcY0EZIY2QXrMaNNcEbvJvu1Fc3ddXYU4nbWSwznNE74zKDGTqCBoaa2Yu0k9pQ2fuM7Gq6oLLGebjAkEAuC3Rh+0JQtLrVUha45dCBNZmhqCYWrOWk2a8M0OX+mHjO9XgHrzbu/OcDgv94VaSvSqt10Dz9aRQF8KrcvSE0wJABwZl86EyOOXBr4GQ3UD78uogvoakBQr3R8fg4G9BJyhtovFrZplbZRrSnWIXHCO3Seb6HsDHcy57EqSj/sjcywJASEAJG0ZcRK+TC/CHXxKeHmGw9I0ZVzNuFULO/1k7zEgMKRPciJJTrKaDCGuh23znsXeFnJRpX2pm1PtT4JOVawJAA1vxkwYaPBsqqkDmq8rxxgjmRnW0BBSw3X0RBLK1pH9qONMO8uyCpBFpFJmqPF6jWYQrAcgVXVqQkouPZSE/5w==";
/**
* 生成公私钥
*
* @param keySize 密钥长度
*/
public static Map<String, String> genKeyPair(int keySize) {
Map<String, String> keyMap = new HashMap<>();
try {
//创建密钥对生成器
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(keySize);
//生成密钥对
KeyPair keyPair = kpg.generateKeyPair();
//公钥
PublicKey publicKey = keyPair.getPublic();
//私钥
PrivateKey privateKey = keyPair.getPrivate();
keyMap.put("publicKey", Base64.encodeBase64String(publicKey.getEncoded()));
keyMap.put("privateKey", Base64.encodeBase64String(privateKey.getEncoded()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return keyMap;
}
/**
* RSA 加密
*
* @param content 待加密字符串
* @param key Base64编码的公钥字符串
*/
public static String rsaEncrypt(String content, String key) {
byte[] keyBytes = Base64.decodeBase64(key);
String encryptString = null;
try {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
PublicKey pubKey = KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
encryptString = Base64.encodeBase64String(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)));
} catch (Exception e) {
e.printStackTrace();
}
return encryptString;
}
/**
* RSA 解密
*
* @param content 待解密Base64字符串
* @param key Base64编码的私钥字符串
*/
public static String rsaDecrypt(String content, String key) throws Exception {
byte[] contentBytes = Base64.decodeBase64(content);
byte[] keyBytes = Base64.decodeBase64(key);
String decryptString = null;
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
decryptString = new String(cipher.doFinal(contentBytes));
return decryptString;
}
public static void main(String[] args) throws Exception {
// Map<String, String> keyMap = genKeyPair(1024);
// System.out.println("公钥======"+keyMap.get("publicKey"));
// System.out.println("私钥======"+keyMap.get("privateKey"));
String encryptString = rsaEncrypt("测试明文", PUBLICKEY);
System.out.println("密文======" + encryptString);
String decryptString = rsaDecrypt(encryptString, PRIVATEKEY);
System.out.println("明文======" + decryptString);
}
}
版权声明:本文为qq_44154912原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。