使用 AND 和 OR 运算符实现的加密程序该如何解密?

247 天前
 ENIAC

AND 和 OR 运算是不是没有逆运算呀?那下面这种程序要如何写一个 decode 算法呢?是不是解密有另外的一个码表?有没有吊大的可以给一下解题思路?

const codeTable = 'a=9-vBc3C0iDWJE4gFumGYsHoyIe7KxVLkXM6ZNqbOtdPwfQzh12jR5lS8nT_pUAr'

const encode = json => {
    let result = ''
    for (let i = 0; i < json.length;) {
        const a = json.charCodeAt(i++)
        const b = json.charCodeAt(i++)
        const c = json.charCodeAt(i++)
        const d = a >> 2
        const e = ((3 & a) << 4) | (b >> 4)
        let f = ((b & 15) << 2) | (c >> 6)
        let g = c & 63
        isNaN(b) ? f = g = 64 : isNaN(c) && (g = 64)
        result = result + codeTable[d] + codeTable[e] + codeTable[f] + codeTable[g]
    }
    return result
}

const decode = cipher => {

}
1223 次点击
所在节点    程序员
12 条回复
pagxir
247 天前
这不就是 base64 编码么,只是把码表换而已。
gwy15
247 天前
这就是一个自定义了映射的 base64 编码
momocraft
247 天前
假定可以解密而且代码是对的,因为我不知道这里用 charCodeAt 是不是对
注意到每次循环把 json 的 3 字符编码成 result4 个字符
所以把 \x00\x00\x00 ~ \xff\xff\xff 跑一遍就得到码表了
NoOneNoBody
247 天前
首先,有个词叫“位运算”,如果不懂这个,那不知道 base64 的实现原理也就正常了
位运算虽然在高级语言使用场合不多,但它是编程的基础之一,也是各种逻辑算式在二进制整数的体现(原理),不懂的话建议补充学习一下
laozhoubuluo
247 天前
找 ChatGPT 写了个解密函数,测试是通过的。

```
const decode = cipher => {
let result = ''
for (let i = 0; i < cipher.length;) {
const d = codeTable.indexOf(cipher[i++])
const e = codeTable.indexOf(cipher[i++])
const f = codeTable.indexOf(cipher[i++])
const g = codeTable.indexOf(cipher[i++])
const a = (d << 2) | (e >> 4)
const b = ((e & 15) << 4) | (f >> 2)
const c = ((f & 3) << 6) | g
result += String.fromCharCode(a)
if (f !== 64) {
result += String.fromCharCode(b)
}
if (g !== 64) {
result += String.fromCharCode(c)
}
}
return result
}
```
ENIAC
247 天前
原来如此,没有想到 base64 那里去,乍一看还是挺相似的,感谢感谢!
ENIAC
247 天前
@laozhoubuluo 有用过 chatgpt 写,但是我拿着密文解出来之后部分字符是乱码的
laozhoubuluo
247 天前
@ENIAC 仔细测试了一下发现了个问题,encode 似乎并不能处理中文。

例如:
```
const json = '{"Username":"测试","UID":20}';
const encoded = encode(json);
console.log("编码内容:", encoded);
```

返回结果是(空格是我添加以便区分的):
编码内容: x10Y75Y1eNBwyuCnC undefined undefined YCXzXYGZvCMb1W3jr

那这样的话如果输入内容包含中文,那么势必会出现乱码的情况的。
hyperbin
247 天前
除非你是密码学专业,否则永远不要使用自己写的加密
sherlockwhite
247 天前
```
const decode = cipher => {
let result = ''
for(let i = 0; i < cipher.length; i+=4) {
const a = codeTable.indexOf(cipher[i])
const b = codeTable.indexOf(cipher[i+1])
const c = codeTable.indexOf(cipher[i+2])
const d = codeTable.indexOf(cipher[i+3])

const n1 = a << 2 | b >> 4
const n2 = (b & 15) << 4 | c >> 2
const n3 = (c & 3) << 6 | d
result += String.fromCharCode(n1)
if(c != 64) {
result += String.fromCharCode(n2)
}
if(d != 64) {
result += String.fromCharCode(n3)
}
}
return result
}
```
zbinlin
247 天前
@laozhoubuluo 这肯定要先转成 utf8 字节再 encode 才行
mcfog
247 天前
b64Table='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
decode = s=>atob(Array.from(s).map(c=>b64Table[codeTable.indexOf(c)]).join(''))
console.log(decode(encode('Hello World!')))

没啥毛病,中文是因为整个过程用 charcode 当取字节用了,但实际上 charcode 取的是 unicode codepoint

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/971232

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX