lab6
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// Function to generate permutation order from the key
vector<int> getPermutationOrder(string key) {
vector<pair<char, int>> keyOrder;
vector<int> order(key.length());
// Store characters of key along with their original positions
for (int i = 0; i < key.length(); i++) {
keyOrder.push_back(make_pair(key[i], i));
}
// Sort key characters to determine order
sort(keyOrder.begin(), keyOrder.end());
// Assign order mapping
for (int i = 0; i < keyOrder.size(); i++) {
order[keyOrder[i].second] = i;
}
return order;
}
// Function to replace spaces with '~' to preserve them
string preprocessText(string text) {
for (char &ch : text) {
if (ch == ' ') {
ch = '~'; // Replace space with '~' to maintain its position
}
}
return text;
}
// Function to restore spaces after decryption
string restoreSpaces(string text) {
for (char &ch : text) {
if (ch == '~') {
ch = ' '; // Convert '~' back to spaces
}
}
return text;
}
// Function to encrypt using Single Transposition Cipher
string transpositionEncrypt(string text, string key) {
text = preprocessText(text); // Replace spaces with '~'
int columns = key.length();
int rows = (text.length() + columns - 1) / columns; // Calculate required rows
vector<vector<char>> grid(rows, vector<char>(columns, '_')); // Fill with '_'
// Step 1: Fill the grid row-wise
int index = 0;
for (int r = 0; r < rows && index < text.length(); r++) {
for (int c = 0; c < columns && index < text.length(); c++) {
grid[r][c] = text[index++];
}
}
// Step 2: Determine column order from key
vector<int> order = getPermutationOrder(key);
string ciphertext = "";
// Step 3: Read column-wise in the new order
for (int i = 0; i < columns; i++) {
for (int r = 0; r < rows; r++) {
ciphertext += grid[r][order[i]];
}
}
return ciphertext;
}
// Function to decrypt using Single Transposition Cipher
string transpositionDecrypt(string text, string key) {
int columns = key.length();
int rows = (text.length() + columns - 1) / columns;
vector<vector<char>> grid(rows, vector<char>(columns, '_'));
// Step 1: Get column order from key
vector<int> order = getPermutationOrder(key);
// Step 2: Fill the grid column-wise based on key order
int index = 0;
for (int i = 0; i < columns; i++) {
for (int r = 0; r < rows; r++) {
if (index < text.length()) {
grid[r][order[i]] = text[index++];
}
}
}
// Step 3: Read row-wise to get plaintext
string plaintext = "";
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
if (grid[r][c] != '_') { // Ignore padding
plaintext += grid[r][c];
}
}
}
return restoreSpaces(plaintext); // Restore spaces
}
// Main function to test encryption and decryption
int main() {
string plaintext, key, encryptedText, userCipherText, userKey;
// Get plaintext and key from user
cout << "Enter the plaintext: ";
getline(cin, plaintext); // Allows multi-word input
cout << "Enter the encryption key (numeric string without spaces, e.g., '3214'): ";
cin >> key;
// Encryption Process
encryptedText = transpositionEncrypt(plaintext, key);
cout << "\nEncrypted Text (Single Transposition): " << encryptedText << endl;
// Ask user for decryption input
cout << "\nNow enter the encrypted text to decrypt: ";
cin >> userCipherText;
cout << "Enter the decryption key: ";
cin >> userKey;
// Decryption Process
string decryptedText = transpositionDecrypt(userCipherText, userKey);
cout << "\nDecrypted Text: " << decryptedText << endl;
return 0;
}
6(b) Double Transposition
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// Function to generate permutation order from the key
vector<int> getPermutationOrder(string key) {
vector<pair<char, int>> keyOrder;
vector<int> order(key.length());
// Store characters of key along with their original positions
for (int i = 0; i < key.length(); i++) {
keyOrder.push_back(make_pair(key[i], i));
}
// Sort key characters to determine order
sort(keyOrder.begin(), keyOrder.end());
// Assign order mapping
for (int i = 0; i < keyOrder.size(); i++) {
order[keyOrder[i].second] = i;
}
return order;
}
// Function to replace spaces with '~' to preserve them
string preprocessText(string text) {
for (char &ch : text) {
if (ch == ' ') {
ch = '~'; // Replace space with '~' to maintain its position
}
}
return text;
}
// Function to restore spaces after decryption
string restoreSpaces(string text) {
for (char &ch : text) {
if (ch == '~') {
ch = ' '; // Convert '~' back to spaces
}
}
return text;
}
// Function to encrypt using Single Transposition Cipher
string transpositionEncrypt(string text, string key) {
text = preprocessText(text); // Replace spaces with '~'
int columns = key.length();
int rows = (text.length() + columns - 1) / columns; // Calculate required rows
vector<vector<char>> grid(rows, vector<char>(columns, '_')); // Fill with '_'
// Step 1: Fill the grid row-wise
int index = 0;
for (int r = 0; r < rows && index < text.length(); r++) {
for (int c = 0; c < columns && index < text.length(); c++) {
grid[r][c] = text[index++];
}
}
// Step 2: Determine column order from key
vector<int> order = getPermutationOrder(key);
string ciphertext = "";
// Step 3: Read column-wise in the new order
for (int i = 0; i < columns; i++) {
for (int r = 0; r < rows; r++) {
ciphertext += grid[r][order[i]];
}
}
return ciphertext;
}
// Function to decrypt using Single Transposition Cipher
string transpositionDecrypt(string text, string key) {
int columns = key.length();
int rows = (text.length() + columns - 1) / columns;
vector<vector<char>> grid(rows, vector<char>(columns, '_'));
// Step 1: Get column order from key
vector<int> order = getPermutationOrder(key);
// Step 2: Fill the grid column-wise based on key order
int index = 0;
for (int i = 0; i < columns; i++) {
for (int r = 0; r < rows; r++) {
if (index < text.length()) {
grid[r][order[i]] = text[index++];
}
}
}
// Step 3: Read row-wise to get plaintext
string plaintext = "";
for (int r = 0; r < rows; r++) {
for (int c = 0; c < columns; c++) {
if (grid[r][c] != '_') { // Ignore padding
plaintext += grid[r][c];
}
}
}
return restoreSpaces(plaintext); // Restore spaces
}
// Function to perform Double Transposition Encryption
string doubleTranspositionEncrypt(string text, string key1, string key2) {
string firstPass = transpositionEncrypt(text, key1);
string secondPass = transpositionEncrypt(firstPass, key2);
return secondPass;
}
// Function to perform Double Transposition Decryption
string doubleTranspositionDecrypt(string text, string key1, string key2) {
string firstPass = transpositionDecrypt(text, key2);
string secondPass = transpositionDecrypt(firstPass, key1);
return secondPass;
}
// Main function to test encryption and decryption
int main() {
string plaintext, key1, key2, encryptedText, userCipherText, userKey1, userKey2;
// Get plaintext and keys from user
cout << "Enter the plaintext: ";
getline(cin, plaintext); // Allows multi-word input
cout << "Enter the first encryption key (numeric string without spaces, e.g., '3214'): ";
cin >> key1;
cout << "Enter the second encryption key: ";
cin >> key2;
// Encryption Process
encryptedText = doubleTranspositionEncrypt(plaintext, key1, key2);
cout << "\nEncrypted Text (Double Transposition): " << encryptedText << endl;
// Ask user for decryption input
cout << "\nNow enter the encrypted text to decrypt: ";
cin >> userCipherText;
cout << "Enter the first decryption key: ";
cin >> userKey1;
cout << "Enter the second decryption key: ";
cin >> userKey2;
// Decryption Process
string decryptedText = doubleTranspositionDecrypt(userCipherText, userKey1, userKey2);
cout << "\nDecrypted Text: " << decryptedText << endl;
return 0;
}
Comments
Post a Comment