按位异或加密字符串,字符串加解密都是该函数
缺陷是加密密钥使用的字符最好不要出现需要加密的字符串中的字符,一旦出现原字符与加密字符一样额情况,异或结果为0,导致不能还原字符串,可以考虑更改算法避免这种情况
import _ from 'lodash'
export const xor = str => {
const key = '^%@*$' // 加密密钥,这个随便写
const strlength = str.length
const keylength = key.length
const repeatkey = _.repeat(key, _.floor(strlength / keylength) + 1)
let newstr = ''
for (let index = 0; index < strlength; index++) {
const n = str.charCodeAt(index) ^ repeatkey.charCodeAt(index)
newstr += String.fromCharCode(n)
}
return newstr
}