七猫小说 chapterdata解密

chapterdata解密代码

// 引入CryptoJS库
const CryptoJS = require('crypto-js');
const fs = require('fs');
/**
 * 自定义stringify函数
 * @param {Object} wordArray - 包含words和sigBytes的对象
 * @returns {string} - 转换后的字符串
 */
function custom_stringify(wordArray) {
  const words = wordArray.words;
  const sigBytes = wordArray.sigBytes;
  const chars = [];

  for (let i = 0; i < sigBytes; i++) {
    const bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
    chars.push(String.fromCharCode(bite));
  }

  return chars.join('');
}


/**
 * Base64解码实现
 */
const BASE64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
const REVERSE_MAP = {};

// 初始化反向映射表
for (let i = 0; i < BASE64_CHARS.length; i++) {
  REVERSE_MAP[BASE64_CHARS[i]] = i;
}

function parse_base64(str) {
  // 移除填充字符
  str = str.replace(/[^A-Za-z0-9+/]/g, '');

  const words = [];
  let wordCount = 0;

  // 每4个字符一组处理
  for (let i = 0; i < str.length; i += 4) {
    const chunk = str.slice(i, i + 4);
    let value = 0;

    // 将4个Base64字符转换为24位
    for (let j = 0; j < chunk.length; j++) {
      value = (value << 6) | REVERSE_MAP[chunk[j]];
    }

    // 将24位拆分为3个字节
    for (let j = 0; j < 3 && (i * 3 / 4 + j) < (str.length * 3 / 4); j++) {
      const byte = (value >> (16 - j * 8)) & 0xff;
      words[wordCount >>> 2] |= byte << (24 - (wordCount % 4) * 8);
      wordCount++;
    }
  }

  return new CryptoJS.lib.WordArray.init(words, Math.floor(str.length * 3 / 4));
}

/**
 * 将字符串解析为WordArray格式
 * @param {string} str - 要解析的字符串
 * @returns {Object} - WordArray对象
 */
function parse_str_to_word_array(str) {
  const length = str.length;
  const words = [];

  for (let i = 0; i < length; i++) {
    words[i >>> 2] |= (0xff & str.charCodeAt(i)) << (24 - (i % 4) * 8);
  }

  return new CryptoJS.lib.WordArray.init(words, length);
}



/**
 * 解密章节内容
 * @param {string} encryptedText - 加密的文本,格式为:密文+32位密钥
 * @returns {string} - 解密后的文本
 */
function decrypt_chapter_content(encryptedText) {
  // 获取最后32个字符作为密钥
  const key = encryptedText.slice(-32);
  // const key = "UUQdPWyNaoMzEZ90xziyynyNsjiX5274"
  // 获取除最后32个字符外的其余部分作为密文
  const ciphertext = encryptedText.slice(0, encryptedText.length - 32);

  // 对密钥进行MD5加密
  const md5Key = CryptoJS.MD5(key);
  // 将MD5结果转换为WordArray格式
  const keyWords = new CryptoJS.lib.WordArray.init(md5Key.words, 16);
  //console.log(keyWords);
  // 处理密钥
  const parsedKey = parse_str_to_word_array(CryptoJS.MD5(key).toString())
  console.log(parsedKey);

  const d = atob(ciphertext)
  console.log(d)
  const b64 = parse_base64(d)
  // 使用DES-ECB模式进行解密
  console.log(b64)

  console.log(CryptoJS.enc.Base64.parse(atob(ciphertext)))
  const decrypted = CryptoJS.DES.decrypt({
    ciphertext: CryptoJS.enc.Base64.parse(atob(ciphertext))
  }, parsedKey, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  });


  // 使用自定义stringify函数处理解密结果
    //console.log(decrypted);
    const stringified = custom_stringify(decrypted);
    //console.log(stringified);
    const result = decodeURIComponent(escape(stringified));
    console.log(result);
    return result;
}

// 测试

const data = fs.readFileSync('data.txt', 'utf-8');
const result = decrypt_chapter_content(data);
console.log('解密结果:', result);


module.exports = {
    decrypt_chapter_content
};

你可能感兴趣的:(爬虫,网络爬虫,爬山算法,js,逆向)