From 53f31602d24ef46a2661a2bd96221b532ce83158 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 16 Nov 2023 21:58:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8D=E5=9C=A8=E7=B1=BB=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=97=B6=E5=B0=B1=E7=94=9F=E6=88=90=20Cipher=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/security/RSA.java | 51 ++++++++----------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/security/RSA.java b/src/main/java/xyz/zhouxy/plusone/commons/security/RSA.java index 98944da..44bd3c9 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/security/RSA.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/security/RSA.java @@ -1,17 +1,15 @@ package xyz.zhouxy.plusone.commons.security; +import java.math.BigInteger; import java.nio.charset.StandardCharsets; -import java.security.InvalidKeyException; +import java.security.GeneralSecurityException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; -import javax.crypto.BadPaddingException; import javax.crypto.Cipher; -import javax.crypto.IllegalBlockSizeException; -import javax.crypto.NoSuchPaddingException; import com.google.common.annotations.Beta; @@ -26,33 +24,20 @@ public class RSA { private final PublicKey publicKey; private final PrivateKey privateKey; - private final Cipher encryptModeCipher; - private final Cipher decryptModeCipher; - private RSA(final PublicKey publicKey, final PrivateKey privateKey) - throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { + private RSA(final PublicKey publicKey, final PrivateKey privateKey) { this.publicKey = publicKey; this.privateKey = privateKey; - this.encryptModeCipher = Cipher.getInstance(TRANSFORMATION); - encryptModeCipher.init(Cipher.ENCRYPT_MODE, publicKey); - this.decryptModeCipher = Cipher.getInstance(TRANSFORMATION); - decryptModeCipher.init(Cipher.DECRYPT_MODE, privateKey); } public static RSA of(final PublicKey publicKey, final PrivateKey privateKey) { - try { - return new RSA(publicKey, privateKey); - } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { - throw new SecurityException(e); - } + return new RSA(publicKey, privateKey); + } public static RSA of(final KeyPair keyPair) { - try { - return new RSA(keyPair.getPublic(), keyPair.getPrivate()); - } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { - throw new SecurityException(e); - } + return new RSA(keyPair.getPublic(), keyPair.getPrivate()); + } public static RSA withKeySize(int keySize) { @@ -61,7 +46,7 @@ public class RSA { keyPairGenerator.initialize(keySize); final KeyPair keyPair = keyPairGenerator.generateKeyPair(); return new RSA(keyPair.getPublic(), keyPair.getPrivate()); - } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) { + } catch (NoSuchAlgorithmException e) { throw new SecurityException(e); } } @@ -78,27 +63,31 @@ public class RSA { return publicKey; } - public byte[] encrypt(byte[] input) throws IllegalBlockSizeException, BadPaddingException { + public byte[] encrypt(byte[] input) throws GeneralSecurityException { + Cipher encryptModeCipher = Cipher.getInstance(TRANSFORMATION); + encryptModeCipher.init(Cipher.ENCRYPT_MODE, publicKey); return encryptModeCipher.doFinal(input); } - public byte[] encrypt(String input) throws IllegalBlockSizeException, BadPaddingException { + public byte[] encrypt(String input) throws GeneralSecurityException { return encrypt(input.getBytes(StandardCharsets.UTF_8)); } - public String encryptToString(String input) throws IllegalBlockSizeException, BadPaddingException { - return new String(encrypt(input)); + public String encryptToString(String input) throws GeneralSecurityException { + return new BigInteger(1, encrypt(input)).toString(16); } - public byte[] decrypt(byte[] input) throws IllegalBlockSizeException, BadPaddingException { + public byte[] decrypt(byte[] input) throws GeneralSecurityException { + Cipher decryptModeCipher = Cipher.getInstance(TRANSFORMATION); + decryptModeCipher.init(Cipher.DECRYPT_MODE, privateKey); return decryptModeCipher.doFinal(input); } - public byte[] decrypt(String input) throws IllegalBlockSizeException, BadPaddingException { - return decryptModeCipher.doFinal(input.getBytes(StandardCharsets.UTF_8)); + public byte[] decrypt(String input) throws GeneralSecurityException { + return decrypt(new BigInteger(input, 16).toByteArray()); } - public String decryptToString(String input) throws IllegalBlockSizeException, BadPaddingException { + public String decryptToString(String input) throws GeneralSecurityException { return new String(decrypt(input)); } }