sourcecode.txt

来自「学习一个加密程序」· 文本 代码 · 共 137 行

TXT
137
字号
//本程序是实现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 + =
减小字号Ctrl + -
显示快捷键?