简体   繁体   English

CryptJS PyCryptoDome - JS 端 AES 解密是不可能的

[英]CryptJS PyCryptoDome - JS Side AES Decryption not possible

I have been struggling for a day now with the following problem.我已经为以下问题苦苦挣扎了一天。 I have already check a lot of answers in other topics, but nothing seems to work.我已经在其他主题中检查了很多答案,但似乎没有任何效果。 Therefore, I have to ask here what the problem might be.因此,我必须在这里问可能是什么问题。

So, I can encrypt something in JS using AES w/ MODE_CBC, and decrypt it in python.所以,我可以使用 AES w/MODE_CBC 在 JS 中加密一些东西,并在 python 中解密它。 However, I cannot do the other way around, therefore, I cannot encrypt in Python and decrypt in CryptoJS using the same mode, keys, and IVs.但是,我不能反过来做,因此,我不能使用相同的模式、密钥和 IV 在 Python 中加密和在 CryptoJS 中解密。 I thought that maybe the JS would receive the Passphrase, or the string or the IV worng, but it does not.我认为 JS 可能会收到密码短语、字符串或 IV 磨损,但它不会。

Can anyone help?任何人都可以帮忙吗?

Here is the code这是代码

function Decrypt(key_from_python, passcode_encrypted_from_python){
    var key, k = sha256(key_from_python).substring(0, 32)
    var iv, i = sha256(key_from_python).substring(0, 16)
    key =  CryptoJS.enc.Utf8.parse(key);
    iv = CryptoJS.enc.Utf8.parse(iv);
    var to_decrypt = CryptoJS.enc.Base64.parse(passcode_encrypted_from_python)
    var encrypted = CryptoJS.AES.decrypt({ciphertext : to_decrypt}, key, {
          iv: iv,
    });
    return {
        "passcode": to_decrypt.toString(CryptoJS.enc.Base64),
        "passcode_bytes": to_decrypt.toString(CryptoJS.enc.Latin1),
        "s" : to_decrypt,
        "key" : k,
        "iv" : i,
        "enc" : encrypted
    }
}

Python Code:蟒蛇代码:

def _pad(self, data):
    length = 16 - (len(data) % 16)
    return data + chr(length)*length

def _encrypt(self, to_encrypt, passphrase, iv):
    to_encrypt = self._encode(to_encrypt)
    print("To Encrypt", to_encrypt)
    passphrase = self._encode(passphrase)
    iv = self._encode(iv)

     #aes = AES.new(passphrase, AES.MODE_CFB, iv, segment_size=128)
     aes = AES.new(passphrase, AES.MODE_CBC, iv=iv)
     return aes.encrypt(to_encrypt)

OUTPUT FROM PYTHON:来自 Python 的输出:

key:  f26e2b2b7f6e89f5a8601bd24fb15327
iv:  f26e2b2b7f6e89f5
passcode:  b'8/TCP7QS\x08\x08\x08\x08\x08\x08\x08\x08'
To Encrypt b'8/TCP7QS\x08\x08\x08\x08\x08\x08\x08\x08'
Encrypted:  ¹Ïô·öï³Æ¼ÏÛ 
Encryptedb64 oLnPmfS39u+zxrzP28KYIA==

OUTPUT FROM JS: JS 的输出:

key f26e2b2b7f6e89f5a8601bd24fb15327
iv f26e2b2b7f6e89f5
Encrypted:  ¹Ïô·öï³Æ¼ÏÛ 
Encryptedb64 oLnPmfS39u+zxrzP28KYIA==

However, the decrypted from JS giveS:然而,从 JS 解密得到:

{
  'sigBytes': -114, 
   'words': [-1446864399, -2036001390, 207964078, 2005241986]
}

Any idea??任何的想法??

Many Thanks非常感谢

I had a similar issue between Python and Java in the past which came down to incompatibilities in how the data was padded prior to encryption.过去,我在 Python 和 Java 之间遇到过类似的问题,归结为在加密之前填充数据的方式不兼容。 The Python code was using a python module which had a defect ( https://github.com/jeppeter/pypkcs7/issues/1 ) and in some cases wouldn't pad at all and the Java code was expecting a pad. Python 代码使用的 Python 模块存在缺陷( https://github.com/jeppeter/pypkcs7/issues/1 ),在某些情况下根本不会填充,而 Java 代码需要填充。

The _pad method is padding correctly but it is not called in this example. _pad 方法正确填充,但在本示例中未调用。 If this is not called and CryptoJS is expecting a pad then this may explain your issue.如果这没有被调用并且 CryptoJS 需要一个垫子,那么这可以解释你的问题。

As mentioned by @Topaco in the comments there could be some other things going on depending on the content of the encode and sha256 methods.正如@Topaco 在评论中提到的,根据 encode 和 sha256 方法的内容,可能还会发生其他一些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM