JS凯撒密码

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);
复制代码

 

posted @   mingruqi  阅读(147)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2020-08-31 HTML禁止右键 选择
2019-08-31 利用ARouter实现组件间通信,解决子模块调用主模块问题
2017-08-31 Android中的Handler的机制与用法详解
2017-08-31 在Android开发中,定时执行任务的3种实现方法
点击右上角即可分享
微信分享提示