JS凯撒密码
加密
function jiami(str, num) { var newStr = ""; for (let i = 0; i < str.length; i++) { if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) { newStr += String.fromCharCode((str.charCodeAt(i) - 65 + num + 26) % 26 + 65) } else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) { newStr += String.fromCharCode((str.charCodeAt(i) - 97 + num + 26) % 26 + 97) } //特殊符号不做处理 else newStr += String.fromCharCode(str.charCodeAt(i)); } // console.log(newStr); return newStr; } var result1 = jiami("zlf.zlf.666", 3); console.log(result1);
解密
function jiemi(str, num) { var newStr = ""; for (let i = 0; i < str.length; i++) { if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) { newStr += String.fromCharCode((str.charCodeAt(i) - 65 - num + 26) % 26 + 65) } else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) { newStr += String.fromCharCode((str.charCodeAt(i) - 97 - num + 26) % 26 + 97) } //特殊符号不做处理 else newStr += String.fromCharCode(str.charCodeAt(i)); } return newStr; } var result2 = jiemi("coi.coi.666", 3); console.log(result2);
标签:function,&&,JS,密码,str,charCodeAt,凯撒 From: https://www.cnblogs.com/Im-Victor/p/16641890.html