easy way to transform your plaintext into ciphertext

The outputs:

The codes:

//use ASCII value to transform plaintext into ciphertext
#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
void transFromStrToASCII(string word,vector<int>& v);
int main()
{
    //simple one with only one English word
    string word = "English";
    cout << "Original plaintext: " << word << endl;
    vector<int> values;
    transFromStrToASCII(word ,values);
    int last = values[values.size()-1];
    for_each(values.begin(),values.end(),[](int x){
        cout << setw(3) << x << " ";
    });
    return 0;
}
void transFromStrToASCII(string word, vector<int>& v)
{
    for (int i = 0; i < word.length(); i++){
        v.push_back(int(word[i])) ;
    }
}

The basic ways to use the for_each and lambda function:

for_each(cyphertext.begin(),cyphertext.end(),[](char c){
        cout << c;
    });

 The following codes, just use transform the elements in vector into the string 

Original plaintext: English
 69 110 103 108 105 115 104
the content in the string of cyphertext:
69 110 103 108 105 115 104

THE corresponding codes:

//use ASCII value to transform plaintext into ciphertext
#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
void transFromStrToASCII(string word,vector<int>& v);
void to_string(vector<int> v, string& cyphertext);
void deEncryption(string cyphertext, string plaintext);
int main()
{
    //simple one with only one English word
    string word = "English";
    cout << "Original plaintext: " << word << endl;
    vector<int> values;
    transFromStrToASCII(word ,values);
    int last = values[values.size()-1];
    for_each(values.begin(),values.end(),[](int x){
        cout << setw(3) << x << " ";
    });
    cout << endl;
    string cyphertext;
    to_string(values,cyphertext);
    cout << "the content in the string of cyphertext:\n";
    for_each(cyphertext.begin(),cyphertext.end(),[](char c){
        cout << c;
    });
    /*for (int i = 0; i < cyphertext.size(); i++){
        cout << setw(3) << cyphertext[i];
    }*/
    return 0;
}
void transFromStrToASCII(string word, vector<int>& v)
{
    for (int i = 0; i < word.length(); i++){
        v.push_back(int(word[i])) ;
    }
}
void to_string(vector<int> v, string& cyphertext)
{
    for (int i = 0; i < v.size(); i++){
        if (i!= v.size()-1){
            cyphertext += to_string(v[i]);
            cyphertext += ' ';
        }else{
            cyphertext += to_string(v[i]);
        }
    }
}

The final version of the encryption of string by its ascii:

The outputs:

Original plaintext: English
 69 110 103 108 105 115 104
the content in the string of cyphertext:
69 110 103 108 105 115 104
The plaintext from the string of cyphertext
English

The corresponding codes:

//use ASCII value to transform plaintext into ciphertext
#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
void transFromStrToASCII(string word,vector<int>& v);
void to_string(vector<int> v, string& cyphertext);
void deEncryption(string cyphertext, string &plaintext);
int main()
{
    //simple one with only one English word
    string word = "English";
    cout << "Original plaintext: " << word << endl;
    vector<int> values;
    transFromStrToASCII(word ,values);
    int last = values[values.size()-1];
    for_each(values.begin(),values.end(),[](int x){
        cout << setw(3) << x << " ";
    });
    cout << endl;
    string cyphertext;
    to_string(values,cyphertext);
    cout << "the content in the string of cyphertext:\n";
    for_each(cyphertext.begin(),cyphertext.end(),[](char c){
        cout << c;
    });
    /*for (int i = 0; i < cyphertext.size(); i++){
        cout << setw(3) << cyphertext[i];
    }*/
    string plaintext;
    deEncryption(cyphertext,plaintext);
    cout << "\nThe plaintext from the string of cyphertext"<<endl;
    cout << plaintext << endl;
    return 0;
}
void transFromStrToASCII(string word, vector<int>& v)
{
    for (int i = 0; i < word.length(); i++){
        v.push_back(int(word[i])) ;
    }
}
void to_string(vector<int> v, string& cyphertext)
{
    for (int i = 0; i < v.size(); i++){
        cyphertext += to_string(v[i]);
        cyphertext += ' ';
    }
}
void deEncryption(string cyphertext, string &plaintext)
{
    string temp;
    for (int i = 0; i < cyphertext.size(); i++) {
        if (cyphertext[i] != ' '){
            temp += cyphertext[i];
            continue;
        }
        int value = atoi(temp.c_str());
        plaintext += (char)value;  
        temp.clear();      
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_38396940/article/details/121640038
way