⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 RC5-CTS模式加密解密算法C++程序实现。 解压之后运行main.dsw.在VC++下面打开
💻 CPP
字号:
/*the main funtion of the project*/
/*created by erjianChai and Guo Jialin 2005-04-12*/
#include "RC5_CTS.h"

void GetKey( unsigned char key[b] )
{
   char ch;
   printf("please input the key which is 16 bytes in length!\n");
   int keyCount = 0;
   while( (ch = getchar()) != '\n' ) 
  {
   if( keyCount< 16)
   {
	  key[keyCount++] = ch;
   }
  else 
   {
    printf("length over");
    return;
   }
  }
}

/*get the File name from the stdin*/
void GetFileName( char *fileName)
{
  int chCount = 0;
  char ch;
  while( ( ch = getchar() ) != '\n')
  {
	fileName[chCount++] = ch;
  }
   fileName[chCount] = '\0';
}

/*get the length of the file*/
int GetFileLength( char * fileName)
{
	FILE *fpPlain;
	char ch;
	int count = 0; //  the length of the file 

	if((fpPlain=fopen(fileName,"r"))==NULL)
	{
		printf("cannot open this file\n");
        exit(0);
	}

	while( ( ch = fgetc(fpPlain) ) != EOF )
	{
		count ++;
	}
	fclose(fpPlain);

	return count;

}

/*read strings from the file*/
void ReadFromFile( char* fileName, char* plaintext)
{
  FILE *fpPlain;
  char ch;
  int count = 0;

  if((fpPlain=fopen(fileName,"r"))==NULL)
	{
		printf("cannot open this file\n");
		exit(0);
	}

	while( ( ch = fgetc(fpPlain) ) != EOF )
	{
	   plaintext[count++] = ch;
		   
	}

	fclose(fpPlain);
	
}
/*write string to file */
void WriteToFile(char *fileName, char * string)
{
  FILE *fpFile;
  int count = 0;
  char ch;
  if((fpFile=fopen(fileName,"w"))==NULL)
	{
		printf("cannot open the file\n");
		exit(0);

	}

  while( (ch = string[count] )!= '\0')
  {
	  fputc(ch,fpFile);
	  count++;
  }

}

void main()
{	
 char *plaintext; // the plaintext from  the file 
 char ch;
 
 unsigned char key[b]; // the key 
 
 //get the key from input 
 GetKey(key);
 RC5_SETUP(key); /*Set the key*/
 
 //Get the plaintext from a file
printf("please input the file which you want to encrypt\n");
char fileName[1024]; // used to get the name of the plaintext and plaintext 
 
GetFileName(fileName); // get the plaitext file name 
int count = 0; // the num of characters in file
 
count = GetFileLength(fileName);

plaintext = new char[count];

ReadFromFile(fileName,plaintext); // read the plaintext from file 

 plaintext[count] = '\0';
 
 // get the ciphtext name 
 printf("please input the the ciphertext name you want to create\n");
 GetFileName(fileName);
 
 RC5_CTSEN(plaintext,count); //encrypt
 WriteToFile(fileName,plaintext); //Write to the file 
 
 //get the filename of the decrypted file 
 printf("please input the file name to store the plaintext decrypted from\n the plaintext\n");
 GetFileName(fileName);
 RC5_CTSDE(plaintext,count); //decrypt
 WriteToFile(fileName,plaintext); //Write to the file 
 
 
 
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -