liusheng
4 天以前 78f4e7c416e50c4f21709feda1034cb1047444fd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.ruoyi.common.utils;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
import javax.crypto.Cipher;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
 
@Slf4j
@Component
public class RSAPublicKeyExample {
 
//
//    /**
//     * 数据解密
//     *
//     * @param encryptedData
//     * @return
//     */
//    public String decryptedData(String encryptedData, String pri_key) {
//        String privateKeyString = "私钥的Base64编码字符串"; // 后端私钥的Base64编码字符串
//
//        try {
//            // 将私钥Base64编码字符串转换为PrivateKey对象
//            byte[] privateKeyBytes = Base64.getDecoder().decode(pri_key);
//            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
//            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
//
//            // 使用私钥解密数据
//            Cipher decryptCipher = Cipher.getInstance("RSA");
//            decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
//            byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedData));
//
//            // 解密后的数据
//            String decryptedData = new String(decryptedBytes);
//            System.out.println("解密后的数据:" + decryptedData);
//            return decryptedData;
//        } catch (Exception e) {
//            log.error("解密报错了:{}", e.getMessage());
//        }
//        return null;
//    }
//
//    /**
//     * 要加密的明文数据
//     *
//     * @param plainText
//     * @return
//     */
//    public String encryptedData(String plainText, String pub_key) {
//        log.info("需要加密的数据:{}", plainText);
//        try {
//
//            byte[] publicKeyBytes = Base64.getDecoder().decode(pub_key);
//            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
//            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//            PublicKey publicKey = keyFactory.generatePublic(keySpec);
//
//            // 使用公钥加密数据
//            Cipher encryptCipher = Cipher.getInstance("RSA");
//            encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
//            byte[] encryptedBytes = encryptCipher.doFinal(plainText.getBytes());
//
//            // 将加密后的数据转换为Base64编码的字符串
//            String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
//            log.info("Base64加密后的数据:{}", encryptedText);
//            encryptedText = URLEncoder.encode(encryptedText, StandardCharsets.UTF_8.toString());
//            log.info("URLEncoder编码后的数据:{}", encryptedText);
//            String decodedString = URLDecoder.decode(encryptedText, "UTF-8");
//            log.info("URLEncoder解码后的数据:{}", decodedString);
//            return encryptedText;
//        } catch (Exception e) {
//            log.error("加密失败了:{}", e.getMessage());
//        }
//        return null;
//    }
 
    /**
     * 要加密的明文数据
     *
     * @param plainText
     * @return
     */
    public String encryptedData(String plainText, String pub_key) {
        try {
            byte[] publicKeyBytes = Base64.getDecoder().decode(pub_key);
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(keySpec);
 
            // 使用公钥加密数据
            Cipher encryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = encryptCipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
 
            // URL Safe Base64 编码
            return Base64.getUrlEncoder().encodeToString(encryptedBytes);
        } catch (Exception e) {
            log.error("加密失败: {}", e.getMessage());
        }
        return null;
    }
 
    /**
     * 数据解密
     *
     * @param encryptedData
     * @return
     */
    public String decryptedData(String encryptedData, String pri_key) {
        try {
            // 将私钥Base64编码字符串转换为PrivateKey对象
            byte[] privateKeyBytes = Base64.getDecoder().decode(pri_key);
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
 
            // URL Safe Base64 解码
            byte[] cipherBytes = Base64.getUrlDecoder().decode(encryptedData);
 
            // 使用私钥解密数据
            Cipher decryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            decryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedBytes = decryptCipher.doFinal(cipherBytes);
 
            return new String(decryptedBytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.error("解密失败: {}", e.getMessage());
        }
        return null;
    }
 
    public static void main(String[] args) {
        String decodedString = null;
        try {
            decodedString = URLDecoder.decode("0902%E4%BB%BB%E5%8A%A1%E6%B5%8B%E8%AF%95--------", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        System.out.println(decodedString);
    }
 
}