java RSA 非对称加密算法实现
1、新建 RsaUtil 工具类
package com.comm.utils;
import com.alibaba.fastjson.JSON;
import com.comm.entity.po.LoginUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import static java.lang.System.out;
/**
* @Description: TODO
* @Author: mis_wu
* @Date: 2022/7/19
* String publicKey = Base64.encodeBase64String(rsaPublicKey.getEncoded());
* String privateKey = Base64.encodeBase64String(rsaPrivateKey.getEncoded());
* 私钥加密,公钥解密 -- 加密 : Base64.encodeBase64String(result)
* 私钥加密,公钥解密 -- 解密 : new String(result)
**/
@Slf4j
public class RsaUtil {
/**
* 指定加密算法 使用RSA算法,非对称加密
*/
public static final String KEY_ALGORITHM = "RSA";
public static final String RSA_PUBLIC_KEY = "rsaPublicKey";
public static final String RSA_PRIVATE_KEY = "rsaPrivateKey";
/**
* 公钥加密结果
*/
public static final String RSA_ENCRYPTION_RESULT = "encryptionResult";
/**
* 初始化大小,越大越安全
*/
public static final Integer INIT_KEY_SIZE = 1024;
/**
* key
* 保存公钥、私钥、以及公钥加密结果
*/
private static final Map<String,Object> KEY_MAP = new HashMap<>();
/**
* 初始化key
* @throws Exception e
*/
public static void initRsaKey() throws Exception{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGenerator.initialize(INIT_KEY_SIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
log.info("公钥信息生成:publicKey");
log.info("私钥信息生成:privateKey");
KEY_MAP.put(RSA_PUBLIC_KEY,rsaPublicKey);
KEY_MAP.put(RSA_PRIVATE_KEY,rsaPrivateKey);
}
/**
* 公钥加密
* @param data 数据
* @return string
*/
public static String encryptionByPublicKey(String data){
try {
//初始化key
initRsaKey();
RSAPublicKey rsaPublicKey = (RSAPublicKey) KEY_MAP.get(RSA_PUBLIC_KEY);
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] res = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
KEY_MAP.put(RSA_ENCRYPTION_RESULT,res);
return Base64.encodeBase64String(res);
}catch (Exception e){
log.error("数据加密失败:{}",e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* 私钥解密
* @return string
*/
public static String decryptByPrivate(){
try {
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
RSAPrivateKey privateKey = (RSAPrivateKey) KEY_MAP.get(RSA_PRIVATE_KEY);
byte[] res = (byte[]) KEY_MAP.get(RSA_ENCRYPTION_RESULT);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
res = cipher.doFinal(res);
return new String(res);
}catch (Exception e){
log.error("数据解密失败:{}",e.getMessage());
e.printStackTrace();
}
return null;
}
//方法二|私钥加密、公钥解密
private static void jdkRsa(String data) {
try {
// 初始化密钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
keyPairGenerator.initialize(INIT_KEY_SIZE);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
log.info("公钥信息生成:publicKey");
log.info("私钥信息生成:privateKey");
// 私钥加密,公钥解密 -- 加密
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] result = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
// 私钥加密,公钥解密 -- 解密
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
result = cipher.doFinal(result);
out.println("私钥加密,公钥解密 -- 解密"+new String(result));
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、具体实现调用
//加密 返回一个加密串
String pwd = RsaUtil.encryptionByPublicKey(加密数据);
//解密后的数据
String data = RsaUtil.decryptByPrivate();