-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathSimple_Columnar_Transposition_multiple_rounds.cpp
71 lines (60 loc) · 1.47 KB
/
Simple_Columnar_Transposition_multiple_rounds.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//SIMPLE COLUMNAR MULTIPLE ROUNDS TRANSPOSITION TECHNIQUE
/* It is a type of transposition cipher technique which encrypts the plain text
message into a cipher text by rearranging the characters of the
plain text. The plain text is written row-wise and read column-wise.
This procedure is chained and carried out k times. */
#include<bits/stdc++.h>
using namespace std;
string encrypt(string s,vector<int> &order) {
float n = s.length();
int row = ceil(n/5),col = 5;
char arr[row][col];
for(int i = 0;i < row; ++i) {
for(int j = 0;j < col; ++j) {
if((i * col + j) >= s.length()) {
//1 is the bogus character
arr[i][j] = '1' ;
}
else
arr[i][j] = s[i*col+j];
}
}
string encString = "";
for(int i = 0;i < col; ++i) {
for(int j = 0;j < row; ++j) {
encString += arr[j][order[i]];
}
}
return encString;
}
int main() {
string s;
cout<<"Enter the plain text: ";
cin>>s;
int rounds;
cout<<"Enter the number of rounds: ";
cin>>rounds;
vector<int>order(5);
//For example order of repositioning columns could be 1 0 2 3 4
cout<<"Enter the order of repositioning columns: ";
for(int i = 0;i < 5;i++) {
cin>>order[i];
}
//Multiple Rounds
for(int i = 0;i < rounds; ++i) {
s = encrypt(s,order);
}
cout<<s;
}
/*
Example:
Plain Text: TESSERACTCODING
No. of rounds: 3
Order of Columns: 2 3 0 1 4
Cipher Text: ENTSAEICTRSDOCG
Plain Text: NEOALGO
Order of Columns: 3 0 1 4 2
Cipher Text: LAOEN1O1G1
Time Complexity: O(n*k)
Space Complexity: O(1)
*/