#includestdio.h
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計、成都網(wǎng)站制作、寶豐網(wǎng)絡(luò)推廣、小程序開發(fā)、寶豐網(wǎng)絡(luò)營銷、寶豐企業(yè)策劃、寶豐品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供寶豐建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
#includeconio.h char encrypt(char ch,int n)/*加密函數(shù),把字符向右循環(huán)移位n*/
{
while(ch=Ach=Z)
{
return (A+(ch-A+n)%26);
}
while(ch=ach=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;
}void logo()/*顯示版權(quán)信息*/
{
printf("\nZhensoft Encryption [Version:1.0.0]");
printf("\nCopyright (C) 2004 Zhensoft Corp.\n");
printf("\n \n");
return;
}
main()
{
int i,n;
char ch0,ch1;
FILE *in,*out;
char infile[20],outfile[20];textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();logo();
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);/*顯示當前破解所用密碼*/
printf("Press Q to quit and other key to continue......\n");
printf("===============================================================================\n");
ch1=getch();
if(ch1==q||ch1==Q)/*按Q或者q時退出*/
{
clrscr();
logo();
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();
logo();
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是密鑰。
#include?stdio.h
#define?isletter(?c?)????(?((c)='a'(c)='z')?||?((c)='A'(c)='Z')?)
void?Enc(?const?char?*str,?char?*out,?int?key?)
{
int?i?=?0;?
while(?str[i]?)
{
if?(?isletter(?str[i]?)?)
{
out[i]?=?str[i]?+?key;
if?(?!?isletter(?out[i])??)
out[i]?-=?26;
}
else
out[i]?=?str[i];
i++;
}
out[i]?=?0;
}
void?Denc(?const?char?*str,?char?*out,?int?key?)
{
int?i=0;
while(?str[i]?)
{
if?(?isletter(?str[i]?)?)
{
out[i]?=?str[i]?-?key;
if?(?!?isletter(?out[i]?)?)
out[i]?+=?26;
}
else
out[i]?=?str[i];
i++;
}
out[i]?=?0;
}
int?main()
{
char??out[100],?out2[100];
Enc(?"THE?QUICK?BROWn?fox?jumps?over?THE?LAZY?DOG",?out,?3?);
printf(?"%s\n",?out?);
Denc(?out,?out2,?3?);
printf(?"%s\n",?out2?);
}
你需要添加路徑: ../file.txt;
因為你的exe文件在debug文件夾下,在該文件夾下沒有你輸入的哪個文件,程序中是只讀的形式打開文件的。所以,會提示存出錯。
兩種解決辦法:
第一個:將exe拷貝到加密文件的路徑下
第二個:將文件烤到exe文件路徑下。
根本的解決辦法就是: 給定文件的相對路徑或者絕對路徑,絕對不會出現(xiàn)問題的。
1、在密碼學(xué)中,愷撒密碼(或稱愷撒加密、愷撒變換、變換加密)是一種最簡單且最廣為人知的加密技術(shù)。它是一種替換加密的技術(shù),明文中的所有字母都在字母表上向后(或向前)按照一個固定數(shù)目進行偏移后被替換成密文。例如,當偏移量是3的時候,所有的字母A將被替換成D,B變成E,以此類推。這個加密方法是以愷撒的名字命名的,當年愷撒曾用此方法與其將軍們進行聯(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);/*顯示當前破解所用密碼*/
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);
}