用C语言写一个古典密码学算法的加密解密实验,其中包含移位密码,代替密码,置换密码
时间: 2024-01-24 11:05:06 浏览: 78
好的,以下是一个简单的C语言实现古典密码学算法的加密和解密的示例代码,其中包括移位密码,代替密码和置换密码。
移位密码:
```c
#include <stdio.h>
#include <string.h>
void encrypt(char *message, int shift);
void decrypt(char *message, int shift);
int main()
{
char message[100];
int shift;
printf("Enter message to encrypt: ");
fgets(message, 100, stdin);
printf("Enter shift: ");
scanf("%d", &shift);
encrypt(message, shift);
printf("Encrypted message: %s\n", message);
decrypt(message, shift);
printf("Decrypted message: %s\n", message);
return 0;
}
void encrypt(char *message, int shift)
{
int i;
for(i = 0; message[i] != '\0'; i++)
{
if(message[i] >= 'a' && message[i] <= 'z')
{
message[i] = ((message[i] - 'a') + shift) % 26 + 'a';
}
else if(message[i] >= 'A' && message[i] <= 'Z')
{
message[i] = ((message[i] - 'A') + shift) % 26 + 'A';
}
}
}
void decrypt(char *message, int shift)
{
int i;
for(i = 0; message[i] != '\0'; i++)
{
if(message[i] >= 'a' && message[i] <= 'z')
{
message[i] = ((message[i] - 'a') - shift + 26) % 26 + 'a';
}
else if(message[i] >= 'A' && message[i] <= 'Z')
{
message[i] = ((message[i] - 'A') - shift + 26) % 26 + 'A';
}
}
}
```
代替密码:
```c
#include <stdio.h>
#include <string.h>
void encrypt(char *message, char *key);
void decrypt(char *message, char *key);
int main()
{
char message[100];
char key[26];
printf("Enter message to encrypt: ");
fgets(message, 100, stdin);
printf("Enter key: ");
fgets(key, 26, stdin);
encrypt(message, key);
printf("Encrypted message: %s\n", message);
decrypt(message, key);
printf("Decrypted message: %s\n", message);
return 0;
}
void encrypt(char *message, char *key)
{
int i, j;
for(i = 0; message[i] != '\0'; i++)
{
if(message[i] >= 'a' && message[i] <= 'z')
{
j = message[i] - 'a';
message[i] = key[j];
}
else if(message[i] >= 'A' && message[i] <= 'Z')
{
j = message[i] - 'A';
message[i] = key[j];
}
}
}
void decrypt(char *message, char *key)
{
int i, j;
char temp;
for(i = 0; message[i] != '\0'; i++)
{
if(message[i] >= 'a' && message[i] <= 'z')
{
for(j = 0; j < 26; j++)
{
if(key[j] == message[i])
{
temp = j + 'a';
break;
}
}
message[i] = temp;
}
else if(message[i] >= 'A' && message[i] <= 'Z')
{
for(j = 0; j < 26; j++)
{
if(key[j] == message[i])
{
temp = j + 'A';
break;
}
}
message[i] = temp;
}
}
}
```
置换密码:
```c
#include <stdio.h>
#include <string.h>
void encrypt(char *message, char *key);
void decrypt(char *message, char *key);
int main()
{
char message[100];
char key[26];
printf("Enter message to encrypt: ");
fgets(message, 100, stdin);
printf("Enter key: ");
fgets(key, 26, stdin);
encrypt(message, key);
printf("Encrypted message: %s\n", message);
decrypt(message, key);
printf("Decrypted message: %s\n", message);
return 0;
}
void encrypt(char *message, char *key)
{
int i, j, k;
char temp[100];
for(i = 0, k = 0; message[i] != '\0'; i++)
{
if(message[i] >= 'a' && message[i] <= 'z')
{
j = message[i] - 'a';
temp[k++] = key[j];
}
else if(message[i] >= 'A' && message[i] <= 'Z')
{
j = message[i] - 'A';
temp[k++] = key[j];
}
}
temp[k] = '\0';
strcpy(message, temp);
}
void decrypt(char *message, char *key)
{
int i, j, k;
char temp[100];
for(i = 0, k = 0; message[i] != '\0'; i++)
{
if(message[i] >= 'a' && message[i] <= 'z')
{
for(j = 0; j < 26; j++)
{
if(key[j] == message[i])
{
temp[k++] = j + 'a';
break;
}
}
}
else if(message[i] >= 'A' && message[i] <= 'Z')
{
for(j = 0; j < 26; j++)
{
if(key[j] == message[i])
{
temp[k++] = j + 'A';
break;
}
}
}
}
temp[k] = '\0';
strcpy(message, temp);
}
```
阅读全文