crypto-js is used by javascript developers to encrypt text. Its simple to use.
var CryptoJS = require("crypto-js");
// Encrypt
var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123');
// Decrypt
var bytes = CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123');
var plaintext = bytes.toString(CryptoJS.enc.Utf8);
console.log(plaintext);
In this example encrypt function is taking only two arguments messageToEncrypt and salt. Rest of the configuration would be inside its definition. I am not a javascript guy so its difficult to find and understand encrypt definition.
I want to achieve same AES
encryption using java. So for same input argument e.g. messageToEncrypt and salt I should get same encrypted text using crypto-js library and Java implementation.
I tried javax.crypto
by exploring some links on google.
String plainText = "messageToEncrypt";
String key = "mySalt";
SecretKey secKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
byte[] x = aesCipher.doFinal(plainText.getBytes());
System.out.println(x);
But this is not working for me because I dont know exact parameters like keySize and iterationCount.
I tried to use https://github.com/mpetersen/aes-example/blob/master/src/main/java/org/cloudme/sample/aes/AesUtil.java also but again I am not sure about keySize and iterationCount.
How can I create simple exact implementation of crypto-js's AES encryption in java?