liujie
22 小时以前 34e64f1437ed897056bed0c3851e7a46ac220423
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
package com.ruoyi.system.utils.util;
 
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
 
/**
 * rsa工具类
 *
 * @author xuwenbing
 * @date 2019-06-12
 */
public class RsaSimpleUtil {
    private static final String RSA = "RSA";
    private static final String MD5withRSA = "MD5withRSA";
 
    /**
     * 加密
     *
     * @param source
     * @param publicKeyBase64
     * @return
     */
    public static String encrypt(String source, String publicKeyBase64) throws Exception {
        // 获取publicKey
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyBase64.getBytes(StandardCharsets.UTF_8));
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA);
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        // 数据加密
        Cipher cipher = Cipher.getInstance(RSA);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptBytes = cipher.doFinal(source.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptBytes);
    }
 
    /**
     * 解密
     *
     * @param encrypted
     * @param privateKeyBase64
     * @return
     */
    public static String decrypt(String encrypted, String privateKeyBase64) throws Exception {
        // 获取privateKey
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyBase64.getBytes(StandardCharsets.UTF_8));
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA);
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        // 解密数据
        Cipher cipher = Cipher.getInstance(RSA);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptBytes = cipher.doFinal(Base64.getDecoder().decode(encrypted));
        return new String(decryptBytes, StandardCharsets.UTF_8);
    }
 
    /**
     * 签名
     *
     * @param source
     * @param privateKeyBase64
     * @return
     */
    public static String sign(String source, String privateKeyBase64) throws Exception {
        // 获取privateKey
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyBase64.getBytes(StandardCharsets.UTF_8));
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA);
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        // 签名
        Signature signature = Signature.getInstance(MD5withRSA);
        signature.initSign(privateKey);
        signature.update(source.getBytes(StandardCharsets.UTF_8));
        byte[] signed = signature.sign();
        return Base64.getEncoder().encodeToString(signed);
    }
 
    /**
     * 验签
     *
     * @param source
     * @param signed
     * @param publicKeyBase64
     * @return
     */
    public static boolean verify(String source, String signed, String publicKeyBase64) throws Exception {
        // 获取publicKey
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyBase64.getBytes(StandardCharsets.UTF_8));
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(RSA);
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        // 验签
        Signature signature = Signature.getInstance(MD5withRSA);
        signature.initVerify(publicKey);
        signature.update(source.getBytes(StandardCharsets.UTF_8));
        boolean verify = signature.verify(Base64.getDecoder().decode(signed));
        return verify;
    }
}