We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
前段时间看了阮一峰老师的 《XOR 加密简介》, 突发奇想写了一个基于XOR(异或) 加密解密的JavaScript程序
+-------------------------------------+ | 待 加 密 字 符 串 | +-------------+----+------------------+ | | +-----------------------v----v----------------------------+ | | | 将 字 符 串 每 个 字 符 转 成 ascll 码 | | | +-----------------------+----+----------------------------+ | | +-----------------------v----v----------------------------+ | | | 将 ascll 码 进 行 xor ( 异 或 ) 加 密 | | | +-----------------------+----+----------------------------+ | | +-----------------------v----v----------------------------+ | | | 将 加 密 的 ascll 码 转 成 自 定 义 进 制 的 字 符 | | | +-----------------------+----+----------------------------+ | | +-------------v----v------------------+ | 加 密 后 的 字 符 串 | +-------------------------------------+
+-------------------------------------+ | 待 解 密 字 符 串 | +-------------+----+------------------+ | | +-----------------------v----v----------------------------+ | | | 将 字 符 串 每 个 字 符 转 成 ascll 码 | | | +-----------------------+----+----------------------------+ | | +-----------------------v----v----------------------------+ | | | 将 ascll 码 进 行 xor ( 异 或 ) | | | +-----------------------+----+----------------------------+ | | +-----------------------v----v----------------------------+ | | | 将 解 密 的 ascll 码 转 成 对 应 进 制 的 字 符 | | | +-----------------------+----+----------------------------+ | | +-------------v----v------------------+ | 解 密 后 的 字 符 串 | +-------------------------------------+ Close
/** * encrypto 加密程序 * @param {Strng} str 待加密字符串 * @param {Number} xor 异或值 * @param {Number} hex 加密后的进制数 * @return {Strng} 加密后的字符串 */ function encrypto( str, xor, hex ) { if ( typeof str !== 'string' || typeof xor !== 'number' || typeof hex !== 'number') { return; } let resultList = []; hex = hex <= 25 ? hex : hex % 25; for ( let i=0; i<str.length; i++ ) { // 提取字符串每个字符的ascll码 let charCode = str.charCodeAt(i); // 进行异或加密 charCode = (charCode * 1) ^ xor; // 异或加密后的字符转成 hex 位数的字符串 charCode = charCode.toString(hex); resultList.push(charCode); } let splitStr = String.fromCharCode(hex + 97); let resultStr = resultList.join( splitStr ); return resultStr; }
/** * decrypto 解密程序 * @param {Strng} str 待加密字符串 * @param {Number} xor 异或值 * @param {Number} hex 加密后的进制数 * @return {Strng} 加密后的字符串 */ function decrypto( str, xor, hex ) { if ( typeof str !== 'string' || typeof xor !== 'number' || typeof hex !== 'number') { return; } let strCharList = []; let resultList = []; hex = hex <= 25 ? hex : hex % 25; // 解析出分割字符 let splitStr = String.fromCharCode(hex + 97); // 分割出加密字符串的加密后的每个字符 strCharList = str.split(splitStr); for ( let i=0; i<strCharList.length; i++ ) { // 将加密后的每个字符转成加密后的ascll码 let charCode = parseInt(strCharList[i], hex); // 异或解密出原字符的ascll码 charCode = (charCode * 1) ^ xor; let strChar = String.fromCharCode(charCode); resultList.push(strChar); } let resultStr = resultList.join(''); return resultStr; }
let s1 = 'hello world'; // 加密 s1 = encrypto(s1, 123, 25); console.log('s1=', s1); // s1= jz15znznzkz3gzczkz9znz16 // 解密 let s2 = decrypto(s1, 123, 25); console.log('s2=', s2); // s2= hello world
The text was updated successfully, but these errors were encountered:
对比了一下base64-xor。base64-xor时间慢些,但是加密后的字符串要短些:WVdfXV0TRl1BXVY=
Sorry, something went wrong.
感谢 挺实用的小工具
No branches or pull requests
前言
前段时间看了阮一峰老师的 《XOR 加密简介》, 突发奇想写了一个基于XOR(异或) 加密解密的JavaScript程序
具体步骤
加密
解密
字符串加密
字符串解密
测试
The text was updated successfully, but these errors were encountered: