1

I've looked for javascript libraries that can decrypt against an AES 128 encrypted String. I've found several :

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)...

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
Marcel Falliere
  • 1,884
  • 21
  • 39
  • `Java != JavaScript` ! – Lukas Knuth Feb 27 '13 at 11:54
  • Java is to Javascript what a Car is to a Carpet... I know :) I haven't given the context, but I need to do this in javascript because it's for a mobile app for multiple devices using phonegap. I could code a phonegap plugin but I want the least amount of platform specific code. And this, the aes decipher, should not be specific to android or blackberry. That's why I want it in javascript. The java part is just to validate the data against a test case. – Marcel Falliere Feb 27 '13 at 11:57
  • You should in this case tag it as such. – Lukas Knuth Feb 27 '13 at 12:00
  • 1
    From what I've read.. wouldn't it just be easier to convert the byte array to a string, and use that? [Here](http://stackoverflow.com/questions/3195865/javascript-html-converting-byte-array-to-string) is a link to answer that question for you. :) – christopher Feb 27 '13 at 12:05
  • @ChrisCooney it gives `ルøワdH è→ ` and `vᄡᅦ￲&9↓s￱ヨテᅯ=¦` :( – Marcel Falliere Feb 27 '13 at 12:16
  • at least with the `bin2String` method from the thread you are refenrencing, I get the same weird result from different libs... – Marcel Falliere Feb 27 '13 at 13:11
  • Why do you need to hex decode the value while it has already been decoded from base64? Do you store the values double encoded? – Maarten Bodewes Feb 27 '13 at 21:01

0 Answers0