?? zhihuan.cpp
字號:
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
string ah;
int *Q;
bool GetKey() //密鑰的處理
{
int i;
cout<<"密鑰得到并處理:"<<endl;
cout<<" 請輸入密鑰(密鑰為隨機排序的26個字母),輸入以回車結束:";
cin>>ah;
for(i=0;i<ah.length()*2;i++)
{
if(ah[i]<='9'&&ah[i]>='0')
{
return false;
}
}
Q=new int[ah.length()*2];
for(i=0;i<ah.length()*2;i++)
{
Q[i]=0;
}
for( i=0;i<ah.length();i++)
{
Q[i]=0;
for(int j=0;j<ah.length();j++)
{
if(ah[i]>ah[j])
Q[i]++;
}
}
for(i=0;i<ah.length();i++)
{
Q[i]++;
}
cout<<" 通過密鑰得到的序列為:";
for(i=0;i<ah.length();i++)
cout<<Q[i]<<" ";
cout<<endl;
cout<<endl;
return true;}
void jiami() //明文加密操作
{
char o_word[100];
int m=0,x=0;
cout<<"進行明文加密:"<<endl;
cout<<" 請輸入要加密的明文:";
gets(o_word);
m=strlen(o_word);
for(x=0;x<(ah.length()-m%ah.length())%ah.length();x++)
{
o_word[m+x]=' ';
}
o_word[m+x]='\0';
for(int n=0;n<m;n=n+ah.length())
{
int temp=0;
char b[100];
for(int p=0;p<ah.length();p++)
{
b[p]=o_word[n+p];
}
for(int q=0;q<ah.length();q++)
{
o_word[n+q]=b[Q[ah.length()+q]-1];
}
}
cout<<"得到的密文為:"<<o_word<<endl;
cout<<endl;
}
void jiemi() //明文加密操作
{
char s_word[100];
int m=0,x=0;
cout<<"進行密文解密:"<<endl;
cout<<" 請輸入要解密的密文:";
gets(s_word);
m=strlen(s_word);
for(x=0;x<(ah.length()-m%ah.length())%ah.length();x++)
{
s_word[m+x]=' ';
}
s_word[m+x]='\0';
for(int n=0;n<m;n=n+ah.length())
{
int temp=0;
char b[100];
for(int p=0;p<ah.length();p++)
{
b[Q[ah.length()+p]-1]=s_word[n+p];
}
for(int q=0;q<ah.length();q++)
{
s_word[n+q]=b[q];
}
}
cout<<"得到的明文為:"<<s_word<<endl;
cout<<endl;
}
int main()
{
char t;
cout<<" ****************歡迎使用置換密碼功能*******************"<<endl;
cout<<endl;
while(!GetKey())
{
cout<<"請輸入正確的密鑰!!"<<endl;
cout<<endl;
}
for(int m=0;m<ah.length();m++)
{
Q[m+ah.length()]=Q[m];
Q[m]=m+1;
}
cout<<"請選擇(1)加密或(2)解密或(3)退出:";
fflush(stdin);
while(scanf("%c",&t)>0)
{
fflush(stdin);
switch(t)
{
case '1': jiami();break;
case '2': jiemi();break;
case '3': cout<<"程序結束!"<<endl;delete Q;return 0;
default: break;
}
cout<<"請繼續選擇(1)加密或(2)解密或(3)退出:";
}
delete Q;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -