Search code examples
c#node.jsaesportingcryptojs

Porting JS (or node.js, i'm now sure) aes to c# aes


i have this JS code:

d = CryptoJS.AES.decrypt((t), CryptoJS.MD5(key), {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7,
    keySize: 4
}).toString(CryptoJS.enc.Utf8);

I don't know how I can set keySize = 4 in C#

setting keySize = 4 causing exception

I need port JS code to C#


Solution

  • CryptoJS stores all data in a data structure called WordArray. Each "word" stores 32 bit (4 byte). A key size of 4 in CryptoJS means a key size of 128 bit or 16 byte everywhere else.

    APIs are generally written differently and most of the time, you don't have the exact same object that represent the same data in the same way across programming languages.

    C# works with byte arrays. So, you will need to create a key that has 16 bytes in it. Keep in mind that MD5 always produces 128 bit output (16 bytes), so you probably don't have to do anything special.