public class RSAUtil {
public static final String CHARSET = "utf-8";
public static final String ENCRYPTION_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
/**
* 生成密钥
*/
public static KeyPair getKeyPair(int keyLength) throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(keyLength);
return keyPairGenerator.generateKeyPair();
}
/**
* 获取公钥
*
* @param keyPair 密钥对
* @return
*/
public static String getPublicKey(KeyPair keyPair) throws Exception {
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
return Base64Util.encryptBASE64(publicKey.getEncoded());
}
/**
* 公钥字符串转PublicKey实例
*
* @param publicKey
* @return
* @throws Exception
*/
public static PublicKey getPublicKey(String publicKey) throws Exception {
byte[] publicKeyBytes = Base64Util.decryptBASE64(publicKey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
}
/**
* 获取私钥字符串
*
* @param keyPair
* @return
* @throws Exception
*/
public static String getPrivateKey(KeyPair keyPair) throws Exception {
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
return Base64Util.encryptBASE64(privateKey.getEncoded());
}
/**
* 私钥字符串转PrivateKey实例
*
* @param privateKey
* @return
* @throws Exception
*/
public static PrivateKey getPrivateKey(String privateKey) throws Exception {
byte[] privateKeyBytes = Base64Util.decryptBASE64(privateKey);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePrivate(keySpec);
}
/**
* 公钥解密————RSA
* @param content
* @param publicKey
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(byte[] content, String publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, getPublicKey(publicKey));
return cipher.doFinal(content);
}
/**
* 公钥解密————RSA
* @param content
* @param publicKey
* @return
* @throws Exception
*/
public static String encryptByPublicKey(String content, String publicKey) throws Exception {
return new String(encryptByPublicKey(Base64.getDecoder().decode(content),publicKey),CHARSET);
}
/**
* 私钥解密
* @param content
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] content, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(content);
}
/**
* 私钥解密
*
* @param content
* @return
* @throws Exception
*/
public static String decryptByPrivateKey(String content, String privateKey) throws Exception {
return new String(decryptByPrivateKey(Base64.getDecoder().decode(content), getPrivateKey(privateKey)), CHARSET);
}
/**
* 私钥加密
*
* @param content
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] content, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(content);
}
public static String encryptByPrivateKey(String content, String privateKey) throws Exception {
return new String(encryptByPrivateKey(Base64.getDecoder().decode(content), getPrivateKey(privateKey)), CHARSET);
}
/**
* 公钥解密————AES
*
* @param content
* @param publicKey
* @return
* @throws Exception
*/
public static byte[] decrypByPublicKey(byte[] content, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(content);
}
/**
* 公钥解密————AES
* @param content
* @param publicKey
* @return
* @throws Exception
*/
public static String decrypByPublicKey(String content, String publicKey) throws Exception {
return new String(decrypByPublicKey(Base64.getDecoder().decode(content), getPublicKey(publicKey)), CHARSET);
}
/**
* 签名
* @param content
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] sign(byte[] content, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateKey);
signature.update(content);
return signature.sign();
}
/**
* 签名
*
* @param content
* @return
* @throws Exception
*/
public static String sign(String content,String privateKey) throws Exception {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(getPrivateKey(privateKey));
signature.update(Base64.getDecoder().decode(content));
return new String(signature.sign());
}
/**
* 校验签名
*
* @param content 数据内容
* @param publicKey 公钥
* @param sign 签名
* @return
* @throws Exception
*/
public static boolean verify(byte[] content, String publicKey, String sign)throws Exception {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(getPublicKey(publicKey));
signature.update(content);
// 验证签名是否正常
return signature.verify(Base64.getDecoder().decode(sign));
}
/**
* 校验签名
*
* @param content 数据内容
* @param publicKey 公钥
* @param sign 签名
* @return
* @throws Exception
*/
public static boolean verify(String content, String publicKey, String sign) throws Exception {
return verify(Base64.getDecoder().decode(content), publicKey, sign);
}
public static class Base64Util {
/**
* Base64加密
*/
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* Base64解密
*/
public static byte[] decryptBASE64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
}
}
java使用rsa加密算法进行加密解密
最新推荐文章于 2024-07-25 11:27:30 发布