单例模式RSA工具类
1 package com.founder.centerbusiness.common.utils;
2
3 import org.apache.commons.codec.binary.Base64;
4 import javax.crypto.Cipher;
5 import java.security.KeyFactory;
6 import java.security.KeyPair;
7 import java.security.KeyPairGenerator;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.SecureRandom;
10 import java.security.interfaces.RSAPrivateKey;
11 import java.security.interfaces.RSAPublicKey;
12 import java.security.spec.PKCS8EncodedKeySpec;
13 import java.security.spec.X509EncodedKeySpec;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.concurrent.locks.ReentrantLock;
17
18 //Author:guanghe
19 public class RSAEncryptUtil {
20
21 //单例
22 private static Map<Integer, String> keyMap;
23 private static ReentrantLock lock = new ReentrantLock();
24
25 //生成密钥对
26 public static void genKeyPair() throws NoSuchAlgorithmException {
27 if(keyMap == null) {
28 lock.lock();
29 if(keyMap == null) {
30 // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
31 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
32 // 初始化密钥对生成器,密钥大小为512-1024位
33 keyPairGen.initialize(512,new SecureRandom());
34 // 生成一个密钥对,保存在keyPair中
35 KeyPair keyPair = keyPairGen.generateKeyPair();
36 RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
37 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
38 String publicKeyString = new String(Base64.encodeBase64(publicKey.getEncoded()));
39 // 得到私钥字符串
40 String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));
41 // 将公钥和私钥保存到Map
42 keyMap = new HashMap<>();
43 keyMap.put(0,publicKeyString);
44 keyMap.put(1,privateKeyString);
45 }
46 lock.unlock();
47 }
48 }
49
50 //获取公钥
51 public static String getPublicKey() throws NoSuchAlgorithmException{
52 if(keyMap == null) genKeyPair();
53 return keyMap.get(0);
54 }
55
56 //获取私钥
57 public static String getPrivateKey() throws NoSuchAlgorithmException{
58 if(keyMap == null) genKeyPair();
59 return keyMap.get(1);
60 }
61
62 //加密
63 public static String encrypt(String str) throws Exception {
64 return encrypt(str, getPrivateKey());
65 }
66
67 //加密
68 public static String encrypt(String str, String publicKey ) throws Exception {
69 //base64编码的公钥
70 byte[] decoded = Base64.decodeBase64(publicKey);
71 RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
72 //RSA加密
73 Cipher cipher = Cipher.getInstance("RSA");
74 cipher.init(Cipher.ENCRYPT_MODE, pubKey);
75 return Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));
76 }
77
78 //解密
79 public static String decrypt(String str) throws Exception {
80 return decrypt(str, getPrivateKey());
81 }
82
83 //解密
84 public static String decrypt(String str, String privateKey) throws Exception {
85 //64位解码加密后的字符串
86 byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));
87 //base64编码的私钥
88 byte[] decoded = Base64.decodeBase64(privateKey);
89 RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
90 //RSA解密
91 Cipher cipher = Cipher.getInstance("RSA");
92 cipher.init(Cipher.DECRYPT_MODE, priKey);
93 return new String(cipher.doFinal(inputByte));
94 }
95
96 public static void main(String[] args) throws Exception {
97 //加密字符串
98 String message = "guanghe";
99 System.out.println("随机生成的公钥为:" + getPublicKey());
100 System.out.println("随机生成的私钥为:" + getPrivateKey());
101 String messageEn = encrypt(message,getPublicKey());
102 System.out.println(message + " 加密后的字符串为:" + messageEn);
103 String messageDe = decrypt(messageEn,getPrivateKey());
104 System.out.println("还原后的字符串为:" + messageDe);
105 }
106
107 }