【问题标题】:I cannot decrypt message encrypted in cryptojs, whose encryption was done in xycrypto, what will be the problem?我无法解密在cryptojs中加密的消息,其加密是在xycrypto中完成的,会出现什么问题?
【发布时间】:2021-05-25 05:35:57
【问题描述】:

我正在做一个练习,我必须使用 3DES 和 OFB 和一些 python 库进行加密,在我的例子中使用 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()

创建一个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))
})();

插件导入cryptojs库,调用3DES和OFB的解密函数,我导入html参数,尝试解密内容并尝试用alert显示,但返回为空。难道我做错了什么?我突然想到,在 JS 中,我没有正确使用输入参数进行基本更改,但我不确定。

PD:我也尝试将 python 库更改为 PyCryptodome,但得到了相同的结果。

【问题讨论】:

    标签: javascript python cryptography cryptojs


    【解决方案1】:

    Key 和 IV 在 Python 代码中以 ASCII(或 UTF-8)编码,因此必须在 CryptoJS 代码中使用适当的 encoder(Latin1 或 Utf8)。

    此外,CryptoJS 应用PKCS7 padding by default,甚至适用于OFB 等流密码模式。因此,必须明确禁用填充:

    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));
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"&gt;&lt;/script&gt;

    上面的代码 sn-p 使用 Python 代码中的值作为密文、密钥和 IV,并成功地用它们解密了密文。

    如果 CryptoJS 代码中的密文、密钥和 iv 值正确地从 HTML 派生,那么在这些修复之后,您的代码也应该成功解密密文。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多