封装 RSA

feature/security
ZhouXY108 2023-11-15 23:02:31 +08:00
parent cc10f11b37
commit 1976f7d828
1 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,104 @@
package xyz.zhouxy.plusone.commons.security;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
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;
/**
* RSA
*/
@Beta
public class RSA {
private static final int DEFAULT_KEY_SIZE = 2048;
public static final String NAME = "RSA";
private static final String TRANSFORMATION = "RSA/ECB/OAEPWithSHA-256AndMGF1Padding";
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 {
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);
}
}
public static RSA of(final KeyPair keyPair) {
try {
return new RSA(keyPair.getPublic(), keyPair.getPrivate());
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
throw new SecurityException(e);
}
}
public static RSA withKeySize(int keySize) {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(NAME);
keyPairGenerator.initialize(keySize);
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
return new RSA(keyPair.getPublic(), keyPair.getPrivate());
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
throw new SecurityException(e);
}
}
public static RSA withDefaultKeySize() {
return withKeySize(DEFAULT_KEY_SIZE);
}
public PrivateKey getPrivateKey() {
return privateKey;
}
public PublicKey getPublicKey() {
return publicKey;
}
public byte[] encrypt(byte[] input) throws IllegalBlockSizeException, BadPaddingException {
return encryptModeCipher.doFinal(input);
}
public byte[] encrypt(String input) throws IllegalBlockSizeException, BadPaddingException {
return encrypt(input.getBytes(StandardCharsets.UTF_8));
}
public String encryptToString(String input) throws IllegalBlockSizeException, BadPaddingException {
return new String(encrypt(input));
}
public byte[] decrypt(byte[] input) throws IllegalBlockSizeException, BadPaddingException {
return decryptModeCipher.doFinal(input);
}
public byte[] decrypt(String input) throws IllegalBlockSizeException, BadPaddingException {
return decryptModeCipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
}
public String decryptToString(String input) throws IllegalBlockSizeException, BadPaddingException {
return new String(decrypt(input));
}
}