?? sourcecode.txt
字號:
//本程序是實現Vigenere加密算法的小程序
#include<conio.h>
#include<process.h>
#include<stdio.h>
#include<iostream.h>
#define File_Len 10//定義文件名長度
#define Key_Len 20//定義密匙長度
void encrypt()//加密函數
{
char infile[File_Len];
char key[Key_Len];
cout<<"\nInput the filename:";
cin>>infile;
cout<<"Input the key:";
cin>>key;
FILE *fp;
int i=0;
char *p=key,ch;
if((fp=fopen(infile,"r+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(*p=='\0')
p=key;//Loop of key
if(ch>='a'&&ch<='z')
{
if(*p>='a'&&*p<='z') ch=((ch-96)+(*p-96))%26+95;
else ch=((ch-96)+(*p-64))%26+95;
}
else if(ch>='A'&&ch<='Z')
{
if(*p>='A'&&*p<='Z') ch=((ch-64)+(*p-64))%26+63;
else ch=((ch-64)+(*p-96))%26+63;
}
else ;
p++,i++;
fseek(fp,i-1,0);
fputc(ch,fp);
fseek(fp,i,0);
ch=fgetc(fp);
}
fclose(fp);
}
/*****************************************************************/
void decrypt()//解密函數
{
char infile[File_Len];
char key[Key_Len];
cout<<"\nInput the filename:";
cin>>infile;
cout<<"Input the key:";
cin>>key;
FILE *fp;
int i=0;
char *p=key,ch;
if((fp=fopen(infile,"r+"))==NULL)
{
printf("cannot open file\n");
exit(0);
}
ch=fgetc(fp);
while(ch!=EOF)
{
if(*p=='\0')
p=key;//Loop of key
if(ch>='a'&&ch<='z')
{
if(*p>='a'&&*p<='z') ch=((ch-96)+26-(*p-96))%26+97;
else ch=((ch-96)+26-(*p-64))%26+97;
}
else if(ch>='A'&&ch<='Z')
{
if(*p>='A'&&*p<='Z') ch=((ch-64)+26-(*p-64))%26+65;
else ch=((ch-64)+26-(*p-96))%26+65;
}
else ;
p++,i++;
fseek(fp,i-1,0);
fputc(ch,fp);
fseek(fp,i,0);
ch=fgetc(fp);
}
fclose(fp);
}
/*****************************************************************/
void main()//主函數
{
int choice;
Loop:
do
{
clrscr();
cout<<"Welcome to use!\n";
cout<<"Author:DengJianguo(Icerain)\n";
cout<<"E-mail:djg3412@yahoo.com.cn \n";
cout<<"\nPlease make a choice from fallowing options:\n";
cout<<"0)Exit \n";
cout<<"1)Encrypt \n";
cout<<"2)Decrypt \n";
cout<<"\n Please input a option number[0-2]:";
choice=getche();
}while(choice!='0'&&choice!='1'&&choice!='2');
if(choice=='0') exit(0);
if(choice=='1')
{
encrypt();
goto Loop;
}//加密
if(choice=='2')
{
decrypt();
goto Loop;
}//解密
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -