MD5 (Message-Digest Algorithm 5)
SHA-256 (Secure Hash Algorithm 256-bit)
AES (Advanced Encryption Standard)
DES (Data Encryption Standard)
RSA (Rivest-Shamir-Adleman)
BCrypt
适用范围
根据具体需求选择合适的加密方式,如对密码存储使用BCrypt,对数据加密使用AES,进行数据完整性校验使用SHA-256,不同的加密方式在其使用目的、算法复杂度、安全性和应用场景上有所区别
使用java.security
包中的MessageDigest
进行MD5加密
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
public static String md5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : messageDigest) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String original = "password";
String hashed = md5(original);
System.out.println("Original: " + original);
System.out.println("MD5 Hashed: " + hashed);
}
}
使用javax.crypto
包进行AES加密
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtil {
private static final String ALGORITHM = "AES";
public static String encrypt(String data, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
public static String decrypt(String encryptedData, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decoded = Base64.getDecoder().decode(encryptedData);
byte[] original = cipher.doFinal(decoded);
return new String(original);
}
public static void main(String[] args) {
try {
String key = "1234567890123456"; // 16-byte key
String original = "password";
String encrypted = encrypt(original, key);
String decrypted = decrypt(encrypted, key);
System.out.println("Original: " + original);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用BCrypt
进行密码加密
import org.mindrot.jbcrypt.BCrypt;
public class BCryptUtil {
public static String hashPassword(String password) {
return BCrypt.hashpw(password, BCrypt.gensalt());
}
public static boolean checkPassword(String password, String hashed) {
return BCrypt.checkpw(password, hashed);
}
public static void main(String[] args) {
String original = "password";
String hashed = hashPassword(original);
System.out.println("Original: " + original);
System.out.println("BCrypt Hashed: " + hashed);
System.out.println("Password matches: " + checkPassword(original, hashed));
}
}
使用javax.crypto
包进行RSA加密
import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class RSAUtil {
private static final String ALGORITHM = "RSA";
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
keyGen.initialize(2048);
return keyGen.generateKeyPair();
}
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encrypted = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
public static String decrypt(String encryptedData, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decoded = Base64.getDecoder().decode(encryptedData);
byte[] original = cipher.doFinal(decoded);
return new String(original);
}
public static void main(String[] args) {
try {
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String original = "password";
String encrypted = encrypt(original, publicKey);
String decrypted = decrypt(encrypted, privateKey);
System.out.println("Original: " + original);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用java.security
包中的MessageDigest
进行SHA-256加密
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA256Util {
public static String sha256(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedhash = digest.digest(input.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : encodedhash) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String original = "password";
String hashed = sha256(original);
System.out.println("Original: " + original);
System.out.println("SHA-256 Hashed: " + hashed);
}
}
使用javax.crypto
包进行DES加密
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class DESUtil {
private static final String ALGORITHM = "DES";
public static String encrypt(String data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
public static String decrypt(String encryptedData, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decoded = Base64.getDecoder().decode(encryptedData);
byte[] original = cipher.doFinal(decoded);
return new String(original);
}
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(56); // DES requires a key size of 56 bits
return keyGen.generateKey();
}
public static void main(String[] args) {
try {
SecretKey key = generateKey();
String original = "password";
String encrypted = encrypt(original, key);
String decrypted = decrypt(encrypted, key);
System.out.println("Original: " + original);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
}