Answer the question
In order to leave comments, you need to log in
How to implement a block single permutation cipher?
According to the task, you need to implement a block single permutation cipher.
For example, the word "ARROW" and the key "2 3 1" are entered. The result is "TRSLKEEAEA". That is, the original word is divided into blocks of 3 letters, if there are not enough characters in the last block, then the symbol "E" is added: "STR", "ELK", "AEE".
I implemented a simple single permutation cipher, but the block cipher does not work out. How to implement it?
My implementation of a simple single permutation cipher:
#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
using namespace std;
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
string message;
const int n = 8;
int key[n];
cout << "Введите текст: ";
cin >> message;
cout << "Введите ключ (размер ключа = 8): ";
for (int i = 0; i < n; i++)
{
cin >> key[i];
}
string new_message = "";
for (int i = 0; i < n; i++) {
new_message += message[key[i] - 1];
}
cout << "Зашифрованный текст: " << new_message;
_getch();
return 0;
}
Answer the question
In order to leave comments, you need to log in
Like this, for example:
int main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
string message;
const int block_size = 8;
int key[block_size];
cout << "Введите текст: ";
cin >> message;
for (int i = 0; i < message.length()%block_size; i++){
message+= "_"; // default symbol
};
cout << "Введите ключ через пробел(размер ключа: " << block_size << " ): ";
for (int i = 0; i < block_size; i++)
{
cin >> key[i];
}
string encrypt_message = "";
int text_length = message.length();
for (int block_index = 0; block_index <= text_length/block_size; block_index++) {
for (int offset = 0; offset < block_size; offset++) {
encrypt_message += message[block_index*block_size + key[offset] - 1];
};
};
cout << "Зашифрованный текст: " << encrypt_message;
_getch();
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question