Search code examples
javascriptpythonaespycryptocryptojs

Making CryptoJS and pycrypto compatible


I'm trying to figure out how to use the CryptoJS library for AES but the documentation is somewhat scarce.

I've followed their example but I can't seem to decode something that I encoded in PyCrypto

I've formatted as follows:

key = '0f3f0fe38eb5f216cb4ef0649f81d761'
ciphertext = 'Y+gwBI6R37Ko1lQmwZ57gg==FqUSqQ=='

The ciphertext has two components

iv: Y+gwBI6R37Ko1lQmwZ57gg==
encoded message: FqUSqQ==

I tried running the code below but it didn't work. It logs "" in the console. It should be resolving to 'Test'

var decrypted =CryptoJS.AES.decrypt(ciphertext, key);
console.log( decrypted.toString(CryptoJS.enc.Utf8) );

Any pointers would great. Thanks


Solution

  • I ran into this problem as well and unfortunately it was very tricky to get pycrypto to talk to crypto-js. Here are the combination that worked for me:

    1. Use MODE_CFB with segment_size=128 when encrypting in Python
    2. You must pad your string with Pkcs7 as per RFC 2315 [Page 21]
    3. Once you have received the base64 version of ciphertext in JS, you need to wrap it in CryptoJS.lib.CipherParams.create({ ... })

    Take a look at the code that works for me (Python 2.7.x):
    https://gist.github.com/marcoslin/8026990