推荐答案
在Java中,可以使用javax.crypto包提供的加密算法和密钥库来进行对称加密和解密操作。对称加密使用相同的密钥同时进行加密和解密,因此需要安全地管理密钥以确保数据的保密性。下面是一个使用对称加密算法进行加密和解密的示例代码:
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 SymmetricEncryption {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
String encryptionKey = "SecretKey";
byte[] encryptedData = encrypt(plainText, encryptionKey);
System.out.println("Encrypted Data: " + Base64.getEncoder().encodeToString(encryptedData));
String decryptedText = decrypt(encryptedData, encryptionKey);
System.out.println("Decrypted Text: " + decryptedText);
}
public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
SecretKeySpec secretKey = generateKey(encryptionKey);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return encryptedData;
}
public static String decrypt(byte[] encryptedData, String encryptionKey) throws Exception {
SecretKeySpec secretKey = generateKey(encryptionKey);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData, StandardCharsets.UTF_8);
}
public static SecretKeySpec generateKey(String encryptionKey) throws Exception {
byte[] keyBytes = encryptionKey.getBytes(StandardCharsets.UTF_8);
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
return new SecretKeySpec(keyBytes, "AES");
}
}
上述代码使用AES算法进行对称加密和解密。首先,通过generateKey方法生成AES密钥,然后使用该密钥初始化加密和解密的Cipher对象。encrypt方法将明文字符串转换为字节数组后进行加密,返回加密后的字节数组。decrypt方法对加密后的字节数组进行解密并返回解密后的明文字符串。
注意:在实际应用中,密钥的生成和管理应该更加安全可靠,并且考虑使用随机生成的密钥。
其他答案
-
下面是另一种使用Java进行对称加密和解密的示例代码:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.spec.AlgorithmParameterSpec;
public class SymmetricEncryption {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
String encryptionKey = "SecretKey";
byte[] encryptedData = encrypt(plainText, encryptionKey);
System.out.println("Encrypted Data: " + new String(encryptedData, StandardCharsets.UTF_8));
String decryptedText = decrypt(encryptedData, encryptionKey);
System.out.println("Decrypted Text: " + decryptedText);
}
public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
char[] password = encryptionKey.toCharArray();
byte[] salt = { 1, 2, 3, 4, 5, 6, 7, 8 };
int iterationCount = 1000;
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
AlgorithmParameterSpec parameterSpec = new PBEParameterSpec(salt, iterationCount);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
byte[] encryptedData = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return encryptedData;
}
public static String decrypt(byte[] encryptedData, String encryptionKey) throws Exception {
char[] password = encryptionKey.toCharArray();
byte[] salt = { 1, 2, 3, 4, 5, 6, 7, 8 };
int iterationCount = 1000;
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
AlgorithmParameterSpec parameterSpec = new PBEParameterSpec(salt, iterationCount);
cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData, StandardCharsets.UTF_8);
}
}
上述代码使用PBEWithMD5AndDES算法进行对称加密和解密。通过使用相同的密码和盐值参数,可以生成相应的密钥并初始化Cipher对象。encrypt方法将明文字符串转换为字节数组后进行加密,返回加密后的字节数组。decrypt方法对加密后的字节数组进行解密并返回解密后的明文字符串。
-
下面是另一种使用Java进行对称加密和解密的示例代码,使用了更加高级的AES算法和加密模式,同时采用密钥生成器和Base64进行密钥和密文的编码:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.security.spec.KeySpec;
import java.util.Base64;
public class SymmetricEncryption {
public static void main(String[] args) throws Exception {
String plainText = "Hello, World!";
String encryptionKey = "SecretKey";
byte[] encryptedData = encrypt(plainText, encryptionKey);
System.out.println("Encrypted Data: " + Base64.getEncoder().encodeToString(encryptedData));
String decryptedText = decrypt(encryptedData, encryptionKey);
System.out.println("Decrypted Text: " + decryptedText);
}
public static byte[] encrypt(String plainText, String encryptionKey) throws Exception {
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
SecretKey secretKey = generateKey(encryptionKey, salt);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
byte[] iv = cipher.getIV();
byte[] encryptedDataWithIV = new byte[iv.length + encryptedData.length];
System.arraycopy(iv, 0, encryptedDataWithIV, 0, iv.length);
System.arraycopy(encryptedData, 0, encryptedDataWithIV, iv.length, encryptedData.length);
return encryptedDataWithIV;
}
public static String decrypt(byte[] encryptedDataWithIV, String encryptionKey) throws Exception {
byte[] iv = new byte[16];
System.arraycopy(encryptedDataWithIV, 0, iv, 0, iv.length);
byte[] encryptedData = new byte[encryptedDataWithIV.length - iv.length];
System.arraycopy(encryptedDataWithIV, iv.length, encryptedData, 0, encryptedData.length);
SecretKey secretKey = generateKey(encryptionKey, iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
byte[] decryptedData = cipher.doFinal(encryptedData);
return new String(decryptedData, StandardCharsets.UTF_8);
}
public static SecretKey generateKey(String encryptionKey, byte[] salt) throws Exception {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(encryptionKey.toCharArray(), salt, 65536, 256);
SecretKey tempSecretKey = factory.generateSecret(spec);
return new SecretKeySpec(tempSecretKey.getEncoded(), "AES");
}
}
上述代码使用更强大的AES算法和加密模式(CBC),并使用随机的初始化向量(IV)来提供更好的安全性。encrypt方法生成随机的salt并使用密码基础导出(PBKDF2)算法生成密钥,并使用CBC模式进行加密。密文包括IV和加密数据。decrypt方法从密文中提取IV并使用密钥进行解密。最终返回解密后的明文字符串。
无论使用哪种方法,对称加密和解密都需要处理密钥的安全性,选择合适的加密算法和使用正确的密钥长度是保护数据安全的重要因素。同时,对密钥的生成、存储和分发也需要考虑到安全性要求。在真实的应用中,请遵循密码学最佳实践,并确保密钥和加密的数据在传输和存储过程中受到适当的保护。