我有这个简单的代码,我在互联网上找到的..我正在学习加密/解密的东西..这个代码似乎工作正常,但我不明白......为什么在“c.doFinal() "(用于使用 AES-256 进行加密/解密)这家伙使用 BASE64 对加密值进行编码/解码?仅使用 AES 还不够吗?
`private static final String ALGO = "AES";
private static final byte[] keyValue =
new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't', 'S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
public static void main(String[] args) throws Exception {
String data = "SOME TEXT";
String dataEnc = AES.encrypt(data);
String dataDec = AES.decrypt(dataEnc);
System.out.println("Plain Text : " + data);
System.out.println("Encrypted Text : " + dataEnc);
System.out.println("Decrypted Text : " + dataDec);
}`
谢谢!!