liusheng
2024-09-04 9526971c403417c1c007804f24884c443b9e6cd7
ruoyi-common/src/main/java/com/ruoyi/common/utils/RSAPublicKeyExample.java
@@ -4,6 +4,10 @@
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;
@@ -54,8 +58,7 @@
     * @return
     */
    public String encryptedData(String plainText, String pub_key) {
        String publicKeyString = "公钥的Base64编码字符串"; // 前端传递的公钥字符串
        // 将公钥Base64编码字符串转换为PublicKey对象
        log.error("需要加密的数据:{}", plainText);
        try {
            byte[] publicKeyBytes = Base64.getDecoder().decode(pub_key);
@@ -70,7 +73,11 @@
            // 将加密后的数据转换为Base64编码的字符串
            String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
            System.out.println("加密后的数据:" + encryptedText);
            log.error("Base64加密后的数据:{}", encryptedText);
            encryptedText = URLEncoder.encode(encryptedText, StandardCharsets.UTF_8.toString());
            log.error("URLEncoder编码后的数据:{}", encryptedText);
            String decodedString = URLDecoder.decode(encryptedText, "UTF-8");
            log.error("URLEncoder解码后的数据:{}", decodedString);
            return encryptedText;
        } catch (Exception e) {
            log.error("加密失败了:{}", e.getMessage());
@@ -78,5 +85,14 @@
        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);
    }
}