Skip to content

Commit 39c6066

Browse files
committed
弃用三方的 Base64 使用 jdk 的
1 parent facd051 commit 39c6066

3 files changed

Lines changed: 16 additions & 12 deletions

File tree

LOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
## v2.2.4
1212

1313
- 支持使用 OkHttp 发起请求
14+
- 弃用 `com.fengwenyi.javalib.third.Base64`
15+
- RSAUtils 中使用 `com.fengwenyi.javalib.third.Base64` 都进行替换
1416

1517

1618

src/main/java/com/fengwenyi/javalib/encryption/RSAUtils.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.fengwenyi.javalib.encryption;
22

3-
import com.fengwenyi.javalib.constant.CharsetConstant;
4-
import com.fengwenyi.javalib.third.Base64;
53
import com.fengwenyi.javalib.convert.HexUtils;
64
import com.fengwenyi.javalib.util.StringUtils;
75

@@ -10,6 +8,7 @@
108
import javax.crypto.IllegalBlockSizeException;
119
import javax.crypto.NoSuchPaddingException;
1210
import java.io.UnsupportedEncodingException;
11+
import java.nio.charset.StandardCharsets;
1312
import java.security.*;
1413
import java.security.interfaces.RSAPrivateKey;
1514
import java.security.interfaces.RSAPublicKey;
@@ -79,9 +78,9 @@ public static String privateKeyEncrypt(String key, String plainText) throws NoSu
7978
PrivateKey privateKey = commonGetPrivatekeyByText(key);
8079
Cipher cipher = Cipher.getInstance(RSA);
8180
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
82-
byte [] result = cipher.doFinal(plainText.getBytes(CharsetConstant.UTF_8));
81+
byte [] result = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
8382

84-
return Base64.byteArrayToBase64(result);
83+
return Base64Utils.encrypt(result);
8584
}
8685

8786
/**
@@ -102,7 +101,7 @@ public static String publicKeyDecrypt(String key, String cipherText) throws NoSu
102101
PublicKey publicKey = commonGetPublickeyByText(key);
103102
Cipher cipher = Cipher.getInstance(RSA);
104103
cipher.init(Cipher.DECRYPT_MODE, publicKey);
105-
byte[] result = cipher.doFinal(Base64.base64ToByteArray(cipherText));
104+
byte[] result = cipher.doFinal(Base64Utils.decode(cipherText));
106105

107106
return new String(result);
108107
}
@@ -127,9 +126,9 @@ public static String publicKeyEncrypt(String key, String plainText) throws NoSuc
127126
Cipher cipher = Cipher.getInstance(RSA);
128127
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
129128

130-
byte [] result = cipher.doFinal(plainText.getBytes(CharsetConstant.UTF_8));
129+
byte [] result = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
131130

132-
return Base64.byteArrayToBase64(result);
131+
return Base64Utils.encrypt(result);
133132
}
134133

135134
/**
@@ -149,7 +148,7 @@ public static String privateKeyDecrypt(String key, String cipherText) throws NoS
149148
PrivateKey privateKey = commonGetPrivatekeyByText(key);
150149
Cipher cipher = Cipher.getInstance(RSA);
151150
cipher.init(Cipher.DECRYPT_MODE, privateKey);
152-
byte[] result = cipher.doFinal(Base64.base64ToByteArray(cipherText));
151+
byte[] result = cipher.doFinal(Base64Utils.decode(cipherText));
153152

154153
return new String(result);
155154
}
@@ -165,17 +164,17 @@ private static String[] commonKey(int size) throws NoSuchAlgorithmException {
165164
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
166165

167166
// 私钥
168-
keys[0] = Base64.byteArrayToBase64(rsaPrivateKey.getEncoded());
167+
keys[0] = Base64Utils.encrypt(rsaPrivateKey.getEncoded());
169168
// 公钥
170-
keys[1] = Base64.byteArrayToBase64(rsaPublicKey.getEncoded());
169+
keys[1] = Base64Utils.encrypt(rsaPublicKey.getEncoded());
171170

172171
return keys;
173172
}
174173

175174
// (common)通过公钥文本获取公钥
176175
private static PublicKey commonGetPublickeyByText(String keyText)
177176
throws NoSuchAlgorithmException, InvalidKeySpecException {
178-
byte[] keyBytes = Base64.base64ToByteArray(keyText);
177+
byte[] keyBytes = Base64Utils.decode(keyText);
179178
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
180179
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
181180
return keyFactory.generatePublic(x509KeySpec);
@@ -184,7 +183,7 @@ private static PublicKey commonGetPublickeyByText(String keyText)
184183
// (common)通过私钥文本获取私钥
185184
private static PrivateKey commonGetPrivatekeyByText(String keyText)
186185
throws NoSuchAlgorithmException, InvalidKeySpecException {
187-
byte[] keyBytes = Base64.base64ToByteArray(keyText);
186+
byte[] keyBytes = Base64Utils.decode(keyText);
188187
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
189188
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
190189
return keyFactory.generatePrivate(pkcs8EncodedKeySpec);

src/main/java/com/fengwenyi/javalib/third/Base64.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
* @author Josh Bloch
2323
* @version %I%, %G%
2424
* @since 1.4
25+
* @see java.util.Base64
26+
* @deprecated 可以直接使用 {@link java.util.Base64}
2527
*/
28+
@Deprecated
2629
public class Base64 {
2730

2831
/**

0 commit comments

Comments
 (0)