Java中如何实现加密和解密字符串?
发布时间:2023-07-04 03:05:01
在Java中,可以使用各种加密算法来实现字符串的加密和解密。下面将介绍两种常用的加密算法:对称加密和非对称加密。
1. 对称加密:
对称加密算法使用相同的密钥对数据进行加密和解密。常用的对称加密算法有DES、AES和DESede等。下面是使用AES对称加密算法来实现字符串的加密和解密的示例代码:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class SymmetricEncryptionExample {
public static void main(String[] args) throws Exception {
String message = "Hello World!";
String secretKey = "MySecretKey";
// 加密
String encryptedMessage = encrypt(message, secretKey);
System.out.println("Encrypted Message: " + encryptedMessage);
// 解密
String decryptedMessage = decrypt(encryptedMessage, secretKey);
System.out.println("Decrypted Message: " + decryptedMessage);
}
public static String encrypt(String message, String secretKey) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedMessage, String secretKey) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedMessage));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}
2. 非对称加密:
非对称加密算法使用一对密钥,其中一个密钥用于加密数据,另一个密钥用于解密数据。常用的非对称加密算法有RSA和DSA等。下面是使用RSA非对称加密算法来实现字符串的加密和解密的示例代码:
import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
public class AsymmetricEncryptionExample {
public static void main(String[] args) throws Exception {
String message = "Hello World!";
// 生成密钥对
KeyPair keyPair = generateKeyPair();
// 加密
String encryptedMessage = encrypt(message, keyPair.getPublic());
System.out.println("Encrypted Message: " + encryptedMessage);
// 解密
String decryptedMessage = decrypt(encryptedMessage, keyPair.getPrivate());
System.out.println("Decrypted Message: " + decryptedMessage);
}
public static KeyPair generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
return keyPairGenerator.generateKeyPair();
}
public static String encrypt(String message, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedMessage, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedMessage));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}
通过使用对称加密和非对称加密算法,可以实现字符串的加密和解密,在保护数据的安全性方面发挥重要作用。
