下面是一个尽可能全面的 Java 加密解密工具类的示例,包括对称加密、非对称加密、哈希算法、消息摘要和数字签名等功能。
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;
/**
* @Author 果酱 Ken
* 加密解密工具类,包含对称加密、非对称加密、哈希算法、消息摘要和数字签名等功能。
*/
public class EncryptionUtils {
private static final String AES_ALGORITHM = "AES";
private static final String RSA_ALGORITHM = "RSA";
private static final String HASH_ALGORITHM = "SHA-256";
private static final String SIGNATURE_ALGORITHM = "SHA256withRSA";
/**
* 使用 AES 算法对文本进行加密
*
* @param plaintext 明文
* @param secretKey 密钥
* @return 加密后的文本
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
* @throws NoSuchPaddingException 当请求的填充机制不可用时抛出此异常
* @throws InvalidKeyException 当密钥无效时抛出此异常
* @throws IllegalBlockSizeException 当块大小无效时抛出此异常
* @throws BadPaddingException 当填充机制异常时抛出此异常
*/
public static String encryptAES(String plaintext, String secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), AES_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
/**
* 使用 AES 算法对文本进行解密
*
* @param encryptedText 加密后的文本
* @param secretKey 密钥
* @return 解密后的明文
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
* @throws NoSuchPaddingException 当请求的填充机制不可用时抛出此异常
* @throws InvalidKeyException 当密钥无效时抛出此异常
* @throws IllegalBlockSizeException 当块大小无效时抛出此异常
* @throws BadPaddingException 当填充机制异常时抛出此异常
*/
public static String decryptAES(String encryptedText, String secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), AES_ALGORITHM);
Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
/**
* 生成 RSA 密钥对
*
* @return 生成的密钥对
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
*/
public static KeyPair generateRSAKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM);
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
/**
* 使用 RSA 公钥对文本进行加密
*
* @param plaintext 明文
* @param publicKey 公钥
* @return 加密后的文本
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
* @throws NoSuchPaddingException 当请求的填充机制不可用时抛出此异常
* @throws InvalidKeyException 当密钥无效时抛出此异常
* @throws IllegalBlockSizeException 当块大小无效时抛出此异常
* @throws BadPaddingException 当填充机制异常时抛出此异常
*/
public static String encryptRSA(String plaintext, PublicKey publicKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
/**
* 使用 RSA 私钥对文本进行解密
*
* @param encryptedText 加密后的文本
* @param privateKey 私钥
* @return 解密后的明文
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
* @throws NoSuchPaddingException 当请求的填充机制不可用时抛出此异常
* @throws InvalidKeyException 当密钥无效时抛出此异常
* @throws IllegalBlockSizeException 当块大小无效时抛出此异常
* @throws BadPaddingException 当填充机制异常时抛出此异常
*/
public static String decryptRSA(String encryptedText, PrivateKey privateKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
/**
* 使用 SHA-256 算法对文本进行哈希
*
* @param plaintext 明文
* @return 哈希后的文本
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
*/
public static String hashSHA256(String plaintext) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
byte[] hashBytes = digest.digest(plaintext.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hashBytes);
}
/**
* 使用 SHA-256 算法对文本进行消息摘要
*
* @param plaintext 明文
* @return 消息摘要
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
*/
public static byte[] digestSHA256(String plaintext) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance(HASH_ALGORITHM);
return digest.digest(plaintext.getBytes(StandardCharsets.UTF_8));
}
/**
* 使用私钥对数据进行签名
*
* @param data 要签名的数据
* @param privateKey 私钥
* @return 签名值
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
* @throws InvalidKeyException 当密钥无效时抛出此异常
* @throws SignatureException 当签名过程中出现错误时抛出此异常
*/
public static byte[] signSHA256withRSA(byte[] data, PrivateKey privateKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
/**
* 使用公钥验证签名
*
* @param data 被签名的数据
* @param signatureBytes 签名值
* @param publicKey 公钥
* @return 验证结果,true 表示验证通过,false 表示验证失败
* @throws NoSuchAlgorithmException 当算法不可用时抛出此异常
* @throws InvalidKeyException 当密钥无效时抛出此异常
* @throws SignatureException 当签名过程中出现错误时抛出此异常
*/
public static boolean verifySHA256withRSA(byte[] data, byte[] signatureBytes, PublicKey publicKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(signatureBytes);
}
}
这个工具类包含了常见的加密和解密功能,使用了 Java 8 提供的加密相关类库。
请注意,这只是一个示例工具类,实际应用中可能需要根据具体需求进行适当的修改和扩展。
👉 💐🌸 公众号请关注 "果酱桑", 一起学习,一起进步! 🌸💐