I've looked for javascript libraries that can decrypt against an AES 128 encrypted String. I've found several :
- http://www.movable-type.co.uk/scripts/aes.html
- http://www.hanewin.net/encrypt/aes/aes-test.htm : you will have to look the source
- https://code.google.com/p/crypto-js/
My problem is these algoritms take as input either a String or a HexString. My case is a bit special, because my input in a byte array. I've coded a test case in Java :
String key = "MrSShZqHM6dtVNdX";
String message = "NzZiNGM3ZjIyNjM5ZWM3M2YxMGM5NjgzZDQzZDA3ZTQ=";
String charsetName = "UTF-8";
String algo = "AES";
// decode message
byte[] decodeBase64 = Base64.decodeBase64(message.getBytes(charsetName));
System.out.println("decoded message: " + new String(decodeBase64));
// prepare the key
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(charsetName), algo);
// aes 128 decipher
Cipher cipher = Cipher.getInstance(algo);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
byte[] doFinal = cipher.doFinal(Hex.decodeHex(new String(decodeBase64).toCharArray()));
System.out.println("done with: " + new String(doFinal));
Output is :
decoded message: 76b4c7f22639ec73f10c9683d43d07e4
done with: 390902
But this is Java, right? The org.apache.commons.codec.binary.Hex.decodeHex
method converts an array of characters representing hexadecimal values into an array of bytes of those same values. The returned array will be half the length of the passed array, as it takes two characters to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
In decimal representation, Hex.decodeHex
method gives this byte array : [118, -76, -57, -14, 38, 57, -20, 115, -15, 12, -106, -125, -44, 61, 7, -28];
The java AES decipher takes a byte array as input, but in Javascript, no lib does that. I've tried to tweak a bit the one here but dude that's hardcore code. This is really not my field...
The closest I've been was on this online tool. My key is MrSShZqHM6dtVNdX
and with apache commons Hex.encodeHex I get 4d725353685a71484d366474564e6458 giving me an output of 3339303930320a0a0a0a0a0a0a0a0a0a, which is almost my wanted output (390902)...