您的位置:首页 > 编程语言 > C语言/C++

用C++实现Vigenre密码得加解密

2011-10-20 19:10 429 查看
#include<iostream>

#include<vector>

using namespace std;

#define len ('a'-'A')

class Vigenre

{

public:

vector<char> vec_Key;

char Vr[26][26];

public:

void Key();

void Encrytion();

void Decrytion();

void VigenreRec();

};

void Vigenre::VigenreRec()

{

for(int i = 0;i<26;i++)

{

int count = 0;

int j = i;

while(count<26)

{



Vr[i][count] = j + 65;

count++;

if('Z'==j+65) j = -1;

j++;

}

}

}

void Vigenre::Key() // 输入密钥

{

char ch;

cout<<"Please input the key(Crtl+d to stop): ";

while(cin>>ch)

{

if(ch>'z'||ch<'A'||(ch>'Z'&&ch<'a')) continue;

if(ch>='a'&&ch<='z') ch-=len; //将密钥变成大写

vec_Key.push_back(ch);

}

}

//原则上,加解密函数可以写在一个函数里面,但是为了使代码更加明了,下面分开来写了,所以肯定会有冗余在里面

void Vigenre::Encrytion() //加密函数

{

vector<char> vec_Plaintext;

vector<char> vec_Ciphertext;

char ch;

cin.clear();

cout<<"Please input the Plaintext(Crtl+d to stop): ";

while(cin>>ch)

{

if(ch>'z'||ch<'A'||(ch>'Z'&&ch<'a')) continue; //输入明文

if(ch>='a'&&ch<='z') ch-=len; //判断密文是否是字母,如果不是就果断的舍弃掉,如果是小写的

vec_Plaintext.push_back(ch); //就将其变成大写的

}

vector<char>::iterator iter_Plaintext= vec_Plaintext.begin();

vector<char>::iterator iter_Key = vec_Key.begin();

while(iter_Plaintext !=vec_Plaintext.end())

{

int i = int(*iter_Plaintext++-65);

int j = int(*iter_Key++-65);

vec_Ciphertext.push_back(Vr[i][j]);

if(iter_Key==vec_Key.end()) iter_Key = vec_Key.begin();

}

cout<<"After Encry,the ciphertext is :"<<endl;

vector<char>::iterator iter_Ciphertext = vec_Ciphertext.begin();

int count = 0;

while(iter_Ciphertext !=vec_Ciphertext.end()) //输出加密后的密文,并每隔五个加上空格

{

if(count%5==0&&count!=0) cout<<" ";

cout<<*iter_Ciphertext++;

count++;

}

cout<<endl;

}

void Vigenre::Decrytion()

{

vector<char> vec_Plaintext;

vector<char> vec_Ciphertext;

char ch;

cin.clear();

cout<<"Please input the Ciphertext(Crtl+d to stop): ";

while(cin>>ch)

{

if(ch>'z'||ch<'A'||(ch>'Z'&&ch<'a')) continue; //输入明文

if(ch>='a'&&ch<='z') ch-=len; //判断密文是否是字母,如果不是就果断的舍弃掉,如果是小写的

vec_Ciphertext.push_back(ch); //就将其变成大写的

}

vector<char>::iterator iter_Ciphertext= vec_Ciphertext.begin();

vector<char>::iterator iter_Key = vec_Key.begin();

while(iter_Ciphertext !=vec_Ciphertext.end())

{

int i = int(*iter_Ciphertext++-65);

int j = int(91-(*iter_Key++));

vec_Plaintext.push_back(Vr[i][j]);

if(iter_Key==vec_Key.end()) iter_Key = vec_Key.begin();

}

cout<<"After Decry,the Plaintext is :"<<endl;

vector<char>::iterator iter_Plaintext = vec_Plaintext.begin();

int count = 0;

while(iter_Plaintext !=vec_Plaintext.end()) //输出加密后的密文,并每隔五个加上空格

{

if(count%5==0&&count!=0) cout<<" ";

cout<<*iter_Plaintext++;

count++;

}

cout<<endl;

}

int main()

{

cout<<"********----------------********Vigenre密码********-------------------********"<<endl;

Vigenre v ;

v.Key(); //调用输入密钥函数

v.VigenreRec(); //调用实现Vigenre方阵函数

v.Encrytion(); //调用加密函数

v.Decrytion(); //调用解密函数

system("pause");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: