Java 字符串加密解密技术解析
引言
在现代信息社会中,数据的安全性成为一项非常重要的问题。随着互联网的普及,人们在进行数据传输时需要考虑数据的保密性。在这个过程中,加密和解密技术成为了必不可少的手段。本文将重点介绍使用Java语言对字符串进行加密和解密的技术。
加密算法介绍
对称加密算法
对称加密算法是指加密和解密使用同一个密钥的算法。常见的对称加密算法有DES、3DES、AES等。下面是一个使用AES算法对字符串进行加密的示例代码:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
public class AESEncryptor {
public static String encrypt(String plainText) throws Exception {
// 生成密钥
SecretKey secretKey = generateKey();
// 创建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 encryptedText) throws Exception {
// 生成密钥
SecretKey secretKey = generateKey();
// 创建AES解密器
Cipher cipher = Cipher.getInstance("AES");
// 初始化解密器
cipher.init(Cipher.DECRYPT_MODE, secretKey);
// 执行解密操作
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes);
}
private static SecretKey generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
return keyGenerator.generateKey();
}
}
非对称加密算法
非对称加密算法是指加密和解密使用不同密钥的算法。常见的非对称加密算法有RSA、DSA等。下面是一个使用RSA算法对字符串进行加密的示例代码:
import java.security.*;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class RSAEncryptor {
public static String encrypt(String plainText) throws Exception {
// 生成密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
// 获取公钥
PublicKey publicKey = keyPair.getPublic();
// 创建RSA加密器
Cipher cipher = Cipher.getInstance("RSA");
// 初始化加密器
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
// 执行加密操作
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
// 创建RSA解密器
Cipher cipher = Cipher.getInstance("RSA");
// 初始化解密器
cipher.init(Cipher.DECRYPT_MODE, privateKey);
// 执行解密操作
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decryptedBytes);
}
}
加密解密过程示例
以下是一个使用AES加密算法对字符串进行加密和解密的示例代码:
public class Main {
public static void main(String[] args) {
try {
String plainText = "Hello, World!";
String encryptedText = AESEncryptor.encrypt(plainText);
String decryptedText = AESEncryptor.decrypt(encryptedText);
System.out.println("Plain Text: " + plainText);
System.out.println("Encrypted Text: " + encryptedText);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行以上代码,将输出如下结果:
Plain Text: Hello, World!
Encrypted Text: z3oKoVyGHei2Y2rW5oYCSg==
Decrypted Text: Hello, World!
加密解密的应用场景
字符串的加密解密技术在实际应用中有着广泛