恺撒密码function rot13(str) { return str; } rot13("SERR PBQR PNZC");
时间: 2023-07-16 19:15:01 浏览: 41
恺撒密码是一种简单的替换密码,将字母表中的每个字母向后移动13个位置。
在这个JavaScript函数中,我们可以实现恺撒密码解密:
```
function rot13(str) {
var newStr = "";
for (var i = 0; i < str.length; i++) {
var charCode = str.charCodeAt(i);
// 如果是大写字母,将其向后移动13个位置
if (charCode >= 65 && charCode <= 90) {
charCode = ((charCode - 65 + 13) % 26) + 65;
}
// 如果是小写字母,将其向后移动13个位置
else if (charCode >= 97 && charCode <= 122) {
charCode = ((charCode - 97 + 13) % 26) + 97;
}
newStr += String.fromCharCode(charCode);
}
return newStr;
}
rot13("SERR PBQR PNZC"); // "FREE CODE CAMP"
```
在这个例子中,我们将字符串 "SERR PBQR PNZC" 作为参数传递给 `rot13` 函数,函数将返回解密后的字符串 "FREE CODE CAMP"。
相关问题
rot13加密算法c
以下是使用C语言实现Rot13加密算法的代码示例:
```c
#include <stdio.h>
#include <ctype.h>
char rot13(char c) {
if (isalpha(c)) {
if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M')) {
c += 13;
} else {
c -= 13;
}
}
return c;
}
void rot13_encrypt(char *str) {
while (*str) {
*str = rot13(*str);
str++;
}
}
void rot13_decrypt(char *str) {
rot13_encrypt(str);
}
int main() {
char str[100];
printf("请输入要加密的字符串:");
fgets(str, 100, stdin);
rot13_encrypt(str);
printf("加密后的字符串为:%s", str);
rot13_decrypt(str);
printf("解密后的字符串为:%s", str);
return 0;
}
```
c语言rot13加密算法
很抱歉,提供的引用内容中并没有C语言实现的Rot13加密算法。不过,Rot13加密算法的实现比较简单,可以用C语言轻松实现。下面是一个C语言实现的Rot13加密算法的例子:
```c
#include <stdio.h>
#include <string.h>
void rot13(char *input, char *output) {
int i;
for (i = 0; i < strlen(input); i++) {
char c = input[i];
if (c >= 'a' && c <= 'm') {
c += 13;
} else if (c >= 'A' && c <= 'M') {
c += 13;
} else if (c >= 'n' && c <= 'z') {
c -= 13;
} else if (c >= 'N' && c <= 'Z') {
c -= 13;
}
output[i] = c;
}
output[i] = '\0';
}
int main() {
char input[100], output[100];
printf("请输入要加密的字符串:");
scanf("%s", input);
rot13(input, output);
printf("加密后的字符串为:%s\n", output);
return 0;
}
```