// js加密与解密的原生处理方案思考
// 加密
function asencode(str, keys) {
const strLen = str.length
const keysLen = keys.length
let newstr = ''
for (let i = 0; i < strLen; i++) {
const rnum = Math.round(Math.random() * 1000) % keysLen
const rnumx = Number(rnum.toString().slice(-1)) + 1
const ylen = keysLen - rnumx
let tmpnum = str.codePointAt(i)
const y1 = tmpnum % ylen;
tmpnum = (tmpnum - y1) / ylen;
const y2 = tmpnum % ylen;
tmpnum = (tmpnum - y2) / ylen;
newstr += keys[(tmpnum % ylen) + rnumx] + keys[y2 + rnumx] + keys[y1 + rnumx] + keys.charAt(rnum)
}
return newstr
}
// 解密
function asdecode1(str, keys) {
const strLen = str.length
const keysLen = keys.length
let newstr = ''
let j = 0
for (let i = 0; i < strLen / 4; i++) {
const rnum = keys.indexOf(str[j + 3])
const rnumx = Number(rnum.toString().slice(-1)) + 1
const ylen = keysLen - rnumx
newstr += String.fromCodePoint(
(keys.indexOf(str[j++]) - rnumx) * ylen * ylen +
(keys.indexOf(str[j++]) - rnumx) * ylen +
(keys.indexOf(str[j++]) - rnumx)
)
j++
}
return newstr
}
// 解密(改进)
function asdecode(str, keys) {
const strLen = str.length
const keysLen = keys.length
let newstr = ''
for (let i = 0; i < strLen; i++) {
const rnum = keys.indexOf(str[i + 3])
const rnumx = Number(rnum.toString().slice(-1)) + 1
const ylen = keysLen - rnumx
newstr += String.fromCodePoint(
(keys.indexOf(str[i++]) - rnumx) * ylen * ylen +
(keys.indexOf(str[i++]) - rnumx) * ylen +
(keys.indexOf(str[i++]) - rnumx)
)
}
return newstr
}
// 随机打乱字符串
function asnewkey(str) {
let newstr = [];
str.split('').forEach((item, index, array) => {
newstr.splice(Math.round(Math.random() * newstr.length), 0, item);
});
return newstr.join('');
}
const xx = {
askey: 'VW&o*8-9XYvwxp+qTUrsPQRSjtuy4f0123ghi7deABZaCDEHIJ!KL@MNFG56Ok#lm$bc%nz',// 服务端原始秘钥=>最高机密
userkey: 'n&o*8-9VWXHIJjtuvwxy4567Zabc!KL@MNOYp+qrsPQRSTUdef0123ghiABCDEFGk#lm$%z',// 客户端原始秘钥
keymy: '12ovdd-701&J77YE77SiggYa11rA3g@xf0EXf0Hrf01I3gAL01hsgh8lghqa12vv01Ys7dj-hiQmghPpiirh11+v22T100qf23NQ3gm&3g$2hiVCddW723Ak7dKTi77t3gCx7d@F12ZA12Lvf0WI77vFhi+M3gw2i7r$01BY12C03gJ2ghL312HOde#HhiWM7d-E12$V77qFhh+*ffYX00ws01mei79h23VKdeZy3g3Zffu5ffy5010sf0fz23ikgh-a00$ff0c4deT7ff$%ffbri7tN', // 通信秘钥
}
const userkey=asnewkey(xx.askey)
console.log(666.1001, '生成原始秘钥:', userkey)
const keymy = asencode(userkey, xx.askey)
console.log(666.1002, '生成通信秘钥:', keymy)
const mimi = '这是一个密文!OK!123'
console.log(666.1003, '要加密的信息:', mimi)
const mimix = asencode(mimi, userkey)
const mimidata = {
key: keymy,
content: mimix
}
console.log(666.1004, '生成加密信息:', mimidata)
const mimiopenuser = asdecode(mimidata.content, userkey)
console.log(666.1005, '用户解密:', mimiopenuser)
const mimiopenidc = asdecode(mimidata.content, asdecode(mimidata.key, xx.askey))
console.log(666.1006, '服务器解密:', mimiopenidc)
// 用户成功连接服务器
// 服务器发给用户原始秘钥(随机)和通信秘钥,存入缓存
// 用户使用原始秘钥加密信息,附加通信秘钥,与服务器通信
// 服务器解密信息,回复的信息采用用户的通信秘钥,以及加密信息发给用户
// 用户使用原始秘钥解密加密的信息
js加密与解密的原生处理方案思考
于 2023-06-29 16:46:18 首次发布
文章展示了如何使用JavaScript实现字符串的加密和解密功能,包括一个加密函数asencode、两个解密函数asdecode1和asdecode,以及一个用于打乱字符串顺序的辅助函数asnewkey。通过这些函数,可以创建通信秘钥并进行信息的安全传输,确保在用户和服务器之间的数据安全。
摘要由CSDN通过智能技术生成