在加密js和pycrypto与AES问题 [英] Problems with AES in crypto-js and pycrypto
问题描述
我尝试实施
-
加密JS (一个javascript加密库)
crypto-js (a javascript crypto library)
和 pycrypto (一个Python加密库)
and pycrypto (a python crypto library)
在蟒蛇服务器端的我加密与 IV
和密码
并用密文的base64发送IV EN coded到的 JavaScript客户端的。然后我想解密
字符串的密码,用户可以进入。
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.
蟒蛇 - 服务器
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的 - 客户端
// <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});
通过这个例子,我得到一个JavaScript错误
With this example I get a javascript error
Uncaught URIError: URI malformed
但是,这仅仅是一个例子 - 我试过的base64编码/我能想到的解码的每一个星座。我也试图改变模式。但这些都是随机的测试,我想了解我真正要做的。
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.
- 不隐窝-JS想要什么编码?
- 我应该选择哪种模式?
- 有什么我应该蟒蛇服务器端更改?
- 是什么样的填充?难道还有错?
- 任何其他JavaScript库可以推荐?
非常感谢你,善良的reagards,
samuirai
thank you very much and kind reagards, samuirai
推荐答案
您之前带code为Base64您必须总结IV和encrypted_text:
Before You encode to base64 You must sum iv and encrypted_text:
encrypted_text = base64.b64encode(iv + aes.encrypt("this is a test.."))
从官方文档(https://www.dlitz.net/software/pycrypto/doc/):
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')
这篇关于在加密js和pycrypto与AES问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!