Problems with AES in crypto-js and pycrypto

血红的双手。 提交于 2019-12-10 21:07:42

问题


I try to implement a communication between

  • crypto-js (a javascript crypto library)

  • and pycrypto (a python crypto library)

On the python server side I encrypt a string with an iv and a passphrase and send the iv with the encrypted text base64 encoded to the javascript client side. Then I want to decrypt the string with the passphrase the user can enter.

python - server

from Crypto.Cipher import AES
from Crypto import Random

iv = Random.get_random_bytes(16)
key = "1234567812345678"
aes = AES.new(key, AES.MODE_CFB, iv)
encrypted_text = base64.b64encode(aes.encrypt("this is a test.."))
iv = base64.b64encode(iv)
# send iv, encrypted_text to client

javascript - client

// <script type="text/javascript" 
        src="http://crypto-js.googlecode.com/files/2.5.3-crypto-sha1-hmac-pbkdf2-blockmodes-aes.js">
   </script>
// text, and iv is base64encoded from the python script
// key is a string from an <input type='text'>
decrypted = Crypto.AES.decrypt(text, key, {iv: iv, mode: new Crypto.mode.CFB});

With this example I get a javascript error

Uncaught URIError: URI malformed

But this is just one example - I tried every constellation of base64 encodings/decodings I could think of. I also tried to changed the Mode. But these are all random tests and I want to understand what I really have to do.

  • What encoding does the crypt-js want?
  • Which mode should I chose?
  • Is there something I should change on the python server side?
  • what is about padding? Could there be the fault?
  • any other javascript libraries you can recommend?

thank you very much and kind reagards, samuirai


回答1:


Before You encode to base64 You must sum iv and encrypted_text:

encrypted_text = base64.b64encode(iv + aes.encrypt("this is a test.."))

From the official documentation (https://www.dlitz.net/software/pycrypto/doc/) :

As an example, encryption can be done as follows:

from Crypto.Cipher import AES
from Crypto import Random

key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn')


来源:https://stackoverflow.com/questions/9565017/problems-with-aes-in-crypto-js-and-pycrypto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!