#include <stdio.h>
#include <string.h>
#include <ctype.h>
// 换字式密码加密函数
void substitutionEncrypt(char *text, const char *key) {
int len = strlen(text);
int i=0;
for (i = 0; i < len; i++) {
if (islower(text[i])) {
text[i] = key[text[i] - 'a'];
} else if (isupper(text[i])) {
text[i] = toupper(key[text[i] - 'A']);
}
}
}
// 换字式密码解密函数
void substitutionDecrypt(char *text, const char *key) {
char reverseKey[26];
int i=0;
for (i = 0; i < 26; i++) {
if (islower(key[i])) {
reverseKey[key[i] - 'a'] = 'a' + i;
} else {
reverseKey[key[i] - 'A'] = 'A' + i;
}
}
int len = strlen(text);
for (i = 0; i < len; i++) {
if (islower(text[i])) {
text[i] = reverseKey[text[i] - 'a'];
} else if (isupper(text[i])) {
text[i] = toupper(reverseKey[text[i] - 'A']);
}
}
}
int main() {
char message[] = "Hello, World!";
char key[] = "zyxwvutsrqponmlkjihgfedcba";
printf("原始消息: %s\n", message);
substitutionEncrypt(message, key);
printf("加密后的消息: %s\n", message);
substitutionDecrypt(message, key);
printf("解密后的消息: %s\n", message);
return 0;
}