Reputation: 31
I need A Sample Code in Java to
Encrypt The String in AES 256 (Rijndael) with padding in CBC mode : MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC
Then Encode the result in base 64.
using Encryption Key:-"f53c50e4ab798944a4debe137ec8ba677dbf44ebd37e373f785bf59a8c048c54".
*i had created this example and it is giving error key invalid but i want to use the above key only
public class aes {
public static void main(String[] args) throws Exception {
System.out.println("started");
System.out.println(encrypt("hello"));
System.out.println("end");
}
public static String encrypt(String value) throws Exception{
byte[] keyBytes="f53c50e4ab798944a4debe137ec8ba677dbf44ebd37e373f785bf59a8c048c54".getBytes();
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] res=cipher.doFinal(value.getBytes());
String result=Base64.getEncoder().encodeToString(res);
return result;
}
}
Upvotes: 0
Views: 478