📄 kaiser2.cpp
字号:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
/***
本程序实现kaiser密码加密
1.本程序将所有可能出现的英文和符号定义在ASCII表中的“032--126”中的95个常用字符
3.在实现2的基础上,实现控制台对英文文本文件(注意明文和密文都以文件形式存在)的加解密
形式:cipher 源文件名 目的文件名 密钥 E/D(标示加解密)说明:对于加密来说,源文件名就是明文,对于解密来说,源文件名就是密文
***/
char* encode(char* str,int k)
{
int i,j=(strlen(str)-1);
for(i=0;i<j;i++)
str[i]=(((str[i]-32)+k)%95)+32;
return str;
}
char* decode(char* str,int k)
{
int i,j=(strlen(str)-1),end=0;
for(i=0;i<j;i++)
{
if((end=((str[i]-32)-k))<0)
end=end+95;
str[i]=(end%95)+32;
}
return str;
}
char* fread(char* filename)
{
char str[100] ;
FILE *fp1;
int i=0;
if( (fp1=fopen(filename,"r"))==NULL)
{
printf("can not open file\n");
exit(0);
}
while(!feof(fp1))
{
str[i]=fgetc(fp1);
printf("%c",str[i]);
i++;
}
str[i-1]='\0';
printf("\n");
fclose(fp1);
return str;
}
void fwriteM()
{
FILE *fp;
char str1[10],ch;
printf("please input the filename of plaintext:\n");
scanf("%s",str1);
fp=fopen(str1,"w");
printf("please input the message('#'结束) :\n");
ch=getchar();
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
}
void fwirteC(char * str)
{
FILE *fp;
int i=0;
char str1[10];
printf("please input the filename of the cryption you want to encryp:\n");
scanf("%s",str1);
fp=fopen(str1,"w");
while(str[i]!='\0')
{
fputc(str[i],fp);
i++;
}
fclose(fp);
}
void main()
{
char str[100],str1[10];
int k,i=0,c;
do{
printf("__________________________________________\n");
printf(" (1)创建明文文件 \n");
printf(" (2)对明文文件加密成密文文件 \n");
printf(" (3)查看密文文件和明文文件 \n");
printf(" (4)退出 \n");
printf("__________________________________________\n");
printf("please choose:\n");
scanf("%d",&c);
switch(c)
{
case 1: fwriteM(); break;
case 2: printf("please input the filename of the plaintext:\n");
scanf("%s",str1);
strcpy(str,fread(str1));
printf("\nplese input the key:\n");
scanf("%d",&k);
strcpy(str,encode(str,k));
fwirteC(str); break;
case 3: printf("please input the filename of the cryption:\n");
scanf("%s",str1);
strcpy(str,fread(str1));
printf("\nplese input the key:\n");
scanf("%d",&k);
puts(decode(str,k));
break;
case 4: exit(0); break;
}
}while(1);
}
/*
fp=fopen("plaintext.txt","w");
//fwrite("plaintext.txt",)
printf("please input the message('#'结束) :\n");
ch=getchar();
while(ch!='#')
{
fputc(ch,fp);
ch=getchar();
}
fclose(fp);
fp1=fopen("plaintext.txt","r");
while(!feof(fp1))
{
str[i]=fgetc(fp1);
i++;
}
str[i]='\0';
fclose(fp1);
puts(str);
printf("\nplese input the key:\n");
scanf("%d",&k);
strcpy(str1,encode(str,k));
fp=fopen("ciphertext.txt","w");
printf("please input the message('#'结束) :\n");
i=0;
while(str1[i]!='\0')
{
fputc(str[i],fp);
i++;
}
fclose(fp);
printf("密文:");
puts(str1);
printf("\n____解密______\n");
printf("plese input the key:\n");
scanf("%d",&k);
puts(decode(str1,k));
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -