为了防止信息被别人轻易窃取,需要把电码明文通过加密方式变换成为密文。输入一个以回车符为结束标志的字符串(少于80个字符),再输入一个整数offset,用凯撒密码将其加密后输出。恺撒密码是一种简单的替换加密技术,将明文中的所有字母都在字母表上偏移offset位后被替换成密文,当offset大于零时,表示向后偏移;当offset小于零时,表示向前偏移。 Input 第一行输入需要进行的解密/加密操作,0表示解密,1表示加密 第二行给出一个以回车结束的非空字符串(少于80个字符) (密文或明文) 第三行输入一个整数offset。 Output 输出加密/解密后的结果字符串。
时间: 2024-03-17 13:42:39 浏览: 25
以下是一个实现凯撒密码加密/解密的C语言代码示例:
```c
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
int operation; // 0表示解密,1表示加密
char input[80];
int offset;
scanf("%d", &operation);
getchar(); // 读取换行符
fgets(input, 80, stdin);
input[strcspn(input, "\n")] = '\0'; // 去除末尾的换行符
scanf("%d", &offset);
int len = strlen(input);
for (int i = 0; i < len; i++) {
if (isalpha(input[i])) { // 只对字母进行加密/解密
int c = input[i];
if (isupper(c)) { // 大写字母
c = (c - 'A' + offset + 26) % 26 + 'A'; // 加密/解密
} else { // 小写字母
c = (c - 'a' + offset + 26) % 26 + 'a'; // 加密/解密
}
input[i] = c;
}
}
if (operation == 0) { // 解密
offset = -offset;
}
printf("%s\n", input);
return 0;
}
```
该程序会读取用户输入的字符串和偏移量,对字符串中的字母进行凯撒密码加密或解密,然后输出加密/解密后的结果字符串。注意,该程序只对字母进行加密/解密,其他字符不受影响。