資源簡介
古典密碼體系中最基本的一種密碼機制,使用c++設計完成

代碼片段和文件信息
#include
#include
#include
using?namespace?std;
void?encryption()
{
string?key;
cout<<“請輸入密鑰“< cin>>key;
string?key2=key;
sort(key.begin()key.end());
string?str;
cout<<“請輸入需要加密的字符串“< cin>>str;
string?str2=str;
int?a[100];
for?(int?i?=0;?i {
int?j?=?0;
while(key[i]!=key2[j])
{
j++;
}
a[i]=j;
}
int?k=0;
for(int?i=0;i {
for(int?j=0;j {
str[a[j]+k*key.size()]=str2[i];
i++;
}
k++;
i=i-key.size();
}
cout<<“該字符串加密后密文是:??“< };
void?decryption()
{
string?key;
cout<<“請輸入密鑰“< cin>>key;
string?key2=key;
sort(key.begin()key.end());
string?str;
cout<<“請輸入需要解密的字符串“< cin>>str;
string?str2=str;
int?a[100];
for?(int?i?=0;?i {
int?j?=?0;
while(key[i]!=key2[j])
{
j++;
}
a[i]=j;
}
int?k=0;
for(int?i=0;i {
for(int?j=0;j {
str[a[j]+k*key.size()]=str2[i];
i++;
}
k++;
i=i-key.size();
}
cout<<“該字符串解密后密文是:??“< };
void?main()
{
cout<<“-------------------------------------------------“< cout<<“*???????????????????置換密碼????????????????????*“< cout<<“-------------------------------------------------“< cout<<“???????????????-歡迎您使用此程序-“<
cout<<“請按下列菜單選擇功能“< char?s=‘y‘;
while(s==‘y‘)
{
cout<<“1--加密??2--解密??0--退出“< cout<<“請選擇“<
int?i;
cin>>i;
cout< switch(i)
{
case?1:
encryption();
break;
case?2:
decryption();
break;
case?0:
????cout<<“??(?^_^?)/~~??謝謝您的使用,再見!“< return;
}
cout< cout<<“????????????^_^是否繼續運行本程序(yn)?:“< cin>>s;
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1853??2011-03-08?13:23??置換密碼.cpp
-----------?---------??----------?-----??----
?????????????????1853????????????????????1
- 上一篇:MFC讀取SHP文件
- 下一篇:Vigenere算法c語言的簡單實現
評論
共有 條評論