You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					
					
						
							180 lines
						
					
					
						
							6.3 KiB
						
					
					
				
			
		
		
	
	
							180 lines
						
					
					
						
							6.3 KiB
						
					
					
				package com.ynxbd.common.helper.common;
 | 
						|
 | 
						|
import lombok.extern.slf4j.Slf4j;
 | 
						|
import org.apache.commons.codec.binary.Base64;
 | 
						|
 | 
						|
import javax.crypto.Cipher;
 | 
						|
import java.nio.charset.StandardCharsets;
 | 
						|
import java.security.*;
 | 
						|
import java.security.interfaces.RSAPrivateKey;
 | 
						|
import java.security.interfaces.RSAPublicKey;
 | 
						|
import java.security.spec.PKCS8EncodedKeySpec;
 | 
						|
import java.security.spec.X509EncodedKeySpec;
 | 
						|
import java.util.HashMap;
 | 
						|
import java.util.Map;
 | 
						|
 | 
						|
/**
 | 
						|
 * RSA非对称加密
 | 
						|
 *
 | 
						|
 * @Author wsq
 | 
						|
 * @Date 2021/07/28 11:01:28
 | 
						|
 * @Copyright @ 2020 云南新八达科技有限公司 All rights reserved.
 | 
						|
 */
 | 
						|
@Slf4j
 | 
						|
public class RSAHelper {
 | 
						|
    private RSAHelper() {
 | 
						|
    }
 | 
						|
 | 
						|
    private static final int KEY_LENGTH = 1024;
 | 
						|
    private static final Map<Integer, String> KEY_MAP = new HashMap<>();  // 用于封装随机产生的公钥与私钥
 | 
						|
 | 
						|
 | 
						|
    static {
 | 
						|
        try {
 | 
						|
            // KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
 | 
						|
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
 | 
						|
            // 初始化密钥对生成器,密钥大小为96-1024位
 | 
						|
            keyPairGen.initialize(KEY_LENGTH, new SecureRandom());
 | 
						|
            // 生成一个密钥对,保存在keyPair中
 | 
						|
            KeyPair keyPair = keyPairGen.generateKeyPair();
 | 
						|
            RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();   // 得到私钥
 | 
						|
            RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  // 得到公钥
 | 
						|
            String publicKeyStr = new String(Base64.encodeBase64(publicKey.getEncoded()));
 | 
						|
            // 得到私钥字符串
 | 
						|
            String privateKeyStr = new String(Base64.encodeBase64((privateKey.getEncoded())));
 | 
						|
            // 将公钥和私钥保存到Map
 | 
						|
            KEY_MAP.put(0, publicKeyStr);   // 0表示公钥
 | 
						|
            KEY_MAP.put(1, privateKeyStr);  // 1表示私钥
 | 
						|
        } catch (Exception e) {
 | 
						|
            ErrorHelper.println(e);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取公钥
 | 
						|
     *
 | 
						|
     * @return 公钥
 | 
						|
     */
 | 
						|
    public static String getPublicKey() {
 | 
						|
        return KEY_MAP.get(0);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取私钥
 | 
						|
     *
 | 
						|
     * @return 私钥
 | 
						|
     */
 | 
						|
    public static String getPrivateKey() {
 | 
						|
        return KEY_MAP.get(1);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * RSA公钥加密
 | 
						|
     *
 | 
						|
     * @param encryptData 加密数据
 | 
						|
     * @return 密文
 | 
						|
     */
 | 
						|
    public static String encrypt(String encryptData) {
 | 
						|
        return encrypt(encryptData, getPublicKey());
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * RSA公钥加密
 | 
						|
     *
 | 
						|
     * @param encryptData 加密数据
 | 
						|
     * @return 密文
 | 
						|
     */
 | 
						|
    public static String encrypt(String encryptData, String publicKey) {
 | 
						|
        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);
 | 
						|
            return Base64.encodeBase64String(cipher.doFinal(encryptData.getBytes(StandardCharsets.UTF_8)));
 | 
						|
        } catch (Exception e) {
 | 
						|
            log.error("[RSA]加密失败:[{}]", e.getMessage());
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * RSA私钥解密
 | 
						|
     *
 | 
						|
     * @param decryptData 加密数据
 | 
						|
     * @return 明文
 | 
						|
     */
 | 
						|
    public static String decrypt(String decryptData) {
 | 
						|
        return decrypt(decryptData, getPrivateKey());
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * RSA私钥解密
 | 
						|
     *
 | 
						|
     * @param decryptData 加密数据
 | 
						|
     * @return 明文
 | 
						|
     */
 | 
						|
    public static String decrypt(String decryptData, String privateKey) {
 | 
						|
        if (decryptData == null || "".equals(decryptData)) {
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
        try {
 | 
						|
            // 64位解码加密后的字符串
 | 
						|
            byte[] inputByte = Base64.decodeBase64(decryptData.getBytes(StandardCharsets.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);
 | 
						|
            return new String(cipher.doFinal(inputByte));
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
            log.error("[RSA]解密失败:[{}]" + e.getMessage());
 | 
						|
        }
 | 
						|
        return null;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public static void main(String[] args) throws Exception {
 | 
						|
        for (int i = 0; i < 100; i++) {
 | 
						|
            String message = "111223311";
 | 
						|
            System.out.println("随机生成的公钥为:" + KEY_MAP.get(0));
 | 
						|
            System.out.println("随机生成的私钥为:" + KEY_MAP.get(1));
 | 
						|
            long beg = System.currentTimeMillis();
 | 
						|
 | 
						|
            String messageEn = encrypt(message);
 | 
						|
            System.out.println(message + "\t加密后的字符串为:" + messageEn);
 | 
						|
//        String messageDe = decrypt(messageEn);
 | 
						|
            long end = System.currentTimeMillis();
 | 
						|
            System.out.println(end - beg);
 | 
						|
        }
 | 
						|
 | 
						|
        //加密字符串
 | 
						|
 | 
						|
//        System.out.println("还原后的字符串为:" + messageDe);
 | 
						|
    }
 | 
						|
 | 
						|
//    public static void main(String[] args) throws Exception {
 | 
						|
//        // Base64编码的公钥
 | 
						|
//        byte[] decoded = Base64.decodeBase64(getPublicKey());
 | 
						|
//        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
 | 
						|
//        // RSA加密
 | 
						|
//        Cipher cipher = Cipher.getInstance("RSA");
 | 
						|
//        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
 | 
						|
//
 | 
						|
//        String a = Base64.encodeBase64String(cipher.doFinal("encryptData".getBytes(StandardCharsets.UTF_8)));
 | 
						|
//        System.out.println(a);
 | 
						|
//
 | 
						|
//        String b = Base64.encodeBase64String(cipher.doFinal("encryptData".getBytes(StandardCharsets.UTF_8)));
 | 
						|
//        System.out.println(a);
 | 
						|
//
 | 
						|
//        String c = Base64.encodeBase64String(cipher.doFinal("encryptData".getBytes(StandardCharsets.UTF_8)));
 | 
						|
//        System.out.println(a);
 | 
						|
//
 | 
						|
//        String d = Base64.encodeBase64String(cipher.doFinal("encryptData".getBytes(StandardCharsets.UTF_8)));
 | 
						|
//        System.out.println(a);
 | 
						|
//    }
 | 
						|
}
 | 
						|
 |