AES在cryptojs中加密并在python Crypto.Cipher中解密 [英] AES encrypt in cryptojs and decrypt in python Crypto.Cipher

查看:1108
本文介绍了AES在cryptojs中加密并在python Crypto.Cipher中解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用js CryptoJS加密并使用python crypto.Cipher解密时遇到问题

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

这是我在js中的实现,
使用加密的消息附加iv和用base64编码

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>

然后在python脚本中我使用过aes加密和解密

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()

但相同的iv,消息和密钥在python和js中产生不同的加密消息,

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

JavaScript与python解密兼容的问题是什么?

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

两者都使用AES.MODE_CBC并假定两者都使用了Pkcs7填充。硬编码的iv现在是随机生成的

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

推荐答案

尝试使用实际上与AES块大小相同的IV ,16个字节。您当前正在以十六进制指定8个字节。 CBC模式需要一个与块大小相同的IV,并且Python API指定(包括最终输入错误):

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):


对于所有其他模式,必须是block_size个字节长。

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

最好使用上面的方法或(预定义)常量。

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

这篇关于AES在cryptojs中加密并在python Crypto.Cipher中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆