位移密码算法(JavaScript)
栏目: JavaScript · 发布时间: 7年前
内容简介:著名的凯撒密码Caesar cipher,又叫移位密码。移位密码也就是密码中的字母会按照指定的数量来做移位。一个常见的案例就是ROT13密码,字母会移位13个位置。由'A' ↔ 'N', 'B' ↔'O',以此类推。
问题:
著名的凯撒密码Caesar cipher,又叫移位密码。
移位密码也就是密码中的字母会按照指定的数量来做移位。
一个常见的案例就是ROT13密码,字母会移位13个位置。由'A' ↔ 'N', 'B' ↔'O',以此类推。
写一个ROT13函数,实现输入加密字符串,输出解密字符串。
要求:
所有的字母都是大写,不要转化任何非字母形式的字符(例如:空格,标点符号),遇到这些特殊字符,就跳过它们。
解答:
function rot13(str) { // LBH QVQ VG!
var start = "A".charCodeAt(0);
var end = "Z".charCodeAt(0);
var strList = str.split("");
var judge, replace;
for(var i = 0; i < str.length; i++){
judge = strList[i].charCodeAt(0);
if(judge <= end && judge >= start){
replace = start + (judge - start + 13) % 26;
strList[i] = String.fromCharCode(replace);
}
}
newStr = strList.join("");
return newStr;
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
链接:
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
RESTful Web Services Cookbook
Subbu Allamaraju / Yahoo Press / 2010-3-11 / USD 39.99
While the REST design philosophy has captured the imagination of web and enterprise developers alike, using this approach to develop real web services is no picnic. This cookbook includes more than 10......一起来看看 《RESTful Web Services Cookbook》 这本书的介绍吧!