不在类初始化时就生成 Cipher。

feature/security
ZhouXY108 2023-11-16 21:58:00 +08:00
parent 1c4b0631f9
commit 53f31602d2
1 changed files with 20 additions and 31 deletions

View File

@ -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));
}
}