使用C语言实现仿射密码和Vigenere加解密,要求密码算法采用模块化设计,一般密码算法要包括主函数、设置密钥函数、加密函数和解密函数等几个部分,示例
时间: 2023-06-03 18:02:40 浏览: 147
仿射密码 加解密 C语言
5星 · 资源好评率100%
代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1000
#define MODULO 26
void set_key(char *key) {
printf("Please enter the key: ");
scanf("%s", key);
}
void affine_encrypt(char *plaintext, char *ciphertext, int a, int b) {
int i, len;
len = strlen(plaintext);
for (i = 0; i < len; i++) {
if (plaintext[i] >= 'a' && plaintext[i] <= 'z') {
ciphertext[i] = (a * (plaintext[i] - 'a') + b) % MODULO + 'a';
} else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') {
ciphertext[i] = (a * (plaintext[i] - 'A') + b) % MODULO + 'A';
} else {
ciphertext[i] = plaintext[i];
}
}
ciphertext[i] = '\0';
}
void affine_decrypt(char *ciphertext, char *plaintext, int a, int b) {
int i, len, t;
len = strlen(ciphertext);
for (i = 0; i < len; i++) {
if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') {
t = (ciphertext[i] - 'a' - b) % MODULO;
while (t < 0) {
t += MODULO;
}
plaintext[i] = (a * t) % MODULO + 'a';
} else if (ciphertext[i] >= 'A' && ciphertext[i] <= 'Z') {
t = (ciphertext[i] - 'A' - b) % MODULO;
while (t < 0) {
t += MODULO;
}
plaintext[i] = (a * t) % MODULO + 'A';
} else {
plaintext[i] = ciphertext[i];
}
}
plaintext[i] = '\0';
}
void vigenere_encrypt(char *plaintext, char *ciphertext, char *key) {
int i, j, len_plaintext, len_key;
len_plaintext = strlen(plaintext);
len_key = strlen(key);
for (i = 0; i < len_plaintext; i++) {
if (plaintext[i] >= 'a' && plaintext[i] <= 'z') {
ciphertext[i] = 'a' + (plaintext[i] - 'a' + key[i % len_key] - 'a') % MODULO;
} else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') {
ciphertext[i] = 'A' + (plaintext[i] - 'A' + key[i % len_key] - 'a') % MODULO;
} else {
ciphertext[i] = plaintext[i];
}
}
ciphertext[i] = '\0';
}
void vigenere_decrypt(char *ciphertext, char *plaintext, char *key) {
int i, j, len_ciphertext, len_key;
len_ciphertext = strlen(ciphertext);
len_key = strlen(key);
for (i = 0; i < len_ciphertext; i++) {
if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') {
plaintext[i] = 'a' + (ciphertext[i] - 'a' - key[i % len_key] + 'a') % MODULO;
while (plaintext[i] < 'a') {
plaintext[i] += MODULO;
}
} else if (ciphertext[i] >= 'A' && ciphertext[i] <= 'Z') {
plaintext[i] = 'A' + (ciphertext[i] - 'A' - key[i % len_key] + 'a') % MODULO;
while (plaintext[i] < 'A') {
plaintext[i] += MODULO;
}
} else {
plaintext[i] = ciphertext[i];
}
}
plaintext[i] = '\0';
}
int main() {
char plaintext[MAX_SIZE], ciphertext[MAX_SIZE], decrypted[MAX_SIZE], key[MAX_SIZE];
int a, b;
printf("[Affine Cipher]\n");
printf("Please enter the plaintext: ");
scanf("%s", plaintext);
printf("Please enter the coefficient a: ");
scanf("%d", &a);
printf("Please enter the constant b: ");
scanf("%d", &b);
affine_encrypt(plaintext, ciphertext, a, b);
printf("The ciphertext is: %s\n", ciphertext);
affine_decrypt(ciphertext, decrypted, a, b);
printf("The decrypted plaintext is: %s\n", decrypted);
printf("\n[Vigenere Cipher]\n");
printf("Please enter the plaintext: ");
scanf("%s", plaintext);
set_key(key);
vigenere_encrypt(plaintext, ciphertext, key);
printf("The ciphertext is: %s\n", ciphertext);
vigenere_decrypt(ciphertext, decrypted, key);
printf("The decrypted plaintext is: %s\n", decrypted);
return 0;
}
```
阅读全文