这是我的网站刚开始写的工具类,用rsa加密
package com.kongjs.diary.utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.EncodedKeySpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RsaCryptoUtils {
private static final Log logger = LogFactory.getLog(RsaCryptoUtils.class);
private static byte[] publicKey;
private static byte[] privateKey;
private static byte[] slatKey;
private RsaCryptoUtils() { }
public static void init(byte[] slatKey){
init(2048, slatKey);
}
public static void init(CharSequence slatKey) {
init(2048, slatKey.toString().getBytes(StandardCharsets.UTF_8));
}
// 获取公钥私钥
public static void init(int keySize, byte[] slatKey) {
KeyPairGenerator keyPairGenerator;
SecureRandom random;
KeyPair keyPair;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(slatKey);
keyPairGenerator.initialize(keySize, random);// or 2048
keyPair = keyPairGenerator.generateKeyPair(); // 生成密钥对
PrivateKey aPrivate = keyPair.getPrivate();
PublicKey aPublic = keyPair.getPublic();
publicKey = aPublic.getEncoded();
privateKey = aPrivate.getEncoded();
logger.info("RSA 获取密钥对");
} catch (NoSuchAlgorithmException | NullPointerException e) {
logger.error("RSA 获取密钥对失败");
}
}
public static String getStrPublicKey() {
logger.info("RSA 公钥 base64字符串");
return Base64.getEncoder().encodeToString(publicKey);
}
public static String getStrPrivateKey() {
logger.info("RSA 私钥 base64字符串");
return Base64.getEncoder().encodeToString(privateKey);
}
public static byte[] getPublicKey() {
logger.info("RSA 公钥");
return publicKey;
}
public static byte[] getPrivateKey() {
logger.info("RSA 私钥");
return privateKey;
}
public static String publicEncrypt(byte[] data, byte[] publicKey) {
EncodedKeySpec keySpec;
KeyFactory keyFactory;
Cipher cipher;
PublicKey key;
byte[] bytes;
String result = null;
try {
keySpec = new X509EncodedKeySpec(publicKey);
keyFactory = KeyFactory.getInstance("RSA");
cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
key = keyFactory.generatePublic(keySpec);
cipher.init(Cipher.ENCRYPT_MODE, key);
bytes = cipher.doFinal(data);
result = Base64.getEncoder().encodeToString(bytes);
logger.info("RSA 公钥加密");
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
logger.error("RSA 公钥加密失败", e);
}
return result;
}
public static String publicEncrypt(String data, String publicKey) {
return publicEncrypt(data.getBytes(StandardCharsets.UTF_8), Base64.getDecoder().decode(publicKey));
}
public static String publicEncrypt(String data){
return publicEncrypt(Base64.getDecoder().decode(data),publicKey);
}
public static String privateDecrypt(byte[] data, byte[] privateKey) {
PKCS8EncodedKeySpec keySpec;
KeyFactory keyFactory;
Cipher cipher;
PrivateKey key;
byte[] bytes;
String result = null;
try {
keySpec = new PKCS8EncodedKeySpec(privateKey);
keyFactory = KeyFactory.getInstance("RSA");
cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING");
key = keyFactory.generatePrivate(keySpec);
cipher.init(Cipher.DECRYPT_MODE, key);
bytes = cipher.doFinal(data);
result = new String(bytes);
logger.info("RSA 私钥解密");
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeySpecException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
logger.error("RSA 私钥解密失败", e);
}
return result;
}
public static String privateDecrypt(String data, String privateKey) {
return privateDecrypt(Base64.getDecoder().decode(data), Base64.getDecoder().decode(privateKey));
}
public static String privateDecrypt(String data) {
return privateDecrypt(Base64.getDecoder().decode(data), privateKey);
}
public static byte[] getSlatKey() {
return slatKey;
}
public static void setSlatKey(byte[] slatKey) {
RsaCryptoUtils.slatKey = slatKey;
}
public static String getStrSlatKey() {
return Base64.getEncoder().encodeToString(slatKey);
}
public static void setStrSlatKey(String slatKey) {
RsaCryptoUtils.slatKey = slatKey.getBytes(StandardCharsets.UTF_8);
}
}