#includestdio.h
創(chuàng)新互聯(lián)建站專注于企業(yè)網(wǎng)絡(luò)營銷推廣、網(wǎng)站重做改版、常寧網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁面制作、商城網(wǎng)站制作、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為常寧等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
#includestring.h
#define?PASSWORD?3
int?main()
{
char?c;?
FILE?*source?=?fopen("source.txt","r");//源???解密時將該源文件換成加密后的文件?
FILE?*result?=?fopen("result.txt","w");//處理結(jié)果?
while((c=fgetc(source))!=EOF)//加密?
{????????
if(c='a'c='z')
c='a'+(c-'a'+PASSWORD)%26;
else?if(c='A'c='Z')
c='A'+(c-'A'+PASSWORD)%26;
fputc(c,result);
}
/*????
while((c=fgetc(source))!=EOF)//解密??解密時將加密部分注釋掉?
{????????
if(c='a'c='z')
c='a'+(c-'a'+26-PASSWORD)%26;
else?if(c='A'c='Z')
c='A'+(c-'A'+26-PASSWORD)%26;
fputc(c,result);
}
*/????
close(source);
close(result);
system("pause");
return?0;
}
這是之前寫的,可以看看
1、在密碼學(xué)中,愷撒密碼(或稱愷撒加密、愷撒變換、變換加密)是一種最簡單且最廣為人知的加密技術(shù)。它是一種替換加密的技術(shù),明文中的所有字母都在字母表上向后(或向前)按照一個固定數(shù)目進行偏移后被替換成密文。例如,當(dāng)偏移量是3的時候,所有的字母A將被替換成D,B變成E,以此類推。這個加密方法是以愷撒的名字命名的,當(dāng)年愷撒曾用此方法與其將軍們進行聯(lián)系。愷撒密碼通常被作為其他更復(fù)雜的加密方法中的一個步驟,例如維吉尼爾密碼。愷撒密碼還在現(xiàn)代的ROT13系統(tǒng)中被應(yīng)用。但是和所有的利用字母表進行替換的加密技術(shù)一樣,愷撒密碼非常容易被破解,而且在實際應(yīng)用中也無法保證通信安全。例子愷撒密碼的替換方法是通過排列明文和密文字母表,密文字母表示通過將明文字母表向左或向右移動一個固定數(shù)目的位置。
2、kaiser加密算法具體程序:
#includestdio.h
#includeconio.h?
char?encrypt(char?ch,int?n)/*加密函數(shù),把字符向右循環(huán)移位n*/
{
while(ch='A'ch='Z')
{
return?('A'+(ch-'A'+n)%26);
}
while(ch='a'ch='z')
{
return?('a'+(ch-'a'+n)%26);
}
return?ch;
}
void?menu()/*菜單,1.加密,2.解密,3.暴力破解,密碼只能是數(shù)字*/
{
clrscr();
printf("\n=========================================================");
printf("\n1.Encrypt?the?file");
printf("\n2.Decrypt?the?file");
printf("\n3.Force?decrypt?file");
printf("\n4.Quit\n");
printf("=========================================================\n");
printf("Please?select?a?item:");
return;
}
main()
{
int?i,n;
char?ch0,ch1;
FILE?*in,*out;
char?infile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf("\nPlease?input?the?infile:");
scanf("%s",infile);/*輸入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can?not?open?the?infile!\n");
printf("Press?any?key?to?exit!\n");
getch();
exit(0);
}
printf("Please?input?the?key:");
scanf("%d",n);/*輸入加密密碼*/
printf("Please?input?the?outfile:");
scanf("%s",outfile);/*輸入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can?not?open?the?outfile!\n");
printf("Press?any?key?to?exit!\n");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nEncrypt?is?over!\n");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf("\nPlease?input?the?infile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can?not?open?the?infile!\n");
printf("Press?any?key?to?exit!\n");
getch();
exit(0);
}
printf("Please?input?the?key:");
scanf("%d",n);/*輸入解密密碼(可以為加密時候的密碼)*/
n=26-n;
printf("Please?input?the?outfile:");
scanf("%s",outfile);/*輸入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can?not?open?the?outfile!\n");
printf("Press?any?key?to?exit!\n");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nDecrypt?is?over!\n");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf("\nPlease?input?the?infile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can?not?open?the?infile!\n");
printf("Press?any?key?to?exit!\n");
getch();
exit(0);
}
printf("Please?input?the?outfile:");
scanf("%s",outfile);/*輸入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can?not?open?the?outfile!\n");
printf("Press?any?key?to?exit!\n");
fclose(in);
getch();
exit(0);
}
for(i=1;i=25;i++)/*暴力破解過程,在察看信息正確后,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("==========================================================\n");
printf("The?outfile?is:\n");
printf("==========================================================\n");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf("\n========================================================\n");
printf("The?current?key?is:?%d?\n",i);/*顯示當(dāng)前破解所用密碼*/
printf("Press?'Q'?to?quit?and?other?key?to?continue......\n");
printf("==========================================================\n");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'時退出*/
{
clrscr();
printf("\nGood?Bye!\n");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf("\nForce?decrypt?is?over!\n");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf("\nGood?Bye!\n");
sleep(3);
}
凱撒密碼就是簡單的加上一個數(shù),'a'+3='d';'z'+3='c' 假設(shè)原文全是小寫字母,那么 char plain[N]={...}; //明文 char cipher[N]={};//密文 int key=3; int i=0,temp; for(i=0;iN;i++) {if(plain[i]!=' ') {temp=plain[i]+key-'a'; temp=temp%26; cipher[i]=temp+'a'; } else cipher[i]=plain[i]; } 這樣就完成了加密,密文數(shù)組里面就是對原文加密后的密文,key是密鑰。
(2)kaiser加密算法
具體程序:
#includestdio.h
#includeconio.h
char encrypt(char ch,int n)/*加密函數(shù),把字符向右循環(huán)移位n*/
{
while(ch='A'ch='Z')
{
return ('A'+(ch-'A'+n)%26);
}
while(ch='a'ch='z')
{
return ('a'+(ch-'a'+n)%26);
}
return ch;
}
void menu()/*菜單,1.加密,2.解密,3.暴力破解,密碼只能是數(shù)字*/
{
clrscr();
printf("\n=========================================================");
printf("\n1.Encrypt the file");
printf("\n2.Decrypt the file");
printf("\n3.Force decrypt file");
printf("\n4.Quit\n");
printf("=========================================================\n");
printf("Please select a item:");
return;
}
main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*輸入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the key:");
scanf("%d",n);/*輸入加密密碼*/
printf("Please input the outfile:");
scanf("%s",outfile);/*輸入加密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nEncrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the key:");
scanf("%d",n);/*輸入解密密碼(可以為加密時候的密碼)*/
n=26-n;
printf("Please input the outfile:");
scanf("%s",outfile);/*輸入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf("\nDecrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf("\nPlease input the infile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Can not open the infile!\n");
printf("Press any key to exit!\n");
getch();
exit(0);
}
printf("Please input the outfile:");
scanf("%s",outfile);/*輸入解密后文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Can not open the outfile!\n");
printf("Press any key to exit!\n");
fclose(in);
getch();
exit(0);
}
for(i=1;i=25;i++)/*暴力破解過程,在察看信息正確后,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("==========================================================\n");
printf("The outfile is:\n");
printf("==========================================================\n");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf("\n========================================================\n");
printf("The current key is: %d \n",i);/*顯示當(dāng)前破解所用密碼*/
printf("Press 'Q' to quit and other key to continue......\n");
printf("==========================================================\n");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'時退出*/
{
clrscr();
printf("\nGood Bye!\n");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf("\nForce decrypt is over!\n");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf("\nGood Bye!\n");
sleep(3);
}
.
希望能夠幫助你 ^_^ 也希望能夠選為最佳答案!