實現Caesar密碼(加法密碼)加解密
阿新 • • 發佈:2019-02-13
1.加密過程的明文和金鑰可以是任意按鍵輸入的小寫英文字母;解密過程的密文和金鑰可以是任意按鍵輸入的小寫字母
2.給出.cpp檔案原始碼和加解密結果
#include<iostream>
using namespace std;
void main(){
int k;
char c;
int m,n;
char s[2];
int j=0;
cout<<"請輸入您的選擇\n加密單一字元請選1,解密單一字元請選2\n,加密字串請選3,解密字串請選4\n";
cin>>m;
switch(m){
case 1:
cout<<"請輸入加密的字元c:\n";
cin>>c;
cout<<"請輸入金鑰k:\n";
cin>>k;
cout<<"該字元加密為:\n";
c=c+k;
cout<<c<<endl;
break;
case 2:
cout<<"請輸入解密的字元c:\n" ;
cin>>c;
cout<<"請輸入金鑰k:\n";
cin>>k;
cout<<"該字元加密為:\n";
c=c-k;
cout<<c<<endl;
break;
case 3:
cout<<"請輸入字串長度:n"<<endl;
cin >>n;
cout<<"請輸入加密的字元c\n";
for(j=0;j<n;j++){
cin>>s[j]; }
cout<<"請輸入金鑰k:\n";
cin>>k;
cout<<"該字元加密為:\n";
for(j=0;j<n;j++){
s[j]+=k;
cout<<s[j];
}
cout<<"\n";
break;
case 4:
cout<<"請輸入字串長度:n"<<endl;
cin>>n;
cout<<"請輸入解密的字元c:\n";
for(j=0;j<n;j++){
cin>>s[j]; }
cout<<"請輸入金鑰k:\n";
cin>>k;
cout<<"該字元解密為:\n";
for(j=0;j<n;j++){
s[j]+=k;
cout<<s[j];
}
cout<<"\n";
break;
default:
cout<<"err";
break;
}
}
執行結果: