简体   繁体   English

我无法解密在cryptojs中加密的消息,其加密是在xycrypto中完成的,会出现什么问题?

[英]I cannot decrypt message encrypted in cryptojs, whose encryption was done in xycrypto, what will be the problem?

I am doing an exercise where I must encrypt with 3DES with OFB with some python library, in my case use xycrypto ( https://pypi.org/project/xycrypto/ ):我正在做一个练习,我必须使用带有一些 python 库的 OFB 使用 3DES 进行加密,在我的情况下使用 xycrypto ( https://pypi.org/project/xycrypto/ ):

import base64
from xycrypto.ciphers import TripleDES_OFB

key = b'2ndl38aj2nk3l47d'
iv = b'pwler1o2'
plaintext = b'SupErCaliFRAGIlisTICOESpirALiDOso'
cipher = TripleDES_OFB(key, iv=iv)
msg = cipher.encrypt(plaintext)

dex = cipher.decrypt(base64.b64decode('HOQOSp3XKCe85sS6/5VMVuyM8lae5lmpqkMYlVm1Gxg/'))
print(dex)
msgF = '"'+base64.b64encode(msg).decode('utf-8')+'"'
keyF = '"'+key.decode('utf-8')+'"'
ivF = '"'+iv.decode('utf-8')+'"'
print(msgF)
html = open('../index.html','w')
mensaje = """<!DOCTYPE html>
<html lang="es" style="background-color: black; color: white;">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Tarea 3 Criptografia y seguridad en redes</title>
    </head>
    <body>
        <p>Este sitio contiene un mensaje secreto</p>
        <div class="algorithm" id="""+msgF+"""></div>
        <div class="iv" id="""+ivF+"""></div>
        <div class="key" id="""+keyF+"""></div>
    </body>
</html>
"""
html.write(mensaje)
html.close()

Create an HTML whose tags contain the encrypted text, the key and the iv, finally, as required by the exercise, I must take the values with JS and tampermonkey, the plugin code is as follows:创建一个HTML,其标签包含密文、密钥和iv,最后,根据练习的要求,我必须用JS和tampermonkey取值,插件代码如下:

(function decryptTripleDES() {
    'use strict'
    var html = document.getElementsByClassName("algorithm");
    var html2 = document.getElementsByClassName("iv");
    var html3 = document.getElementsByClassName("key");
    var ciphertext = html[0].id
    var key = html3[0].id;
    var iv = html2[0].id;
    var hexK = CryptoJS.enc.Hex.parse(key);
    var hexI = CryptoJS.enc.Hex.parse(iv);
    var decrypted = CryptoJS.TripleDES.decrypt(ciphertext, hexK, {
      iv: hexI,
      mode: CryptoJS.mode.OFB
    });
    alert(decrypted.toString(CryptoJS.enc.Utf8))
})();

The plugin imports the cryptojs library, calling the decrypt function of 3DES and OFB, I import the html parameters and try to decrypt the content and try to display it with alert, but it returns empty.插件导入cryptojs库,调用3DES和OFB的解密function,我导入html参数,尝试解密内容,尝试显示alert,但返回空。 Am I doing something wrong?难道我做错了什么? It occurs to me that in JS I don't do the base changes correctly with the input parameters but I'm not sure.我突然想到,在 JS 中,我没有使用输入参数正确地进行基本更改,但我不确定。

PD: I also tried changing the python library to PyCryptodome but got the same results. PD:我还尝试将 python 库更改为 PyCryptodome,但得到了相同的结果。

Key and IV are ASCII (or UTF-8) encoded in the Python code, so the appropriate encoder must be used in the CryptoJS code ( Latin1 or Utf8 ). Key 和 IV 是在 Python 代码中编码的 ASCII(或 UTF-8),因此必须在 CryptoJS 代码( Latin1Utf8 )中使用适当的编码器

Moreover, CryptoJS applies PKCS7 padding by default , even for stream cipher modes like OFB .此外,CryptoJS默认应用 PKCS7 填充,即使对于像OFB这样的 stream 密码模式也是如此。 Therefore, padding must be explicitly disabled:因此,必须显式禁用填充:

 var ciphertext = "BbUxBiVklN80imgxk/xaonLjwV+BKfEBciTJRF+lGItI"; var key = "2ndl38aj2nk3l47d"; var iv = "pwler1o2"; var hexK = CryptoJS.enc.Latin1.parse(key); // ASCII (or Utf8) encode var hexI = CryptoJS.enc.Latin1.parse(iv); // ASCII (or Utf8) encode var decrypted = CryptoJS.TripleDES.decrypt(ciphertext, hexK, { iv: hexI, mode: CryptoJS.mode.OFB, padding: CryptoJS.pad.NoPadding // disable PKCS7 padding }); console.log(decrypted.toString(CryptoJS.enc.Utf8));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

The above code snippet uses the values from the Python code for ciphertext, key and IV and successfully decrypts the ciphertext with them.上面的代码片段使用来自 Python 代码的值作为密文、密钥和 IV,并成功地用它们解密了密文。

Provided your values for ciphertext, key and iv in the CryptoJS code are correctly derived from the HTML, your code should also successfully decrypt the ciphertext after these fixes.如果 CryptoJS 代码中的密文、密钥和 iv 值正确地从 HTML 派生,那么在这些修复之后,您的代码也应该成功解密密文。

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

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