在Java中,加密方式分为对称加密、非对称加密和哈希算法。下面介绍几种常见的加密方式及其实现:
1. 对称加密 - AESAES(高级加密标准)是目前最常用的对称加密算法,支持128、192和256位密钥长度。
代码语言:java复制import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AESEncryption {
// 生成AES密钥
public static String generateKey(int keySize) throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(keySize);
SecretKey secretKey = keyGen.generateKey();
return Base64.getEncoder().encodeToString(secretKey.getEncoded());
}
// AES加密
public static String encrypt(String plainText, String key) throws Exception {
SecretKey secretKey = getSecretKey(key);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// AES解密
public static String decrypt(String cipherText, String key) throws Exception {
SecretKey secretKey = getSecretKey(key);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
private static SecretKey getSecretKey(String key) {
byte[] keyBytes = Base64.getDecoder().decode(key);
return new javax.crypto.spec.SecretKeySpec(keyBytes, "AES");
}
}2. 非对称加密 - RSARSA使用公钥加密,私钥解密,适合密钥交换和数字签名。
代码语言:java复制import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;
public class RSAEncryption {
// 生成RSA密钥对
public static KeyPair generateKeyPair(int keySize) throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(keySize);
return keyPairGen.generateKeyPair();
}
// RSA公钥加密
public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// RSA私钥解密
public static String decrypt(String cipherText, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}3. 哈希算法 - SHA-256SHA-256用于生成数据的摘要,不可逆,常用于密码存储和数据完整性校验。
代码语言:java复制import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class SHA256Hashing {
public static String hash(String input) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(input.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hashBytes);
}
}4. HMAC - SHA256HMAC结合密钥和哈希算法,用于生成带密钥的消息摘要。
代码语言:java复制import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class HMACExample {
public static String calculateHMAC(String data, String secret) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256_HMAC.init(secretKey);
byte[] hmacBytes = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(hmacBytes);
}
}注意事项密钥管理:对称加密的密钥需要安全传输和存储,非对称加密的私钥必须严格保密。异常处理:实际应用中需要处理NoSuchAlgorithmException、InvalidKeyException等异常。填充方式:加密时需注意填充方式(如"AES/CBC/PKCS5Padding"),不同填充方式可能影响兼容性。盐值:哈希密码时应加入随机盐值,提高安全性。