精易论坛

标题: 文本加密解密,自定义加密后密文内容 [打印本页]

作者: BanBy    时间: 2024-9-7 23:23
标题: 文本加密解密,自定义加密后密文内容
根据自定义密文内容,进行加密,

[JavaScript] 纯文本查看 复制代码
class BeastTranslator {
    constructor() {
        // 构造函数,初始化时设置字符映射表,自定义密文内容,可随意调整,(建议三个字符加一个符号)
        this.sounds = ['精', '易', '论', '坛'];
    }

    beastToHex(beastStr) {
        let hexStr = "";
        for (let i = 0; i < beastStr.length; i += 2) {
            // 遍历输入字符串,每次处理两个字符
            let j = this.sounds.findIndex(item => item === beastStr);
            let k = this.sounds.findIndex(item => item === beastStr[i + 1]);
            k = (j * 4 + k - i / 2 % 16 + 16) % 16;
            // 计算对应的十六进制值
            hexStr += k.toString(16).toUpperCase();
            // 将十六进制值转换为大写字符串并添加到结果中
        }
        return hexStr;
    }

    hexToBeast(hexStr) {
        let buffer = "";
        for (let i = 0; i < hexStr.length; i++) {
            let k = (parseInt(hexStr, 16) + i % 16) % 16;
            // 计算对应的字符索引
            buffer += this.sounds[Math.floor(k / 4)] + this.sounds[k % 4];
            // 根据索引从字符映射表中获取字符并添加到缓冲区中
        }
        return buffer;
    }

    hexToText(hexStr) {
        let buffer = "";
        for (let i = 0; i < hexStr.length; i += 4) {
            // 每四个字符一组进行处理
            buffer += String.fromCharCode(parseInt(hexStr.substr(i, 4), 16));
            // 根据十六进制代码获取对应的字符并添加到缓冲区中
        }
        return buffer;
    }

    textToHex(str) {
        let hexStr = "";
        for (let i = 0; i < str.length; i++) {
            // 遍历输入字符串
            hexStr += ("0000" + str.charCodeAt(i).toString(16)).substr(-4).toUpperCase();
            // 将字符的 ASCII 码转换为十六进制字符串并格式化为四位,添加到结果中
        }
        return hexStr;
    }

    setSoundsFromBeast(beastStr) {
        this.sounds[0] = beastStr[2];
        // 设置字符映射表的第一个字符
        this.sounds[1] = beastStr[1];
        this.sounds[2] = beastStr[beastStr.length - 1];
        this.sounds[3] = beastStr[0];
    }

    setSounds(str) {
        this.sounds[0] = str[0];
        this.sounds[1] = str[1];
        this.sounds[2] = str[2];
        this.sounds[3] = str[str.length - 1];
    }

    getSoundsStr() {
        return this.sounds.join("");
        // 返回字符映射表的字符串表示
    }

    getSounds() {
        return this.sounds;
        // 返回字符映射表
    }

    getSoundsForBeast() {
        return [this.sounds[3], this.sounds[1], this.sounds[0], this.sounds[2]];
        // 返回用于特定处理的字符映射表顺序
    }

    parseToNormal(beastStr) {
        this.setSoundsFromBeast(beastStr);
        // 根据输入字符串设置字符映射表
        let hexCode = this.beastToHex(beastStr.substr(3, beastStr.length - 4));
        // 将输入字符串的一部分转换为十六进制
        return this.hexToText(hexCode);
        // 将十六进制转换为普通文本字符串并返回
    }

    parseToBeast(normalStr, soundMap = null) {
        if (soundMap) {
            this.setSounds(soundMap);
            // 如果有提供自定义字符映射表,则设置字符映射表
        }
        let hexCode = this.textToHex(normalStr);
        // 将普通文本字符串转换为十六进制
        let beastSound = this.hexToBeast(hexCode);
        // 将十六进制转换为特定形式的字符串
        let soundMapForBeast = this.getSoundsForBeast();
        // 获取用于特定处理的字符映射表顺序
        return [soundMapForBeast[0], soundMapForBeast[1], soundMapForBeast[2], beastSound, soundMapForBeast[3]].join("");
        // 返回处理后的字符串
    }
}

function testTranslator() {
    let translator = new BeastTranslator();
    // 待加密文本
    let strToEncode = "精易论坛";
    console.log('-----------------------------------------');
    let encodedStr = translator.parseToBeast(strToEncode);
    console.log("加密后:", encodedStr,'\n');
    let decodedStr = translator.parseToNormal(encodedStr);
    console.log("解密后:", decodedStr);
    console.log('-----------------------------------------');
}

testTranslator();

作者: kiss0459    时间: 2024-9-8 00:00
文本加密解密,自定义加密后密文内容
作者: 虎子666    时间: 2024-9-8 00:36
有点意思
作者: renhe2018    时间: 2024-9-8 02:43
这个感觉不错。
作者: pipicool    时间: 2024-9-8 04:29
学习一下
作者: cqcc    时间: 2024-9-8 05:08
先知道加密结果,再逆向到原文?
作者: year1970    时间: 2024-9-8 07:51
感谢分享
作者: 一指温柔    时间: 2024-9-8 08:42
感谢分享
作者: GawrGura    时间: 2024-9-8 09:07
感谢分享 感谢分享
作者: 小虎来了    时间: 2024-9-8 09:25
感谢分享
作者: editabc    时间: 2024-9-8 14:24
支持~~~~~~~~~~~~~~~~~~~
作者: 何浩文    时间: 2024-9-8 14:26
支持开源~!感谢分享
作者: shuya1    时间: 2024-9-8 16:58
支持开源~!感谢分享
作者: 亿万    时间: 2024-9-8 21:15

支持开源~!感谢分享
作者: ctry78985    时间: 2024-9-8 21:51
感谢分享
作者: 396384183    时间: 2024-9-8 22:04
感谢分享
作者: renhe2018    时间: 2024-9-9 03:04
再来研究研究。
作者: kyo9766    时间: 2024-9-9 08:20
学习一下加密算法,感谢分享
作者: reveriexue    时间: 2024-9-9 08:21
支持开源~!感谢分享

作者: wuqingg    时间: 2024-9-9 08:40
感谢分享,很给力!~
作者: please    时间: 2024-9-11 09:38
感谢分享,支持开源!!!
作者: 2446789312    时间: 2024-9-11 19:14
这个不是会被猜到么?
作者: LDL520    时间: 2024-9-21 04:00
感谢分享,很给力!
作者: 熊不熊    时间: 2024-12-4 14:37
感谢分享,很给力!~




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4