java 如何加密
加密方法一:使用Java内置的MessageDigest类进行哈希加密
哈希加密适用于密码存储等场景,常见算法包括MD5、SHA-1、SHA-256等。以下示例展示SHA-256加密:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashExample {
public static String encryptSHA256(String input) throws NoSuchAlgorithmException {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
}
}
加密方法二:使用对称加密(AES)
AES加密需要密钥,适用于数据加密传输或存储。以下是AES加密和解密的示例:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
public class AESExample {
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256); // 密钥长度
return keyGenerator.generateKey();
}
public static String encrypt(String input, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedInput, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedInput);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
}
加密方法三:使用非对称加密(RSA)
RSA适用于密钥交换或数字签名。以下是生成密钥对及加密解密的示例:
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.util.Base64;
public class RSAExample {
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); // 密钥长度
return keyPairGenerator.generateKeyPair();
}
public static String encrypt(String input, java.security.PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(input.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedInput, java.security.PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedInput);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
}
加密方法四:使用BCrypt进行密码哈希
BCrypt适合密码存储,自动加盐且抗彩虹表攻击。需引入依赖(如org.mindrot:jbcrypt):
import org.mindrot.jbcrypt.BCrypt;
public class BCryptExample {
public static String hashPassword(String plainTextPassword) {
return BCrypt.hashpw(plainTextPassword, BCrypt.gensalt());
}
public static boolean checkPassword(String plainTextPassword, String hashedPassword) {
return BCrypt.checkpw(plainTextPassword, hashedPassword);
}
}
注意事项
- 哈希加密:MD5和SHA-1已不推荐用于安全场景,优先选择SHA-256或BCrypt。
- 密钥管理:对称加密的密钥需安全存储,非对称加密私钥严禁泄露。
- 算法选择:根据场景选择加密方式,如传输用RSA+AES组合,存储用BCrypt。
以上方法覆盖了Java中常见的加密需求,实际应用中需结合具体场景调整参数(如密钥长度、填充模式等)。






