对称加密算法(Symmetric Encryption):
AES(Advanced Encryption Standard)加密算法:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AesEncryptionExample {
public static String encrypt(String plaintext, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
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 ciphertext, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decodedBytes = Base64.getDecoder().decode(ciphertext);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) throws Exception {
String plaintext = "Hello, World!";
String key = "ThisIsASecretKey";
String encryptedText = encrypt(plaintext, key);
System.out.println("Encrypted: " + encryptedText);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Decrypted: " + decryptedText);
}
}
非对称加密算法(Asymmetric Encryption):
RSA(Rivest-Shamir-Adleman)加密算法:
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
public class RsaEncryptionExample {
public static byte[] encrypt(byte[] plaintext, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(plaintext);
}
public static byte[] decrypt(byte[] ciphertext, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(ciphertext);
}
public static byte[] sign(byte[] data, PrivateKey privateKey) throws Exception {
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
public static boolean verifySignature(byte[] data, byte[] signature, PublicKey publicKey) throws Exception {
Signature verifySignature = Signature.getInstance("SHA256withRSA");
verifySignature.initVerify(publicKey);
verifySignature.update(data);
return verifySignature.verify(signature);
}
public static void main(String[] args) throws Exception {
String plaintext = "Hello, World!";
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
byte[] encryptedBytes = encrypt(plaintext.getBytes(StandardCharsets.UTF_8), keyPair.getPublic());
byte[] decryptedBytes = decrypt(encryptedBytes, keyPair.getPrivate());
String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
System.out.println("Decrypted: " + decryptedText);
byte[] signature = sign(plaintext.getBytes(StandardCharsets.UTF_8), keyPair.getPrivate());
boolean verified = verifySignature(plaintext.getBytes(StandardCharsets.UTF_8), signature, keyPair.getPublic());
System.out.println("Signature verified: " + verified);
}
}
在加密算法中,“加盐”(Salting)是指在加密过程中添加一个随机的值(盐值)到要加密的数据中,以增加密码的复杂性和安全性。盐值是一个随机生成的字符串或字节数组,它与要加密的数据结合使用。
使用盐值进行加密的过程如下:
生成随机的盐值,通常具有足够的长度和复杂性。
将盐值与要加密的数据进行组合,创建一个更长的数据字符串。
使用加密算法对组合后的数据进行加密,生成最终的加密结果。
通过使用盐值,每次加密相同的数据都会产生不同的加密结果,即使原始数据相同。这增加了破解密码的难度,因为攻击者无法仅仅通过比对加密结果来猜测原始数据。
在解密过程中,也需要使用相同的盐值来还原正确的原始数据。
盐值可以存储在数据库中或与加密结果一起存储,以便在解密时使用。遵循良好的实践,应该为每个加密数据生成独特的盐值。
使用加盐可以有效提高密码的安全性,尤其是在存储用户密码和敏感数据时。