RSA对电话号码加解密的工具类
1.工具类如下
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
@Slf4j
public class RSAEncryptUtil {
/**
* publicKeyString && privateKeyString 是 后台加密给前台的加密 公私密钥
*/
public final static String publicKeyString = "DNnYqDYUXlTHpCzW6bUuSYi9LJDNqRkeBsz78U68bfSDNnYqDYUXlTHpCzW6bUu/jBdu3f9nYU5LwVE1qorOy0NrBsDECAwEAAQ==";
public final static String privateKeyString = "jRFuF+h9EmB9mfrprwBAiEA+51jPBkOZ2WyMFG+rsl5xl09C87v4yliCcPjgReVMekLNbptS7+MF27d/2dhTkvBUTWqis7LQ2sGwMQIDAQABAkBAlv4XLYzlf7WiziYh8pHiL0KRH55cDM8/wp7Ah0ByzMKAtKSBL/+uyDVM8jiag1e99jRFuF+h9EmB9mfrprwBAiEA+51jPBkOZ2WyMFG+rsl5xl09C87v4yliCcPjgdvEj+kCIQDTHhP4Avr2nWRdlWnxUIj9iUggqfEdfCDzAq9ChYf5CQIhANbNxWMQDQDui9G+Z6h2LjTPcoc7MgZfVOf7SKWbBdLJAiBMXlYE4jccXMUAoVrogyR66l0gC7l/2/YjUfZKDvmn2QIgEgOwDb6juF5aolA8/K7noOAZ9vLuMhRu9T8qlllRHCw=";
/**
* publicKeyClientString && privateKeyClientString 是 前台加密给后台的加密 公私密钥
*/
public final static String publicKeyClientString = "YgZAcBsPD9/g/rx5z5xM5Z9LvZOQe8GqhV1iC+nkTA6PIchCBM4UKijNS1qycu2cv1y4OTKqYYgZAcBsPD9/g/rx5z5xM5Z9LvZOQe8GqhV1icCAwEAAQ==";
public final static String privateKeyClientString = "RMDo8hyEIEzhQqKM1LWrJy7Zy/XLg5MqphiBkBwGw8P3+D+vHnPnEzln0u9k5B7waqFXWJwIDAQABAkAygcStEAhYZCiY1usyXUL6eRMDo8hyEIEzhQqKM1LWrJy7Zy/XLg5MqphiBkBwGw8P3+D+vHnPnEzln0u9k5B7waqFXWJwIDAQABAkAygcStb1hOX6LRm9rpmT3IB5CcZJLuFz5IPbqzWgJJKBuovoI8NXkuXgRSw/xRPzvicmkEPHRjXATdRFKw1i9BAiEA19ggoZRyL3yhLT+Y4+qAG3W9b+UKcCCteqWP3QUwS/cCIQCeXYqgq1Eq5aHlcBXdOe8kEhqmwSUhbhSBlkTxWhtbUQIgez64Wyq1DHJgYcLXnQQwQ/21vmeMzzuJcueIPs6Qpj8CIDbf6OpVkrGPknLDTQA0U/Ymw89me+xCjZ59qHQNKg+RAiAmKx8HLFE/ejiJwL8sPtSE34yomIlsS23uAX+JXGAy7w==";
public static void main(String args[]) throws Exception {
RSAEncryptUtil.genKeyPair();
}
/**
* 随机生成密钥对
* @throws NoSuchAlgorithmException
*/
public static void genKeyPair() throws Exception {
// // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
// KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// // 初始化密钥对生成器,密钥大小为96-1024位
// keyPairGen.initialize(512,new SecureRandom());
// // 生成一个密钥对,保存在keyPair中
// KeyPair keyPair = keyPairGen.generateKeyPair();
// // 得到私钥
// RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
// // 得到公钥
// RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
// String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
// // 得到私钥字符串
// String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
// // 将公钥和私钥保存到Map
// //0表示公钥
// System.out.println(publicKeyString);
// //1表示私钥
// System.out.println(privateKeyString);
//String messageEn = decrypt("Xt702Bm5Mb/y4aS/KrnSZ57gJMkhE8fy2xAkiHx/OZKarEt8QGHeCEuEXKPv9IB8UsDd8nVybHW3Rew8JFQ5dA==");
//1表示私钥
//System.out.println(messageEn);
// String encryptStr = encryptClient("13462458848");
System.out.println(encrypt("13111111111"));
System.out.println(decrypt("Xh+ObL4j3mwoat9XCVTJZIXh+ObL4j3mwoat9XCVTJZIkApuSWyS5euy8+ah4rYR1ZrdE"));
System.out.println(decrypt("sJrS9nO8sJPWC1RLwKguwTqYqtsJrS9nO8sJPWC1RLwKguwTqYqtDsRsJrS9nO8sJPWC1"));
System.out.println(decrypt("jLz9DtdRnG/SNsulqxc24FfEK8dFjLz9DtdRnG/SNsulqxc24FfNXLxClfn9j/VUm4w=="));
System.out.println(decrypt("fRPGaVPwnL09n33GGt+3JYFXeKg22mDx2Obkx19zclYr6LoiDuRPGaVPwnL09n3iCnA=="));
System.out.println(decrypt("KIRCvL6QnUU0309+ymrJIEpdloStzEM0ftZwjPKIRC9+ymrJIEpdloSUjMdhZ42sAAw=="));
//System.out.println(decryptClient(encryptStr));
//System.out.println(decrypt(encryptStr));
}
/**
* RSA公钥加密
* @param str 加密字符串
* @return 密文
* @throws Exception 加密过程中的异常信息
*/
public static String encrypt(String str) throws BusinessException{
return encryptByPublicKey(str,publicKeyString);
}
/**
* RSA私钥解密
* @param str 加密字符串
* @return 铭文
* @throws Exception 解密过程中的异常信息
*/
public static String decrypt(String str) throws BusinessException{
return decryptByPrivateKey(str,privateKeyString);
}
/**
* RSA公钥加密 - 前台给后台
* @param str 加密字符串
* @return 密文
* @throws Exception 加密过程中的异常信息
*/
public static String encryptClient(String str) throws BusinessException{
return encryptByPublicKey(str,publicKeyClientString);
}
/**
* RSA私钥解密 - 前台给后台
* @param str 加密字符串
* @return 铭文
* @throws Exception 解密过程中的异常信息
*/
public static String decryptClient(String str){
return decryptByPrivateKey(str,privateKeyClientString);
}
static String encryptByPublicKey(String str,String publicKey){
String outStr="";
try {
//base64编码的公钥
byte[] decoded = Base64.decodeBase64(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
}catch (Exception e){
log.error("加密失败",e.getMessage());
throw new BusinessException("电话号码编码失败");
}
return outStr;
}
static String decryptByPrivateKey(String str,String privateKey){
String outStr="";
try {
//64位解码加密后的字符串
byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
//base64编码的私钥
byte[] decoded = Base64.decodeBase64(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
outStr = new String(cipher.doFinal(inputByte));
}catch (Exception e){
log.error("解密失败",e.getMessage());
throw new BusinessException("电话号码编码失败");
}
return outStr;
}
}
标签:加密,电话号码,加解密,RSA,static,str,println,String
From: https://blog.csdn.net/hejiefeng111/article/details/140350319