维吉尼亚密码加密解密

//k为加密关键词
string encrypt(string mingwen,string k)
{
string miwen = "";
int cont = 0;
int len = k.length(); //len为加密关键字的长度
for(int i=0; i<mingwen.length(); i++)
{
if(mingwen[i]<'a'||mingwen[i]>'z')
{
miwen+=mingwen[i];
cont++;
}
else
{
int j = (i-cont) % len;
int n = k[j]-'a';
miwen += ((mingwen[i]-'a'+n)%26+'a');
}
}
return miwen;
}

string decrypt(string miwen,string k)
{
string mingwen = "";
int cont = 0;
int len = k.length();  //len为加密关键字的长度
for(int i=0; i<miwen.length(); i++)
{
if(miwen[i]<'a'||miwen[i]>'z')
{
mingwen+=miwen[i];
cont++;
}
else
{
int j = (i-cont) % len;
int n = k[j]-'a';
mingwen += (miwen[i]-'a'+26-n)%26+'a';
}
}
return mingwen;
}

#include<bits/stdc++.h>
using namespace std;
//重合指数接近0.65,用于确定秘钥长度
string encrypt(string mingwen,string k);
string decrypt(string miwen,string k);
int main()
{
int i;
string k="";   //加密关键字
string s;
ifstream fin("in.txt");
ofstream fout("out.txt");
cout<<"输入小写加密关键字:";
cin>>k;
cout<<"选择操作(1:加密 2:解密):";
cin>>i;

switch(i)
{
case 1:
{
while(getline(fin,s))
{
fout<<encrypt(s,k)<<endl;
}
fin.close();
fout.close();
}
break;

case 2:
{
while(getline(fin,s))
{
fout<<decrypt(s,k)<<endl;
}
fin.close();
fout.close();
}
break;

default:
cout<<"输入不合法"<<endl;
break;
}

return 0;
}

//加密
string encrypt(string mingwen,string k)
{
string miwen = "";
int cont = 0;
int len = k.length(); //len为加密关键字的长度
for(int i=0; i<mingwen.length(); i++)
{
if(mingwen[i]<'a'||mingwen[i]>'z')
{
miwen+=mingwen[i];
cont++;
}
else
{
int j = (i-cont) % len;
int n = k[j]-'a';
miwen += ((mingwen[i]-'a'+n)%26+'a');
}
}
return miwen;
}

//解密

string decrypt(string miwen,string k)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           n,string k)
{
string mingwen = "";
int cont = 0;
int len = k.length();  //len为加密关键字的长度
for(int i=0; i<miwen.length(); i++)
{
if(miwen[i]<'a'||miwen[i]>'z')
{
mingwen+=miwen[i];
cont++;
}
else
{
int j = (i-cont) % len;
int n = k[j]-'a';
mingwen += (miwen[i]-'a'+26-n)%26+'a';
}
}
return mingwen;
}



12-01

03-04 6746
10-16 2337
03-29
11-03
10-23
10-24
07-04 1958
04-12 9541
03-26
03-17 1745
03-10 3776
12-04
11-05 1070
04-18 1077