#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define ROW 6
#define COL 6
#define MAX_LEN 1000
char chessboard[ROW][COL] = {
{'a', 'b', 'c', 'd', 'e', 'f'},
{'g', 'h', 'i', 'j', 'k', 'l'},
{'m', 'n', 'o', 'p', 'q', 'r'},
{'s', 't', 'u', 'v', 'w', 'x'},
{'y', 'z', '0', '1', '2', '3'},
{'4', '5', '6', '7', '8', '9'}
};
void encrypt(char *plaintext, char *ciphertext) {
int i, j, k, x, y, len;
len = strlen(plaintext);
k = 0;
for (i = 0; i < len; i++) {
if (isalpha(plaintext[i])) {
plaintext[i] = tolower(plaintext[i]);
for (j = 0; j < ROW; j++) {
for (k = 0; k < COL; k++) {
if (chessboard[j][k] == plaintext[i]) {
x = j;
y = k;
break;
}
}
}
if (x == ROW - 1) {
x = 0;
} else {
x++;
}
ciphertext[i] = chessboard[x][y];
} else {
ciphertext[i] = plaintext[i];
}
}
ciphertext[i] = '\0';
}
void decrypt(char *ciphertext, char *plaintext) {
int i, j, k, x, y, len;
len = strlen(ciphertext);
k = 0;
for (i = 0; i < len; i++) {
if (isalpha(ciphertext[i])) {
ciphertext[i] = tolower(ciphertext[i]);
for (j = 0; j < ROW; j++) {
for (k = 0; k < COL; k++) {
if (chessboard[j][k] == ciphertext[i]) {
x = j;
y = k;
break;
}
}
}
if (x == 0) {
x = ROW - 1;
} else {
x--;
}
plaintext[i] = chessboard[x][y];
} else {
plaintext[i] = ciphertext[i];
}
}
plaintext[i] = '\0';
}
int main() {
char plaintext[MAX_LEN], ciphertext[MAX_LEN], decrypted[MAX_LEN];
printf("请输入明文:");
fgets(plaintext, MAX_LEN, stdin);
plaintext[strlen(plaintext)-1] = '\0';
encrypt(plaintext, ciphertext);
printf("加密后的密文:%s\n", ciphertext);
decrypt(ciphertext, decrypted);
printf("解密后的明文:%s\n", decrypted);
return 0;
}