c语言 编写一个系统用于base加密解密,凯撒加密解密
时间: 2025-06-09 07:05:01 浏览: 24
在C语言中编写一个简单的凯撒密码(Caesar Cipher)加密解密系统,你可以创建两个函数,一个用于加密,另一个用于解密,通过将字母表中的每个字符移动固定数量的位置来实现。以下是一个基础的示例:
```c
#include <stdio.h>
#include <ctype.h>
// 函数声明
void caesar_encrypt(char* plaintext, int shift);
void caesar_decrypt(char* ciphertext, int shift);
// 凯撒加密函数
void caesar_encrypt(char* plaintext, int shift) {
for (int i = 0; plaintext[i] != '\0'; i++) {
if (isalpha(plaintext[i])) { // 检查是否是字母
if (islower(plaintext[i])) {
plaintext[i] = ((plaintext[i] - 'a' + shift) % 26) + 'a';
} else {
plaintext[i] = ((plaintext[i] - 'A' + shift) % 26) + 'A';
}
}
}
}
// 凯撒解密函数
void caesar_decrypt(char* ciphertext, int shift) {
caesar_encrypt(ciphertext, -shift); // 加密时用shift,解密时用-shift
}
int main() {
char input[100], encrypted[100];
printf("请输入明文:");
fgets(input, sizeof(input), stdin);
input[strlen(input)-1] = '\0'; // 去除fgets读取的换行符
int shift;
printf("请输入移位数:");
scanf("%d", &shift);
caesar_encrypt(input, shift);
printf("加密后的文本:%s\n", input);
caesar_decrypt(encrypted, shift); // 使用同一个移位数解密
printf("解密后的文本:%s\n", encrypted);
return 0;
}
```
在这个例子中,用户会被提示输入明文和移位数,然后程序会分别对明文进行加密和解密,并显示结果。
阅读全文
相关推荐




















