0

I have been assigned to decode tokens built in java using AES/CBC/NoPadding.

I am looking into node-forge and crypto for this. My problem is that I don't know what is the equivalent for the algorithm AES/CBC/NoPadding in NodeJs.

I'm using something like this:

var key = Array(xxx); // key.length = 16
var iv = new Buffer(16);
iv.fill(0);

var decipher = crypto.createDecipheriv("aes-128-ecb", key, iv);

As I searched, this createDecipheriv("aes-128-ecb" is equivalent to AES/CBC/PKCS5Padding. I though of decipher.setPadding(false) but I guess I'm in the wrong path).

  • You need padding with a block based cipher unless every message is an exact multiple of the block size, 16-bytes for AES. The typical padding is PKCS#7 (née PKCS#5). – zaph Apr 05 '18 at 13:29
  • Provide a [mcve] including sample data. – zaph Apr 05 '18 at 13:36
  • There is a "Favorite" also. – zaph Apr 05 '18 at 13:52
  • This is not a code request.... please, don't understand my question as such... I've been requested the decryption in Node Js, based on a working java. That is already done, with the exception that I don't find anywhere, which algorithym to use... Right now, I have something like: crypto.createDecipheriv("aes-128-ecb", key, iv); where key is an Array(16) and iv is a Buffer(16). When I run my code, it breaks on this line, with "Invalid IV length". From what i've searched, aes-128-ecb matches AES/ECB/PKCS5Padding in Java (and that is not what I need). – Nuno Pestana Apr 05 '18 at 14:09
  • 1
    ECB mode does not use an IV. `aes-128-ecb` is **not** equivalent to `AES/CBC/PKCS5Padding`. ECB and CBC are encryption modes, PKCS5Padding is adding padding bytes to the end of the data during encrypted so it wi be a multiple of the block size and removing the padding on decryption. Take some time and study AES encryption, modes and padding. – zaph Apr 05 '18 at 14:57
  • 2
    "Convert this custom crypto from one language to another" is notoriously difficult to have answered on SO. I don't answer them any more because they're too specific to one code base and aren't reusable by future searchers. If you do need this kind of work done, I recommend contracting someone for a short engagement. Lots of people handle this kind of work, including me, and it generally isn't very expensive if the algorithm is simple and you have a working version and test cases. – Rob Napier Apr 05 '18 at 18:22
  • Managed to resolve the issue and have a working solution now... thanks for the help! – Nuno Pestana Apr 06 '18 at 13:49

1 Answers1

2

Using forge.cipher.createDecipher('AES-CBC', key); did the job...

Internally it uses padding, but I could overcome the issue adding some garbage before decryption and then taking care of the remaining garbage after internal padding.

  • 1
    Deciphering requires unpadding, not padding. And usually if unpadding fails an error is thrown. Could you possibly share the code? – Maarten Bodewes Apr 10 '18 at 13:04