如何使用Java函数对字符串进行加密与解密
发布时间:2023-07-04 20:47:04
在Java中,可以使用不同的加密算法对字符串进行加密和解密。常见的加密算法包括对称加密和非对称加密。
对称加密算法使用相同的密钥进行加密和解密,加密速度快,适合加密大文件或者数据量较大的情况。常见的对称加密算法有DES、AES等。
非对称加密算法使用一对密钥(公钥和私钥),公钥用于加密,私钥用于解密。相对于对称加密,非对称加密算法更安全,但加密解密性能较低。常见的非对称加密算法有RSA、DSA等。
下面是使用Java函数对字符串进行加密和解密的示例代码:
1. 对称加密示例
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
public class SymmetricEncryption {
private static final String ALGORITHM = "AES";
private static final String ENCODING = "UTF-8";
public static String encrypt(String plaintext, String key) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes(ENCODING));
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes(ENCODING));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext, String key) throws Exception {
byte[] encryptedBytes = Base64.getDecoder().decode(ciphertext);
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes(ENCODING));
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes, ENCODING);
}
public static void main(String[] args) throws Exception {
String plaintext = "Hello World!";
String key = "1234567890abcdef";
String ciphertext = encrypt(plaintext, key);
System.out.println("Cipher text: " + ciphertext);
String decryptedText = decrypt(ciphertext, key);
System.out.println("Decrypted text: " + decryptedText);
}
}
在上述代码中,我们使用AES算法进行加密和解密。首先生成一个随机密钥,然后使用该密钥进行加密和解密。加密后的密文使用Base64编码进行转换和存储。
2. 非对称加密示例
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;
public class AsymmetricEncryption {
private static final String ALGORITHM = "RSA";
public static String encrypt(String plaintext, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
return java.util.Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext, PrivateKey privateKey) throws Exception {
byte[] encryptedBytes = java.util.Base64.getDecoder().decode(ciphertext);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) throws Exception {
String plaintext = "Hello World!";
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(2048);
KeyPair keypair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keypair.getPublic();
PrivateKey privateKey = keypair.getPrivate();
String ciphertext = encrypt(plaintext, publicKey);
System.out.println("Cipher text: " + ciphertext);
String decryptedText = decrypt(ciphertext, privateKey);
System.out.println("Decrypted text: " + decryptedText);
}
}
在上述代码中,我们使用RSA算法生成一对密钥(公钥和私钥),然后使用公钥对明文进行加密,使用私钥对密文进行解密。
综上所述,通过使用Java函数和常见的加密算法,我们可以对字符串进行加密和解密。加密可以保护数据的安全性和隐私,解密可以恢复加密之前的明文内容。在实际应用中,需要根据具体需求选择合适的加密算法和密钥长度,同时注意密钥的保护和管理。
