AESExample.java
AESExample.java
Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.SecureRandom;
import java.utill.Base64;
public class AESExample{
public static void main(String[] args)throws Exception{
String plaintext="Hello,World!';
SecretKey secretKey=generateAeskey()
String ciphertext=encryptAES(plaintext,secretKey);
String decryptedText=decryptAES(ciphertext,secretkey);
System.out.printin("Plaintext:"+plaintext);
System.out.printin("Ciphertext:"+ciphertext);
System.out.printin("Decryptedtext:"+decryptedText);
}
public static SecretKey generateAESKey()throws Exception{
KeyGenerator KeyGenerator=KeyGenerator.getinstance("AES")
KeyGenerator.init(256,new secureRandom());
return KeyGenerator.generateKey();
}
public static String encryptAES(String plaintext,SecretKey secretKey)throws
Exception{
Cipher cipher=Cipher.getinstance("AES");
cipher.init(Cipher.DECRYPT_MODE,secretkey);
byte[]ciphertextByte=Base64.getDecoder().decode(ciphertext);
byte[]plaintextBytes=cipher.doFinall(ciphertextBytes);
return new String(plaintextBytes);
}
}