在 Java 中,常用的加密方式包括对称加密、非对称加密、哈希函数等。不同的加密方式适用于不同的安全需求,如数据加密、数字签名、密码存储等。以下是一些常用的加密方式及其 Java 实现示例:
对称加密算法使用相同的密钥进行加密和解密。常见的对称加密算法包括 AES 和 DES。
● 用途:数据加密,支持不同的密钥长度(如128位、192位、256位)。
● Java示例:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESExample {
public static void main(String[] args) throws Exception {
String plaintext = "Hello, AES!";
// 生成密钥
SecretKey key = generateKey();
String keyString = Base64.getEncoder().encodeToString(key.getEncoded());
System.out.println("Key: " + keyString);
// 加密
String ciphertext = encrypt(plaintext, key);
System.out.println("Ciphertext: " + ciphertext);
// 解密
String decryptedText = decrypt(ciphertext, key);
System.out.println("Decrypted Text: " + decryptedText);
}
// 生成AES密钥
private static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128); // 128位密钥
return keyGen.generateKey();
}
// 加密
private static String encrypt(String plaintext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// 解密
private static String decrypt(String ciphertext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(ciphertext);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
}
● 用途:较旧的加密标准,通常推荐使用 AES 替代。
● Java示例:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class DESEncryptionExample {
public static void main(String[] args) throws Exception {
String plaintext = "Hello, DES!";
// 生成密钥
SecretKey key = generateKey();
String keyString = Base64.getEncoder().encodeToString(key.getEncoded());
System.out.println("Key: " + keyString);
// 加密
String ciphertext = encrypt(plaintext, key);
System.out.println("Ciphertext: " + ciphertext);
// 解密
String decryptedText = decrypt(ciphertext, key);
System.out.println("Decrypted Text: " + decryptedText);
}
// 生成DES密钥
private static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56); // 56位密钥
return keyGen.generateKey();
}
// 加密
private static String encrypt(String plaintext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// 解密
private static String decrypt(String ciphertext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(ciphertext);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
}
非对称加密算法使用一对密钥(公钥和私钥)。常见的非对称加密算法包括 RSA 和 ECC。
● 用途:加密和数字签名,通常用于安全通信。
● Java示例:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class RSAExample {
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048);
KeyPair keyPair = keyGen.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
String plaintext = "Hello, RSA!";
// 加密
String ciphertext = encrypt(plaintext, publicKey);
System.out.println("Ciphertext: " + ciphertext);
// 解密
String decryptedText = decrypt(ciphertext, privateKey);
System.out.println("Decrypted Text: " + decryptedText);
}
// 加密
private static String encrypt(String plaintext, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// 解密
private static String decrypt(String ciphertext, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decodedBytes = Base64.getDecoder().decode(ciphertext);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
}
哈希函数用于生成数据的唯一标识符,常用于数据完整性检查和密码存储。
3.1. SHA-256(Secure Hash Algorithm 256-bit)
● 用途:生成数据的哈希值,广泛用于数据完整性校验。
● Java示例:
import java.security.MessageDigest;
public class SHA256Example {
public static void main(String[] args) throws Exception {
String plaintext = "Hello, SHA-256!";
// 计算哈希值
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(plaintext.getBytes());
String hashString = bytesToHex(hashBytes);
System.out.println("SHA-256 Hash: " + hashString);
}
// 辅助方法:将字节数组转换为十六进制字符串
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}