java如何加密
Java加密方法
Java提供了多种加密方式,包括对称加密、非对称加密和哈希算法。以下是常见的加密实现方法。
对称加密(AES)
对称加密使用相同的密钥进行加密和解密。AES(Advanced Encryption Standard)是常用的对称加密算法。

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
public class AESEncryption {
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256); // 密钥长度
return keyGenerator.generateKey();
}
public static String encrypt(String plainText, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes);
}
}
非对称加密(RSA)
非对称加密使用公钥和私钥。RSA是常见的非对称加密算法。

import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.Base64;
public class RSAEncryption {
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); // 密钥长度
return keyPairGenerator.generateKeyPair();
}
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());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes);
}
}
哈希算法(SHA-256)
哈希算法用于生成数据的唯一摘要,常用于密码存储。
import java.security.MessageDigest;
import java.util.Base64;
public class HashAlgorithm {
public static String hash(String input) throws Exception {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(input.getBytes());
return Base64.getEncoder().encodeToString(hashBytes);
}
}
使用Bouncy Castle库
如果需要更高级的加密功能,可以使用Bouncy Castle库。
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.Security;
public class BouncyCastleExample {
static {
Security.addProvider(new BouncyCastleProvider());
}
public static void encryptWithBouncyCastle() throws Exception {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
// 其他操作与标准库类似
}
}
加密最佳实践
- 密钥管理:密钥应存储在安全的地方,如密钥管理系统(KMS)。
- 盐值:在哈希算法中使用随机盐值增加安全性。
- 算法选择:根据需求选择合适的加密算法,如AES用于对称加密,RSA用于非对称加密。
以上方法涵盖了Java中常见的加密需求,可以根据具体场景选择适合的加密方式。






