easy way to transform your plaintext into ciphertext

本文详细展示了如何使用ASCII值将英文单词转换成整数向量,并演示了加密和解密的过程。通过`transFromStrToASCII`函数实现字符串到ASCII值的转换,`to_string`函数则用于将ASCII向量还原回字符串。实例以'English'为输入,输出了对应的密文和解密后的原始文本。
摘要由CSDN通过智能技术生成

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值