推薦答案
在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并使用密鑰進行解密。最終返回解密后的明文字符串。
無論使用哪種方法,對稱加密和解密都需要處理密鑰的安全性,選擇合適的加密算法和使用正確的密鑰長度是保護數據安全的重要因素。同時,對密鑰的生成、存儲和分發也需要考慮到安全性要求。在真實的應用中,請遵循密碼學最佳實踐,并確保密鑰和加密的數據在傳輸和存儲過程中受到適當的保護。