AES encryption using Java and decryption using Javascript

I am creating an application that requires Java based AES encryption and JavaScript based decryption. I use the following code for encryption as the base form.

public class AESencrp { private static final String ALGO = "AES"; private static final byte[] keyValue = new byte[] { 'A', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k','l', 'm', 'n', 'o', 'p'}; public static String encrypt(String Data) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(Data.getBytes()); String encryptedValue = new BASE64Encoder().encode(encVal); return encryptedValue; } private static Key generateKey() throws Exception { Key key = new SecretKeySpec(keyValue, ALGO); return key; } } 

The JavaScript I'm trying to use for decryption,

 <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"> </script> var decrypted = CryptoJS.AES.decrypt(encrypted,"Abcdefghijklmnop").toString(CryptoJS.enc.Utf8); 

But JavaScript decryption does not work. I'm new to this, can someone tell me a way to solve without changing the code of the Java code?

I tried Base-64 to decrypt my text as follows:

 var words = CryptoJS.enc.Base64.parse(encrKey); var base64 = CryptoJS.enc.Base64.stringify(words); var decrypted = CryptoJS.AES.decrypt(base64, "Abcdefghijklmnop"); alert("dec :" +decrypted); 

but still nothing good.

I tried the solution suggested below to solve a possible filling problem, but did not provide any solution.

 var key = CryptoJS.enc.Base64.parse("QWJjZGVmZ2hpamtsbW5vcA=="); var decrypt = CryptoJS.AES.decrypt( encrKey, key, { mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7 } ); alert("dec :" +decrypt); 
+7
java javascript encryption aes cryptojs
source share
2 answers
  • Java code uses the 128-bit AES key, while JavaScript code uses the 256-bit AES key.

  • Your Java code uses the value "Abcdefghijklmnop" .getBytes () as the actual key value, while your JavaScript code uses "Abcdefghijklmnop" as the passphrase from which the actual key is derived.

  • The default mapping for Java AES is AES / ECB / PKCS5Padding, and the default mapping for CryptoJS is AES / CBC / PKCS7Padding.

One way to fix your example is to fix the JavaScript side:

 // this is Base64 representation of the Java counterpart // byte[] keyValue = new byte[] { 'A', 'b', 'c', 'd', 'e', 'f', 'g', // 'h', 'i', 'j', 'k','l', 'm', 'n', 'o', 'p'}; // String keyForJS = new BASE64Encoder().encode(keyValue); var base64Key = "QWJjZGVmZ2hpamtsbW5vcA=="; console.log( "base64Key = " + base64Key ); // this is the actual key as a sequence of bytes var key = CryptoJS.enc.Base64.parse(base64Key); console.log( "key = " + key ); // this is the plain text var plaintText = "Hello, World!"; console.log( "plaintText = " + plaintText ); // this is Base64-encoded encrypted data var encryptedData = CryptoJS.AES.encrypt(plaintText, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); console.log( "encryptedData = " + encryptedData ); // this is the decrypted data as a sequence of bytes var decryptedData = CryptoJS.AES.decrypt( encryptedData, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 } ); console.log( "decryptedData = " + decryptedData ); // this is the decrypted data as a string var decryptedText = decryptedData.toString( CryptoJS.enc.Utf8 ); console.log( "decryptedText = " + decryptedText ); 
+9
source share

In order for Java and JavaScript to communicate, it is important that the default values ​​are not used when creating the key or encryption. The number of iterations, key length, padding, salt and IV should be the same.

Link: https://github.com/mpetersen/aes-example

Sample code below:

String Encryption in Java:

  String keyValue = "Abcdefghijklmnop"; SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); KeySpec spec = new PBEKeySpec(keyValue.toCharArray(), hex("dc0da04af8fee58593442bf834b30739"), 1000, 128); Key key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES"); Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(hex("dc0da04af8fee58593442bf834b30739"))); byte[] encVal = c.doFinal("The Quick Brown Fox Jumped over the moon".getBytes()); String base64EncodedEncryptedData = new String(Base64.encodeBase64(encVal)); System.out.println(base64EncodedEncryptedData); 

}

Decoding the same string in JavaScript:

 var iterationCount = 1000; var keySize = 128; var encryptionKey ="Abcdefghijklmnop"; var dataToDecrypt = "2DZqzpXzmCsKj4lfQY4d/exg9GAyyj0hVK97kPw5ZxMFs3jQiEQ6LLvUsBLdkA80" //The base64 encoded string output from Java; var iv = "dc0da04af8fee58593442bf834b30739" var salt = "dc0da04af8fee58593442bf834b30739" var aesUtil = new AesUtil(keySize, iterationCount); var plaintext = aesUtil.decrypt(salt, iv, encryptionKey, dataToDecrypt); console.log(plaintext); **//AESUtil - Utility class for CryptoJS** var AesUtil = function(keySize, iterationCount) { this.keySize = keySize / 32; this.iterationCount = iterationCount; }; AesUtil.prototype.generateKey = function(salt, passPhrase) { var key = CryptoJS.PBKDF2(passPhrase, CryptoJS.enc.Hex.parse(salt), { keySize: this.keySize, iterations: this.iterationCount }); return key; } AesUtil.prototype.decrypt = function(salt, iv, passPhrase, cipherText) { var key = this.generateKey(salt, passPhrase); var cipherParams = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Base64.parse(cipherText) }); var decrypted = CryptoJS.AES.decrypt(cipherParams,key, { iv: CryptoJS.enc.Hex.parse(iv) }); return decrypted.toString(CryptoJS.enc.Utf8); } } 
+1
source share

All Articles