The first function could delete the empty space in your plaintext.
the outputs:
The original plaintext:
I love you more.
The plaintext without empty space:
Iloveyoumore.
The corresponding codes:
//realize the shift cipher encryption scheme
#include <iostream>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
void str_to_vec(string str, vector<string> &vec);
void disp_vec(vector<string> vec);
void Gen();
int main()
{
cout << "The original plaintext:" << endl;
string plaintext = "I love you more.";
for_each(plaintext.begin(), plaintext.end(), [](char c) { cout << c; });
vector<string> vec;
str_to_vec(plaintext, vec);
cout << "\nThe plaintext without empty space:" << endl;
disp_vec(vec);
return 0;
}
void str_to_vec(string str, vector<string> &vec)
{
str = str + ' ';
string temp_str = "";
for (int i = 0; i < str.length(); i++){
if(str[i] != ' '){
temp_str = temp_str + str[i];
}else{
vec.push_back(temp_str);
temp_str.clear();
}
}
}
void disp_vec(vector<string> vec)
{
for (int i = 0; i < vec.size(); i++){
cout << vec[i];
}
}
Now it's the time to execute the structure of encryption algorithm:
The frist step to generate the private key:
The original plaintext:
I love you more.
The plaintext without empty space:
Iloveyoumore.
private key:23
The corresponding codes:
//realize the shift cipher encryption scheme
#include <iostream>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
void str_to_vec(string str, vector<string> &vec);
void disp_vec(vector<string> vec);
int Gen(); // generate a randome k range from 1 to 25
int main()
{
srand((unsigned int)time(NULL));
cout << "The original plaintext:" << endl;
string plaintext = "I love you more.";
for_each(plaintext.begin(), plaintext.end(), [](char c) { cout << c; });
vector<string> vec;
str_to_vec(plaintext, vec);
cout << "\nThe plaintext without empty space:" << endl;
disp_vec(vec);
int k = Gen();
cout << "\nprivate key:" << k << endl;
return 0;
}
void str_to_vec(string str, vector<string> &vec)
{
str = str + ' ';
string temp_str = "";
for (int i = 0; i < str.length(); i++){
if(str[i] != ' '){
temp_str = temp_str + str[i];
}else{
vec.push_back(temp_str);
temp_str.clear();
}
}
}
void disp_vec(vector<string> vec)
{
for (int i = 0; i < vec.size(); i++){
cout << vec[i];
}
}
int Gen()
{
return rand() % 26 + 1;
}
The second step is to design the shift operation encryption schemes:
1. use a lambda function to achieve encryption scheme:
The outpus:
oi}wigvix
The corresponding codes:
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
void disp_vec(vector<string> vec);
void encryption(vector<string> &vec, int k);
int main()
{
int k = 4;
vector<string> vec;
vec.push_back("key");
vec.push_back("secret");
encryption(vec, k);
disp_vec(vec);
return 0;
}
void disp_vec(vector<string> vec)
{
for (int i = 0; i < vec.size(); i++){
cout << vec[i];
}
}
void encryption(vector<string> &vec, int k)
{
for_each(vec.begin(), vec.end(),[](string &str){
for_each(str.begin(), str.end(),[](char &c){
c = (char)(c + 4);
});
});
}
The completed codes for shift cipher encryption:
(The outputs:)
The original plaintext:
i love you more.
The plaintext without empty space:
iloveyoumore.
private key:19
The ciphertext:
behoxrhnfhkxA
The corresponging codes:
//realize the shift cipher encryption scheme
#include <iostream>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
void str_to_vec(string str, vector<string> &vec);
void disp_vec(vector<string> vec);
int Gen(); // generate a randome k range from 1 to 25
void encryption(vector<string> &vec, int k); // encrypt the strings in vector
int main()
{
srand((unsigned int)time(NULL));
cout << "The original plaintext:" << endl;
string plaintext = "i love you more.";
for_each(plaintext.begin(), plaintext.end(), [](char c) { cout << c; });
vector<string> vec;
str_to_vec(plaintext, vec);
cout << "\nThe plaintext without empty space:" << endl;
disp_vec(vec);
int k = Gen();
cout << "\nprivate key:" << k << endl;
encryption(vec, k);
cout << "The ciphertext:" << endl;
disp_vec(vec);
return 0;
}
void str_to_vec(string str, vector<string> &vec)
{
str = str + ' ';
string temp_str = "";
for (int i = 0; i < str.length(); i++){
if(str[i] != ' '){
temp_str = temp_str + str[i];
}else{
vec.push_back(temp_str);
temp_str.clear();
}
}
}
void disp_vec(vector<string> vec)
{
for (int i = 0; i < vec.size(); i++){
cout << vec[i];
}
}
int Gen()
{
return rand() % 26 + 1;
}
void encryption(vector<string> &vec, int k)
{
int step = k;
for_each(vec.begin(), vec.end(),[step](string &str){
for_each(str.begin(), str.end(),[step](char &c){
if ((c + step) < 123){
c = (char)(c + step);
}else{
c = (char)((c + step) % 123 + 97);
}
});
});
}