Skip to content

Commit

Permalink
弃用三方的 Base64 使用 jdk 的
Browse files Browse the repository at this point in the history
fengwenyi committed Feb 17, 2023
1 parent facd051 commit 39c6066
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 2 additions & 0 deletions LOG.md
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@
## v2.2.4

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



23 changes: 11 additions & 12 deletions src/main/java/com/fengwenyi/javalib/encryption/RSAUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.fengwenyi.javalib.encryption;

import com.fengwenyi.javalib.constant.CharsetConstant;
import com.fengwenyi.javalib.third.Base64;
import com.fengwenyi.javalib.convert.HexUtils;
import com.fengwenyi.javalib.util.StringUtils;

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

return Base64.byteArrayToBase64(result);
return Base64Utils.encrypt(result);
}

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

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

byte [] result = cipher.doFinal(plainText.getBytes(CharsetConstant.UTF_8));
byte [] result = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));

return Base64.byteArrayToBase64(result);
return Base64Utils.encrypt(result);
}

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

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

// 私钥
keys[0] = Base64.byteArrayToBase64(rsaPrivateKey.getEncoded());
keys[0] = Base64Utils.encrypt(rsaPrivateKey.getEncoded());
// 公钥
keys[1] = Base64.byteArrayToBase64(rsaPublicKey.getEncoded());
keys[1] = Base64Utils.encrypt(rsaPublicKey.getEncoded());

return keys;
}

// (common)通过公钥文本获取公钥
private static PublicKey commonGetPublickeyByText(String keyText)
throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] keyBytes = Base64.base64ToByteArray(keyText);
byte[] keyBytes = Base64Utils.decode(keyText);
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
return keyFactory.generatePublic(x509KeySpec);
@@ -184,7 +183,7 @@ private static PublicKey commonGetPublickeyByText(String keyText)
// (common)通过私钥文本获取私钥
private static PrivateKey commonGetPrivatekeyByText(String keyText)
throws NoSuchAlgorithmException, InvalidKeySpecException {
byte[] keyBytes = Base64.base64ToByteArray(keyText);
byte[] keyBytes = Base64Utils.decode(keyText);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
return keyFactory.generatePrivate(pkcs8EncodedKeySpec);
3 changes: 3 additions & 0 deletions src/main/java/com/fengwenyi/javalib/third/Base64.java
Original file line number Diff line number Diff line change
@@ -22,7 +22,10 @@
* @author Josh Bloch
* @version %I%, %G%
* @since 1.4
* @see java.util.Base64
* @deprecated 可以直接使用 {@link java.util.Base64}
*/
@Deprecated
public class Base64 {

/**

0 comments on commit 39c6066

Please sign in to comment.