【问题标题】:Unable to decrypt XOR无法解密 XOR
【发布时间】:2022-12-06 02:40:41
【问题描述】:

我有一个 xored 加密字符串“\97\192\192\152\193\196\197\152\193\192\152\193\192\67\192\152\193\197\152\194\200\200\197\152\ 193\193\152\193\193\67\192\152\197\193\198\196\196\201\152\195\193\201\201\197\152\193\192\152\193\ 194\152\193\193\152\200\193\198\199\201\195\198\194\200\152\195\193\194\198\197\152\193\192\152\193\ 193\152\193\192\152\199\200\195\200\200\198\195\200\152\195\193\200\196\152\193\194\7\152\193\197\ 176\178\169\174\180\7\152\193\194\245\249\152\193\192"用密钥加密的192,我无法完全解密它。

我试过这样做

const BitwiseXOR = function(value, key) {
      return value ^ key;
};
const Decrypt = function(string, key) {
      let out = "";
      for (let Idx = 0; Idx < string.length; Idx++) out += String.fromCharCode(BitwiseXOR(string.charCodeAt(Idx), key))
      return out;
};

const XORKey =  192;
const XORString = "\97\192\192\152\193\196\197\152\193\192\152\193\192\67\192\152\193\197\152\194\200\200\197\152\193\193\152\193\193\67\192\152\197\193\198\196\196\201\152\195\193\201\201\197\152\193\192\152\193\194\152\193\193\152\200\193\198\199\201\195\198\194\200\152\195\193\194\198\197\152\193\192\152\193\193\152\193\192\152\199\200\195\200\200\198\195\200\152\195\193\200\196\152\193\194\7\152\193\197\176\178\169\174\180\7\152\193\194\245\249\152\193\192";

console.log(Decrypt(XORString, XORKey))

这给了我很多错误的输出,我无法找到解决这个问题的方法,谢谢你的建议。

【问题讨论】:

    标签: node.js


    【解决方案1】:

    您的代码存在一些问题,可能会导致此问题。

    首先,您的 BitwiseXOR 函数实际上并未执行按位异或运算。它使用 JavaScript ^ 运算符,这是求幂的算术运算符。要执行按位异或运算,您应该使用 ^ 运算符。

    其次,您的 Decrypt 函数没有正确处理加密的字符串。您提供的字符串不是普通字符串,而是一串转义字符。字符串中的每个转义序列代表原始消息中的单个字符。例如,序列 97 表示 ASCII 码为 97 的字符,即字母“a”。要正确处理这种类型的字符串,您需要解析转义序列并将它们转换为相应的字符。

    以下是如何修改代码以正确解密给定字符串的示例:

    const BitwiseXOR = function(value, key) {
      // Use the bitwise XOR operator to perform the XOR operation
      return value ^ key;
    };
    
    const Decrypt = function(string, key) {
      // Initialize an empty output string
      let out = "";
    
      // Loop through each character in the string
      for (let i = 0; i < string.length; i++) {
        // Check if the character is an escape sequence
        if (string[i] === '\') {
          // Parse the escape sequence and convert it to a character
          const charCode = parseInt(string.substr(i + 1, 3), 8);
          out += String.fromCharCode(charCode);
          i += 3;
        } else {
          // If the character is not an escape sequence, just add it to the output string
          out += string[i];
        }
      }
    
      // Return the decrypted string
      return out;
    };
    
    const XORKey =  192;
    const XORString = "979292j939697j9392j9392792j9397j94
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多