Tôi có mã đơn giản này, mà tôi tìm thấy trên internet .. im học tập công cụ này của mã hóa/giải mã .. mã này dường như làm việc tốt, nhưng tôi không hiểu điều gì đó ... tại sao sau "c.doFinal()" (để mã hóa/giải mã với AES-256) anh chàng này mã hóa/giải mã giá trị được mã hóa, với BASE64? nó không đủ chỉ bằng cách sử dụng AES?Mã hóa với AES-256 Java
`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);
}`
Cảm ơn !!
Điều này thực sự sử dụng AES 128 bit, không phải 256. Khóa là 16 byte; 16 byte * 8 bit trên mỗi byte = khóa 128 bit. – megaflop