yangys
2024-03-31 2969df3e404db3cd116f27db1495e523ce05bf86
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.qianwen.core.tool.utils;
 
import java.math.BigInteger;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Objects;
import javax.crypto.Cipher;
import com.qianwen.core.tool.tuple.KeyPair;
import org.springframework.lang.Nullable;
import org.springframework.util.Base64Utils;
 
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/RsaUtil.class */
public class RsaUtil {
    public static final String RSA_ALGORITHM = "RSA";
    public static final String RSA_PADDING = "RSA/ECB/PKCS1Padding";
 
    public static KeyPair genKeyPair() {
        return genKeyPair(1024);
    }
 
    public static KeyPair genKeyPair(int keySize) {
        try {
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(RSA_ALGORITHM);
            keyPairGen.initialize(keySize);
            return new KeyPair(keyPairGen.generateKeyPair());
        } catch (NoSuchAlgorithmException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static PrivateKey generatePrivateKey(String modulus, String exponent) {
        return generatePrivateKey(new BigInteger(modulus), new BigInteger(exponent));
    }
 
    public static PrivateKey generatePrivateKey(BigInteger modulus, BigInteger exponent) {
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
            return keyFactory.generatePrivate(keySpec);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static PublicKey generatePublicKey(String modulus, String exponent) {
        return generatePublicKey(new BigInteger(modulus), new BigInteger(exponent));
    }
 
    public static PublicKey generatePublicKey(BigInteger modulus, BigInteger exponent) {
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, exponent);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
            return keyFactory.generatePublic(keySpec);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static PublicKey getPublicKey(String base64PubKey) {
        Objects.requireNonNull(base64PubKey, "base64 public key is null.");
        byte[] keyBytes = Base64Utils.decodeFromString(base64PubKey);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
            return keyFactory.generatePublic(keySpec);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static String getPublicKeyToBase64(String base64PubKey) {
        PublicKey publicKey = getPublicKey(base64PubKey);
        return getKeyString(publicKey);
    }
 
    public static PrivateKey getPrivateKey(String base64PriKey) {
        Objects.requireNonNull(base64PriKey, "base64 private key is null.");
        byte[] keyBytes = Base64Utils.decodeFromString(base64PriKey);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
            return keyFactory.generatePrivate(keySpec);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static String getKeyString(Key key) {
        return Base64Utils.encodeToString(key.getEncoded());
    }
 
    public static String getPrivateKeyToBase64(String base64PriKey) {
        PrivateKey privateKey = getPrivateKey(base64PriKey);
        return getKeyString(privateKey);
    }
 
    public static byte[] encrypt(String base64PublicKey, byte[] data) {
        return encrypt(getPublicKey(base64PublicKey), data);
    }
 
    public static byte[] encrypt(PublicKey publicKey, byte[] data) {
        return rsa(publicKey, data, 1);
    }
 
    public static byte[] encryptByPrivateKey(String base64PrivateKey, byte[] data) {
        return encryptByPrivateKey(getPrivateKey(base64PrivateKey), data);
    }
 
    public static String encryptByPrivateKeyToBase64(String base64PrivateKey, byte[] data) {
        return Base64Util.encodeToString(encryptByPrivateKey(base64PrivateKey, data));
    }
 
    public static byte[] encryptByPrivateKey(PrivateKey privateKey, byte[] data) {
        return rsa(privateKey, data, 1);
    }
 
    @Nullable
    public static String encryptToBase64(String base64PublicKey, @Nullable String data) {
        if (StringUtil.isBlank(data)) {
            return null;
        }
        return Base64Utils.encodeToString(encrypt(base64PublicKey, data.getBytes(Charsets.UTF_8)));
    }
 
    public static byte[] decrypt(String base64PrivateKey, byte[] data) {
        return decrypt(getPrivateKey(base64PrivateKey), data);
    }
 
    public static byte[] decryptByPublicKey(String base64publicKey, byte[] data) {
        return decryptByPublicKey(getPublicKey(base64publicKey), data);
    }
 
    public static byte[] decrypt(PrivateKey privateKey, byte[] data) {
        return rsa(privateKey, data, 2);
    }
 
    public static byte[] decryptByPublicKey(PublicKey publicKey, byte[] data) {
        return rsa(publicKey, data, 2);
    }
 
    private static byte[] rsa(Key key, byte[] data, int mode) {
        try {
            Cipher cipher = Cipher.getInstance(RSA_PADDING);
            cipher.init(mode, key);
            return cipher.doFinal(data);
        } catch (Exception e) {
            throw Exceptions.unchecked(e);
        }
    }
 
    public static byte[] decryptByPublicKeyFromBase64(String base64PublicKey, byte[] base64Data) {
        return decryptByPublicKey(getPublicKey(base64PublicKey), base64Data);
    }
 
    @Nullable
    public static String decryptFromBase64(String base64PrivateKey, @Nullable String base64Data) {
        if (StringUtil.isBlank(base64Data)) {
            return null;
        }
        return new String(decrypt(base64PrivateKey, Base64Utils.decodeFromString(base64Data)), Charsets.UTF_8);
    }
 
    public static byte[] decryptFromBase64(String base64PrivateKey, byte[] base64Data) {
        return decrypt(base64PrivateKey, Base64Utils.decode(base64Data));
    }
 
    @Nullable
    public static String decryptByPublicKeyFromBase64(String base64PublicKey, @Nullable String base64Data) {
        if (StringUtil.isBlank(base64Data)) {
            return null;
        }
        return new String(decryptByPublicKeyFromBase64(base64PublicKey, Base64Utils.decodeFromString(base64Data)), Charsets.UTF_8);
    }
}