在Java中,可以使用第三方庫來實現配置文件的加密與解密。以下是一些常用的加密與解密方法:
JCE提供了一系列的加密算法,如AES、DES、RSA等。以下是一個使用AES加密和解密的示例:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESUtil {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
public static String encrypt(String data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
public static String decrypt(String data, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(data));
return new String(decryptedData);
}
public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();
String plainText = "Hello, world!";
String encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Plain text: " + plainText);
System.out.println("Encrypted text: " + encryptedText);
System.out.println("Decrypted text: " + decryptedText);
}
}
JCA是Java加密框架的基礎,提供了更多的加密算法和更安全的密鑰管理。以下是一個使用JCA和AES加密和解密的示例:
import javax.crypto.Cipher;
import javax.crypto.NoSuchAlgorithmException;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;
public class JcaAESUtil {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
public static String encrypt(String data, Key key) throws NoSuchAlgorithmException {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedData = cipher.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encryptedData);
}
public static String decrypt(String data, Key key) throws NoSuchAlgorithmException {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(data));
return new String(decryptedData);
}
public static void main(String[] args) throws NoSuchAlgorithmException {
Key key = new SecretKeySpec("ThisIsASecretKey".getBytes(), ALGORITHM);
String plainText = "Hello, world!";
String encryptedText = encrypt(plainText, key);
String decryptedText = decrypt(encryptedText, key);
System.out.println("Plain text: " + plainText);
System.out.println("Encrypted text: " + encryptedText);
System.out.println("Decrypted text: " + decryptedText);
}
}
除了Java內置的加密庫,還可以使用一些第三方庫來實現配置文件的加密與解密,如Apache Commons Codec、Bouncy Castle等。這些庫通常提供了更豐富的加密算法和更強大的功能。
例如,使用Apache Commons Codec進行Base64編碼和解碼:
import org.apache.commons.codec.binary.Base64;
public class Base64Util {
public static String encode(String data) {
return Base64.encodeBase64String(data.getBytes());
}
public static String decode(String data) {
return new String(Base64.decodeBase64(data));
}
public static void main(String[] args) {
String plainText = "Hello, world!";
String encodedText = encode(plainText);
String decodedText = decode(encodedText);
System.out.println("Plain text: " + plainText);
System.out.println("Encoded text: " + encodedText);
System.out.println("Decoded text: " + decodedText);
}
}
請注意,加密與解密只是保護配置文件的一部分,還需要考慮如何安全地存儲和管理密鑰。在實際應用中,建議使用密鑰管理系統(KMS)或硬件安全模塊(HSM)等安全存儲設備來保護密鑰。