shift cipher (a kind of encryption and decryption method)

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);
            }
        });
    });
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值