用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;
}
#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;
}
相关文章推荐
- 对RSA密码的Wiener的低解密指数攻击的C++实现
- 仿射密码的加密和解密,蛮力攻击C++实现
- 简单的加密与解密的实现---仿射密码(c++使用string)
- C++调用有道词典API实现Caesar密码的加密解密
- 仿射密码加解密及暴力破解c++实现
- C# 实现PPT文档加密、解密以及重置密码的操作
- 古典密码算法的设计与实现(C++实现)
- 【密码学】AES加解密原理及其C++实现算法
- DES 加密解密算法的C++实现
- python实现仿射密码加解密过程
- 趣味题:恺撒Caesar密码(c++实现)
- Enigma机密码加密解密的实现
- 四种古典密码的C++实现(1)-----Hill密码
- c++输入隐藏密码的实现
- 通过ini文件实现记住密码 c++
- 华为模拟题一:密码截取C++实现
- 古典密码之hill密码的加密与解密程序实现
- JNI调用c++实现AES加密解密
- 【加密解密】ADFGX密码 --图形界面实现加密解密
- 实现Caesar密码(加法密码)加解密