#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cout<<“Please input plaintext:”;
getline(cin,str);
int m = 0,n = str.length();
for(int i = 0; i < n; i ++)
{
if(int(str[i]) >= ‘A’ && int(str[i]) <= ‘Z’)
str[m++] = char(int(str[i]) - ‘A’ + ‘a’);
else if(int(str[i]) >= ‘a’ && int(str[i]) <= ‘z’)
str[m++] = str[i];
else
continue;
}
n = m;
str = str.substr(0,n);
cout<<str<<endl;
for(int i = 0; i < n; i ++)
{
m = int (str[i]);
m = (m - ‘a’ + 3)%26;
m += ‘A’;
str[i] = char(m);
}
cout<<“The cinphertext are:”<<str<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
int k,b;
cout<<“放射密码的参数k,b:”;
cin>>k>>b;
char s[2];
gets(s);
cout<<"Please input the plaintext: ";
getline(cin,str);
int n,j = 0,len = str.length();
for(int i = 0; i < len; i++)
{
n = int(str[i]);
if(n >= ‘A’ && n <= ‘Z’)
{
n -= ‘A’;
n = (k * n + b) % 26;
n += ‘A’;
str[j++] = char(n);
}
else if(n >= 'a' && n <= 'z')
{
n -= 'a';
n = (k * n + b) % 26;
n += 'A';
str[j++] = char(n);
}
else
continue;
}
str = str.substr(0,j);
cout<<"The cinphertext are:"<<str<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
string key,plaintext;
cout<<"Please input the key: ";
cin>>key;
char s[2];
gets(s); ///Eat the “Enter”.
cout<<"Please input the plaintext: ";
getline(cin,plaintext); ///Function getline() allows input with spaces.
int Klen,Plen; ///Klen and Plen denote the length of key and plaintext, respectively.
Klen = key.length();
Plen = plaintext.length();
for(int i = 0; i < Klen; i ++)
key[i] = tolower(key[i]);
///使明文仅保留26个字母并使之成为小写字母
for(int i = 0, j = 0; i < Plen; i ++)
{
if(int(plaintext[i]) >= 'A' && int(plaintext[i]) <= 'Z')
plaintext[j++] = tolower(plaintext[i]);
else if(int(plaintext[i]) >= 'a' && int(plaintext[i]) <= 'z')
plaintext[j++] = plaintext[i];
else
continue;
if(i == Plen - 1)
Plen = j;
}
plaintext = plaintext.substr(0,Plen);
for(int i = 0; i < Plen; i ++) ///Encryption!
{
plaintext[i] = char( ( ( int(plaintext[i]) + int(key[(i % Klen)]) - 2 * 'a') % 26 ) + 'A');
}
cout<<"The cinphertext are: "<<plaintext<<endl;
return 0;
}