按位異或加密字符串,字符串加解密都是該函數
缺陷是加密密鑰使用的字符最好不要出現需要加密的字符串中的字符,一旦出現原字符與加密字符一樣額情況,異或結果為0,導致不能還原字符串,可以考慮更改算法避免這種情況
import _ from 'lodash'export const xor = str => {const key = '^%@*$' // 加密密鑰,這個隨便寫const strlength = str.lengthconst keylength = key.lengthconst 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
}