Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <map>
- #include <random>
- using namespace std;
- std::random_device dev;
- std::mt19937 rng(dev());
- const std::string nulls = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- std::uniform_int_distribution<std::mt19937::result_type> dist_nulls(0, nulls.length()-1);
- std::string key = "ZEBRAS";
- std::map<int, int> key_order;
- void set_permutation_order(){
- for(int i = 0; i < key.length(); ++i)
- key_order[key[i]] = i;
- }
- void normalize(std::string& str){
- str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end());
- std::transform(str.begin(), str.end(), str.begin(), ::toupper);
- }
- std::string columnar_transpos_encrypt(std::string msg){
- normalize(msg);
- //std::cout << msg << '\n';
- int cols = key.length();
- int rows = msg.length()/cols;
- if(msg.length() % cols)
- ++rows;
- char** matrix = new char*[rows];
- for(int i = 0; i < rows; ++i)
- matrix[i] = new char[cols];
- int ind{0};
- for(int row = 0; row < rows; ++row){
- for(int col = 0; col < cols; ++col){
- if(ind >= msg.length())
- matrix[row][col] = nulls[dist_nulls(rng)];
- else
- matrix[row][col] = msg[ind++];
- }
- }
- //std::cout << rows << " " << cols << '\n';
- std::string encrypted{};
- int col_order{};
- for(auto it = key_order.begin(); it != key_order.end(); ++it){
- col_order = it->second;
- for(int row = 0; row < rows; ++row){
- encrypted += matrix[row][col_order];
- }
- }
- //for debug purpose
- for(int row = 0; row < rows; ++row){
- for(int col = 0; col < cols; ++col){
- std::cout << matrix[row][col] << ' ';
- }
- std::cout << '\n';
- }
- for(int i = 0; i < rows; ++i)
- delete[] matrix[i];
- delete[] matrix;
- return encrypted;
- }
- std::string columnar_transpos_decrypt(std::string& ciphered){
- int cols = key.length();
- int rows = ciphered.length()/cols;
- char** matrix = new char*[rows];
- for(int i = 0; i < rows; ++i)
- matrix[i] = new char[cols];
- std::cout << cols << " cols " << rows << " rows\n";
- std::string sorted_key = key;
- std::sort(sorted_key.begin(), sorted_key.end());
- //тут начинаются странности
- int ind{0};
- for(const char& ch: sorted_key){
- int col = key.find(ch);
- for(int j = 0; j < rows; ++j){
- std::cout << ciphered[ind]; //для проверки того, действительно
- //индекс идет по всей строке
- matrix[j][col] = ciphered[ind++];
- }
- }
- std::cout << "\n";
- //здесь видно, что матрица заполняется не полностью, хотя должна быть 6х5
- for(int i = 0; i < cols; ++i){
- for(int j = 0; j < rows; ++j){
- std::cout << matrix[i][j] << ' ';
- }
- std::cout << '\n';
- }
- /*
- std::string sorted_key = key;
- std::sort(sorted_key.begin(), sorted_key.end());
- int index{0};
- for(const auto& ch: sorted_key){
- //при заполнении проходит по sorted_key
- //и находит индекс элемента sorted_key
- //в строке key
- int right_col = key.find(ch);
- for(int row = 0; row < rows; ++row){
- matrix[row][right_col] = ciphered[++index];
- }
- }
- for(int col = 0; col < cols; ++col){
- for(int row = 0; row < rows; ++row){
- std::cout << matrix[row][col] << " ";
- }
- std::cout << '\n';
- }
- std::string deciphered{};
- int str_len{0};
- for(int row = 0; row < rows; ++row){
- for(int col = 0; col < cols; ++col){
- deciphered += matrix[row][col];
- }
- }
- */
- std::string deciphered{};
- for(int i = 0; i < rows; ++i)
- delete[] matrix[i];
- delete[] matrix;
- return deciphered;
- }
- int main()
- {
- std::string ms = "We are discovered flee at once";
- set_permutation_order();
- std::string res = columnar_transpos_encrypt(ms);
- std::cout << "Ciphered: " << res << '\n';
- std::cout << columnar_transpos_decrypt(res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement