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

📄 vcode.cpp

📁 维尼利亚加密解密算法
💻 CPP
字号:
/*********************************************************************************/
/***                 This is a program implemtation the encryption            ****/
/*******            Compile and Run Environmetn: Bloodshed Dev-C++          ******/
/*****                             ******/
/*********************************************************************************/

#include<iostream>
#include<string>


using namespace std;

//加密函数,输入字符和其偏移的步长,返回密文
char encrypt(char a,int step)
{
  
  if(('A'<=a&&a<='Z')||('a'<=a&&a<='z')) // Encryption process.
  {
    if(a<='Z')
        a=((a+step-'A')%26)+'A';
	else
        a=((a+step-'a')%26)+'a';
  }
  return a; 
}


//解密函数,输入密文和偏移量,返回解密明文
char decrypt(char a,int step)
{
  if(('A'<=a&&a<='Z')||('a'<=a&&a<='z'))
  {
	 if(a<='Z')
		a='A' + ((a - 'A' - step + 26) % 26);
	 else
		a='a' + ((a - 'a' - step + 26) % 26);
  }
  return a;
}



int main()
{
  cout<<" This program is only available for English characters.\nYou can type in other characters, but it cann't be encrypted."<<endl<<endl;
/********* The following code get the plaintext you type in, at the same time, it isn't limited.*********/
  string str;
  int num=0;
  const char *ch;
  cout<<"Please input the plaintext:";
  getline(cin, str);  //将整一行读入来,可以包括空格。
  ch=str.c_str();     //这样获得的是 const 的一个指针,可以输出,但是不能修改
  while(0!=*ch)
  {
    num++;
	ch++;
  }  
  char *text = new char[num];   //Allocate memory dynamiclly.
  char *tex;
  tex=text;
  ch=str.c_str();  //再次赋值,重置指针
  while(0!=*ch)
  {
    *tex=*ch;
	tex++;
	ch++;
  }
  cout<<endl<<"The plain text you input is:"<<endl;
  tex=text;
  for(int i=0;i<num;i++)
	  cout<<*tex++;
  cout<<endl<<endl;
  tex=text;    //Reset pointer.
/************************** Get string end.******************************************/


/*******The following code get the password, and its length is unlimited too.********/  
  string str1;
  int num1=0;    //keep the number of the password digits.
  const char *chn;
  cout<<"Please input the Password:";
  cin>>str1;
  chn=str1.c_str();//这样获得的是 const 的一个指针,可以输出,但是不能修改
  while(0!=*chn)
  {
    num1++;
	chn++;
  }
 int *digit = new int[num1];   //Allocate memory dynamiclly.
 int *digi;
 digi=digit;           //Set pointer.
 chn=str1.c_str();     //Reset pointer.
  while(0!=*chn)
  {
    *digi=*chn-'0';
	digi++;
	chn++;
  } 
 digi=digit;
 cout<<"The digits you input is:"<<endl; 
   for(int i=0;i<num1;i++)
	  cout<<*(digi++)<<"  ";  //Only for program test!
   cout<<endl<<endl;
   digi=digit;   //Reset pointer.
/**************************** Get password end.********************************/



/*************** Encryption process...*****************************************/
  int count=0;
  for(int i=0;i<num;i++)   // Walks buffer until NULL
  {
    count=count%num1;
    *tex=encrypt(*tex,*(digi+count));
	tex++;
	count++;
  }
  tex=text;   //Reset pointer.
  cout<<"The encrypted text:"<<endl;
 for(int i=0;i<num;i++)   // Walks buffer until NULL
  {
    cout<<*tex++;
  }
 cout<<endl<<endl;
 tex=text;   //Reset pointer. It's important.
 digi=digit;
/********************* End eccryption...***************************/




 /********* Get decrypted password for decryption.*************/
 cout<<"Please input the password for decryption,\n if the password you type in is not the correct one, the program will end.";
 string str2;
  int num2=0;  // Store the number of the decrypted password digits.
  const char *cha;
  cout<<endl<<endl<<"Please input the Password:";
  cin>>str2;
  cha=str2.c_str();//这样获得的是 const 的一个指针,可以输出,但是不能修改
  while(0!=*cha)
  {
    num2++;
	cha++;
  }
 int *digits = new int[num2];   //Allocate memory dynamiclly.
 int *digis;
 digis=digits;
 cha=str2.c_str();     //Reset pointer.
  while(0!=*cha)
  {
    *digis=*cha-'0';
	digis++;
	cha++;
  } 
 digis=digits;
 cout<<"The digits you input is:"<<endl; 
   for(int i=0;i<num2;i++)
	  cout<<*(digis++)<<"  ";  //Only for program test!
  cout<<endl<<endl;
  digis=digits;     //Reset pointer.
/************************ Get decrypted password end...*************************/


/******************* judgement and decryption process...************************/
int ass=0;
while(ass<num1)
{ 
  if(*digi==*digis)   //Here is an error, not set value, but equal.
  {
    digi++;
	digis++;
	ass++;
  }
  else
	  break;
}
digi=digit;   //Reset the pointer.
digis=digits;

if(ass==num1)
{
  cout<<"The password is the right one, the decrypted text is:"<<endl;
 int count1=0;
  for(int i=0;i<num;i++)   // Walks buffer until NULL
  {
    count1=count1%num1;
    *tex=decrypt(*tex,*(digi+count1));
	tex++;
	count1++;
  }
  tex=text;   //Reset pointer. 
 for(int i=0;i<num;i++)   // Walks buffer until NULL
  {
    cout<<*tex++;
  }
 cout<<endl<<endl;
}
else
  cout<<"The password is not the right one..."<<endl;
/******************* End judgement and decryption process...*****************************/


 delete [] text;
 delete [] digit;
 delete [] digits;
 system("pause");

  return 0;
}

⌨️ 快捷键说明

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