5

Getting problem with encrypt using js CryptoJS and decrypt that using python crypto.Cipher

This is my implementation in js, append iv with encrypted message and encode with base64

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <script> var message='Secreat Message to Encrypt'; var key = CryptoJS.enc.Hex.parse('824601be6c2941fabe7fe256d4d5a2b7'); var iv = CryptoJS.enc.Hex.parse('1011121314151617'); var encrypted = CryptoJS.AES.encrypt(message, key, { iv: iv, mode: CryptoJS.mode.CBC }); encrypted =encrypted.toString(); encrypted = iv+encrypted; encrypted = btoa(encrypted); console.log('encrypted',encrypted ); alert(encrypted); // var decrypted = CryptoJS.AES.decrypt(encrypted, key, { iv: iv, mode: CryptoJS.mode.CBC }); // console.log('decrypted', decrypted); //alert(decrypted.toString(CryptoJS.enc.Utf8)); </script> 

And in the python script for aes encryption and decryption i used

#!/usr/bin/python import os, random, struct from Crypto.Cipher import AES from Crypto import Random import base64 class AESCipher: def __init__(self, key): BS = 16 self.pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) self.unpad = lambda s : s[0:-ord(s[-1])] self.key = self.pad(key[0:16]) def encrypt(self, raw): raw = self.pad(raw) iv = "1011121314151617" cipher = AES.new(self.key, AES.MODE_CBC, iv) return base64.b64encode(iv + cipher.encrypt(raw)) def decrypt(self, enc): enc = enc.replace(' ', '+') enc = base64.b64decode(enc) iv = enc[:16] cipher = AES.new(self.key, AES.MODE_CBC, iv) return self.unpad(cipher.decrypt( enc[16:])) def main(): cipher = AESCipher('824601be6c2941fabe7fe256d4d5a2b7') encrypteddata =cipher.encrypt(''Secreat Message to Encrypt'') print encrypteddata decryptdata =cipher.decrypt(encrypteddata) print decryptdata main() 

but same iv, message and key produce different encrypted message in python and js,

what is the problem with JavaScript to compatible with python decryption?

Both used AES.MODE_CBC and assume both used Pkcs7 padding. hard coded iv for now those are generate randomly

4
  • Does CryptoJS.AES.encrypt() base64 encode the output as well? Because in your js code, you are returning base64 encoded AES stuff. Commented Jan 7, 2014 at 13:30
  • append iv to CryptoJS.AES.encrypt() produce encrypted message as iv+encrypted then do base64 encoding as it to match python encryption "base64.b64encode(iv + cipher.encrypt(raw))" Commented Jan 7, 2014 at 13:36
  • Sorry, didn't see the btoa() call. Hmm.. Commented Jan 7, 2014 at 13:38
  • @UdayaLakmal , did you solve that? ty Commented Feb 4, 2016 at 19:20

1 Answer 1

0

Try with an IV that has actually the same size as the block size of AES, 16 bytes. You are currently specifying 8 bytes in hexadecimals. CBC mode requires an IV of the same size as the block size and the Python API specifies (including final typo):

For all other modes, it must be block_size bytes longs.

It's best to use a method or (predefined) constant like above.

Sign up to request clarification or add additional context in comments.

2 Comments

use iv as '101112131415161718191a1b1c1d1e1f' but when try to decrypt in python get exception "Input strings must be a multiple of 16 in length" same result if iv = CryptoJS.enc.Utf8.parse('7061737323313233');
Does the python expect hex for the IV, or does it expect bytes? You may want to hex decode the IV (and key?) first...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.