|
| 1 | +package com.weiziplus.common.util; |
| 2 | + |
| 3 | +import lombok.Getter; |
| 4 | +import lombok.extern.slf4j.Slf4j; |
| 5 | + |
| 6 | +import javax.crypto.Cipher; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.security.*; |
| 9 | +import java.security.interfaces.RSAPrivateKey; |
| 10 | +import java.security.interfaces.RSAPublicKey; |
| 11 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 12 | +import java.security.spec.X509EncodedKeySpec; |
| 13 | +import java.util.Base64; |
| 14 | + |
| 15 | +/** |
| 16 | + * RSA加密解密 |
| 17 | + * |
| 18 | + * @author wanglongwei |
| 19 | + * @date 2020/06/03 18/09 |
| 20 | + */ |
| 21 | +@Slf4j |
| 22 | +@Getter |
| 23 | +public class RsaUtils { |
| 24 | + |
| 25 | + /** |
| 26 | + * 公钥 |
| 27 | + */ |
| 28 | + private String publicKey; |
| 29 | + |
| 30 | + /** |
| 31 | + * 私钥 |
| 32 | + */ |
| 33 | + private String privateKey; |
| 34 | + |
| 35 | + private RsaUtils(String publicKey, String privateKey) { |
| 36 | + this.publicKey = publicKey; |
| 37 | + this.privateKey = privateKey; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * 标识RSA加密算法名字常量 |
| 42 | + */ |
| 43 | + private final static String RSA = "RSA"; |
| 44 | + |
| 45 | + /** |
| 46 | + * 获取公钥私钥对 |
| 47 | + * |
| 48 | + * @return |
| 49 | + */ |
| 50 | + public static RsaUtils getKeyPair() { |
| 51 | + // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象 |
| 52 | + KeyPairGenerator keyPairGen = null; |
| 53 | + try { |
| 54 | + keyPairGen = KeyPairGenerator.getInstance(RSA); |
| 55 | + } catch (NoSuchAlgorithmException e) { |
| 56 | + log.warn("RSA生成公钥私钥对出错,详情:" + e); |
| 57 | + return null; |
| 58 | + } |
| 59 | + // 初始化密钥对生成器 |
| 60 | + keyPairGen.initialize(1024, new SecureRandom()); |
| 61 | + // 生成一个密钥对,保存在keyPair中 |
| 62 | + KeyPair keyPair = keyPairGen.generateKeyPair(); |
| 63 | + // 得到私钥 |
| 64 | + RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); |
| 65 | + // 得到公钥 |
| 66 | + RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); |
| 67 | + //得到公钥字符串 |
| 68 | + String publicKeyString = Base64.getEncoder().encodeToString(publicKey.getEncoded()); |
| 69 | + // 得到私钥字符串 |
| 70 | + String privateKeyString = Base64.getEncoder().encodeToString(privateKey.getEncoded()); |
| 71 | + return new RsaUtils(publicKeyString, privateKeyString); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 私钥解密 |
| 76 | + * |
| 77 | + * @param privateKey |
| 78 | + * @param content |
| 79 | + * @return |
| 80 | + */ |
| 81 | + public static String privateDecrypt(String privateKey, String content) { |
| 82 | + try { |
| 83 | + //64位解码加密后的字符串 |
| 84 | + byte[] inputByte = Base64.getDecoder().decode(content); |
| 85 | + //base64编码的私钥 |
| 86 | + byte[] decoded = Base64.getDecoder().decode(privateKey); |
| 87 | + RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance(RSA).generatePrivate(new PKCS8EncodedKeySpec(decoded)); |
| 88 | + //RSA解密 |
| 89 | + Cipher cipher = Cipher.getInstance(RSA); |
| 90 | + cipher.init(Cipher.DECRYPT_MODE, priKey); |
| 91 | + return new String(cipher.doFinal(inputByte), StandardCharsets.UTF_8); |
| 92 | + } catch (Exception e) { |
| 93 | + log.warn("RSA私钥解密出错,详情:" + e); |
| 94 | + return null; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * 公钥加密 |
| 100 | + * |
| 101 | + * @param publicKey |
| 102 | + * @param content |
| 103 | + * @return |
| 104 | + */ |
| 105 | + public static String publicEncrypt(String publicKey, String content) { |
| 106 | + try { |
| 107 | + //base64编码的公钥 |
| 108 | + byte[] decoded = Base64.getDecoder().decode(publicKey); |
| 109 | + RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(decoded)); |
| 110 | + //RSA加密 |
| 111 | + Cipher cipher = Cipher.getInstance(RSA); |
| 112 | + cipher.init(Cipher.ENCRYPT_MODE, pubKey); |
| 113 | + return Base64.getEncoder().encodeToString(cipher.doFinal(content.getBytes(StandardCharsets.UTF_8))); |
| 114 | + } catch (Exception e) { |
| 115 | + log.warn("RSA公钥加密出错,详情:" + e); |
| 116 | + return null; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments