C语言设计一个简单的加密解密程序如下:
为洪江管理区等地区用户提供了全套网页设计制作服务,及洪江管理区网站建设行业解决方案。主营业务为成都网站设计、成都网站制作、外贸网站建设、洪江管理区网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
加密程序代码:
#includestdio.h
main()
{
char c,filename[20];
FILE *fp1,*fp2;
printf("请输入待加密的文件名:\n");
scanf("%s",filename);
fp1=fopen(filename,"r");
fp2=fopen("miwen.txt","w");
do
{
c=fgetc(fp1);
if(c=32c=126)
{
c=c-32;
c=126-c;
}
if(c!=-1)
fprintf(fp2,"%c",c);
}
while(c!=-1);
}
解密程序代码:
#includestdio.h
#includestring.h
main()
{
char c,filename[20];
char yanzhengma[20];
FILE *fp1,*fp2;
printf("请输入待解密文件名:\n");
scanf("%s",filename);
printf("请输入验证码:\n");
scanf("%s",yanzhengma);
if(strcmp(yanzhengma,"shan")==0)
{
fp1=fopen(filename,"r");
fp2=fopen("yuanwen.txt","w");
do
{
c=fgetc(fp1);
if(c=32c=126)
{
c=126-c;
c=32+c;
}
if(c!=-1)
fprintf(fp2,"%c",c);
}
while(c!=-1);
}
else
{
printf("验证码错误!请重新输入:\n");
scanf("%s",filename);
}
}
unsigned char* encrypt(unsigned char* psw, int enc) {
int sum = 0, i;
if (enc) {
for (i = 0; i 6; i++) {
psw[i] -= 15;
sum += psw[i];
}
psw[6] = (unsigned char) sum;
psw[7] = 0;
}
else {
for (i = 0; i 6; i++) {
sum += psw[i];
psw[i] += 15;
}
if ((unsigned char)sum != psw[6]) {
printf("Bad password\n");
psw[0] = 0;
return psw;
}
else {
psw[6] = 0;
}
}
for (i = 0; i 3;i++) {
unsigned char t = psw[i];
psw[i] = psw[5-i];
psw[5-i] = t;
}
return psw;
}
int main()
{
unsigned char psw[128];
scanf("%s", psw);
printf("encode to:%s\n", encrypt(psw, 1));
printf("decode to:%s\n", encrypt(psw, 0));
return 0;
}
C语言代码和运行结果如下:
输出符合示例,加解密均正确,望采纳~
附源码链接:字符串加解密
char* encode(const char* str)
{
int i, s;
s = 0;
for(i = 0; str[i]; i++) s += str[i] * str[i];
for(i = 0; str[i]; i++) {
if(str[i] = 'a' str[i] = 'z') {
str[i] = (str[i] + s) % 128;
}
}
return str;
}
不能准确地恢复