一个简单的加密程序,大家可以看看。
---------------------------------------------------------------------------------
#include<stdio.h>
void Encry(void);
void Decry(void);
int main(void)
{
char col;
while (1)
{
printf("加密[E] 解密[D]\n");
scanf("%c", &col);
while(getchar() != '\n');
switch (col)
{
case 'E':
Encry();
break;
case 'D':
Decry();
break;
default :
printf("操作指令无效,请重新输入\n");
}
}
return 0;
}
void Encry(void)
{
char originText[100] = "\0";
char secretKey[100] = "\0";
char cipherText[100] = "\0";
printf("原文:\n");
gets(originText);
printf("密钥:\n");
gets(secretKey);
int i = 0, j = 0;
int secretNumber;
while (originText[i] != '\0')
{
secretNumber = originText[i] + secretKey[j];
if (secretNumber >= 64 && secretNumber <= 126)
{
cipherText[i] = secretNumber;
}
else if (secretNumber >= 127 && secretNumber <= 221)
{
cipherText[i] = secretNumber - 95;
}
else
{
cipherText[i] = secretNumber - 190;
}
i++, j++;
if (secretKey[j] == '\0')
{
j = 0;
}
}
printf("密文:\n%s\n", cipherText);
return ;
}
void Decry(void)
{
char originText[100] = "\0";
char secretKey[100] = "\0";
char cipherText[100] = "\0";
printf("密文:\n");
gets(cipherText);
printf("密钥:\n");
gets(secretKey);
int i = 0, j = 0;
int secretNumber;
while (cipherText[i] != '\0')
{
secretNumber = cipherText[i] - secretKey[j];
if (secretNumber >= 32 && secretNumber <= 94)
{
originText[i] = secretNumber;
}
else if (secretNumber >= -63 && secretNumber <= 31)
{
originText[i] = secretNumber + 95;
}
else
{
originText[i] = secretNumber + 190;
}
i++, j++;
if (secretKey[j] == '\0')
{
j = 0;
}
}
printf("原文:\n%s\n", originText);
return ;
}
---------------------------------------------------------------------------------
主要是用了ASCLL码来对字符进行变化,不过这只能加密英文,不能加密中文。