Columnar transposition in C++

Pages: 12
I found this code on the Internet together with a movie, where it is shown how it works (and on the movie everything works fine), but it doesn't quite work for me. Well, after entering the text and the code, not the whole encrypted code is displayed, but only a part of it (depending on the length of the entered text and code). What should I do to display the whole encrypted text?

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <regex>
#include <string>
#include <sstream>

using namespace std;

string getNumberLocation(string keyword, string kywrdNumListStr) {

    int kywrdNumList[keyword.length()];
    //cout << sizeof(kywrdNumList);
    for (int i = 0; i < sizeof(kywrdNumList)/sizeof(kywrdNumList[0]); i++){
        // using ASCII code '0' is 48, '1' 49 and so on until '9' as 57
        kywrdNumList[i] = kywrdNumListStr[i] - 48;
//        cout << kywrdNumList[i] << endl;
    }

    string numLoc = "";
    for (int i = 1; i < keyword.length() + 1; i++) {
        for (int j = 0; j < keyword.length(); j++) {
            if (kywrdNumList[j] == i){
                numLoc += to_string(j);
            }
        }
    }
    return numLoc;
} // getting number location function

string keywordNumAssign(string keyword){
    string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int kywrdNumList[keyword.length()];

    int init = 0;
    for (int i = 0; i < alpha.length(); i ++){
        for (int j = 0; j < keyword.length(); j++) {
            if (alpha[i] == keyword[j]){
                init++;
                kywrdNumList[j] = init;
//                cout << kywrdNumList[j];
            }
        } // inner for
    } // for

    string str = "";
    for (int i = 0; i < sizeof(kywrdNumList)/sizeof(kywrdNumList[0]); i++){
//        cout << kywrdNumList[i] << " ";
        str += to_string(kywrdNumList[i]);
    }
//    string str = to_string(kywrdNumList);
//    cout << str;
    return str;
}

void cipherEncryption(){
    string msg;
    cout << "Enter Plain Text: ";
    getline(cin, msg);

    string keyword;
    cout << "Enter Keyword: ";
    getline(cin, keyword);

    // plain text to uppercase
    for (int i = 0; i < msg.length(); i++){
        msg[i] = toupper(msg[i]);
    }

    // replacting whitespace in plain text
    msg = regex_replace(msg,regex("\\s+"), "");

    // keyword to uppercase
    for (int i = 0; i < keyword.length(); i++){
        keyword[i] = toupper(keyword[i]);
    }

    for (int i = 0; i < keyword.length(); i++){
        cout << keyword[i] << " ";
    }
    cout << endl;

    // assigning numbers to keywords
    string kywrdNumListStr = keywordNumAssign(keyword);

    int kywrdNumList[keyword.length()];
    //cout << sizeof(kywrdNumList);
    for (int i = 0; i < sizeof(kywrdNumList)/sizeof(kywrdNumList[0]); i++){
        // using ASCII code '0' is 48, '1' 49 and so on until '9' as 57
        kywrdNumList[i] = kywrdNumListStr[i] - 48;
//        cout << kywrdNumList[i] << endl;
    }
    for (int i: kywrdNumList){
        cout << i << " ";
    }

    // in case characters don't fit the entire grid perfectly.
    int extraLetters = msg.length() % keyword.length();
//    cout << endl << extraLetters << endl;
    int dummyCharacters = keyword.length() - extraLetters;
//    cout << endl << dummyCharacters << endl;

    if (extraLetters != 0){
        for (int i = 0; i < dummyCharacters; i++) {
            msg += ".";
        }
    }

    int numOfRows = msg.length() / keyword.length();
    // Converting message into a grid
    char arr[numOfRows][keyword.length()];

    int z = 0;
    for (int i = 0; i < numOfRows; i++) {
        for (int j = 0; j < keyword.length(); j++) {
            arr[i][j] = msg[z];
            z++;
        }
    }

    cout << endl;
    for (int i = 0; i < numOfRows; i++) {
        for (int j = 0; j < keyword.length(); j++) {
            cout << arr[i][j] << " ";
        } // inner for loop
        cout << endl;
    } // for loop

    // cipher text generation
    string cipherText = "";
    // getting locations of numbers
    string numLoc = getNumberLocation(keyword, kywrdNumListStr);
    cout << numLoc << endl;
    cout << endl;

    for (int i = 0, k = 0; i < numOfRows; i++, k++) {
        int d;
        if (k == keyword.length()){
            break;
        } else {
//            d = Character.getNumericValue(numLoc.charAt(k));
            d = numLoc[k];
            d = d- '0';
        }
        for (int j = 0; j < numOfRows; j++) {
            cipherText += arr[j][d];
        }
    }

    cout << cipherText;

}

void cipherDecryption(){
    string msg;
    cout << "Enter Cipher Text: ";
    getline(cin, msg);

    string keyword;
    cout << "Enter Keyword: ";
    getline(cin, keyword);

    // keyword to uppercase
    for (int i = 0; i < keyword.length(); i++){
        keyword[i] = toupper(keyword[i]);
    }

    // assigning numbers to keywords
    string kywrdNumListStr = keywordNumAssign(keyword);
    int kywrdNumList[keyword.length()];
    //cout << sizeof(kywrdNumList);
    for (int i = 0; i < sizeof(kywrdNumList)/sizeof(kywrdNumList[0]); i++){
        // using ASCII code '0' is 48, '1' 49 and so on until '9' as 57
        kywrdNumList[i] = kywrdNumListStr[i] - 48;
//        cout << kywrdNumList[i] << endl;
    }

    int numOfRows = msg.length() / keyword.length();
    // Converting message into a grid
    char arr[numOfRows][keyword.length()];

    // getting locations of numbers
    string numLoc = getNumberLocation(keyword, kywrdNumListStr);

    for (int i = 0, k = 0; i < msg.length(); i++, k++) {
        int d;
        if (k == keyword.length()){
            k = 0;
        } else {
            d = numLoc[k];
            d = d- '0';
        }
        for (int j = 0; j < numOfRows; j++, i++) {
            arr[j][d] = msg[i];
//            cout << arr[j][d];
        }
        i--;

    }

    cout << endl;

    string plainText = "";

    for (int i = 0; i < numOfRows; i++) {
        for (int j = 0; j < keyword.length(); j++) {
            plainText += arr[i][j];

        } // inner for loop
    } // for loop

    cout << "Plain Text: " << plainText << endl;


}

int main()
{
    cout << "Columnar Transposition Cipher" << endl;
    cout << "1. Encryption\n2. Decryption\nChoose(1,2): ";
    int choice;
    cin >> choice;
    cin.ignore();

    if (choice == 1){
        cout << endl << "Encryption" << endl;
        cipherEncryption();
    } else if (choice == 2){
        cout << endl << "Decryption" << endl;
        cipherDecryption();
    } else {
        cout << endl << "Wrong Choice" << endl;
    }

    return 0;
}
Given how bad this code is ... I should write your own. I would imagine that was the intention of your exercise anyway.
Sorry to hear that :( Unfortunately I couldn't find any other code that does the same thing as this one, and I thought this one was pretty good,
> Unfortunately I couldn't find any other code that does the same thing as this one
Sooner or later, you'll be presented with a problem which is new and unique (like in the career you imagine for yourself). At which point, no amount of STFW is going to discover any code remotely close to what you want. You'll be screwed (and jobless).

So the question is, when exactly are you planning to take the training wheels off your bicycle?

Read the question again. I want to know what to do to display all encrypted text, not listen to advice. I need ready code, that's why I asked the question. If you don't know how to help me, don't bother.
Vladek wrote:
I need ready code, that's why I asked the question. If you don't know how to help me, don't bother.

Acting entitled to free labor is not going to encourage anyone to help you.

If you don't know how to help me, don't bother.

Well, sonny, I do know how to help you, but you certainly won't want to read the advice.

Congrats for being yet another pest demanding free work. You might as well post this drivel in the Jobs section. Fair warning, we don't come cheap.
So I understand that you can sell me the code. Will you use the money to buy a course in reading comprehension and read the last sentence of the topic I posted? Will you also pay for advice?
not the whole encrypted code is displayed, but only a part of it (depending on the length of the entered text and code). What should I do to display the whole encrypted text?


Use the debugger to debug your code. Trace through it watching the contents of the variables and then when something isn't as expected, then you have found one issue. Repeat until the program works as expected. This is probably what anyone coming to the code for the first time would do. Note that those of us that often reply would probably have to do something like this to find the problem. Note that we give our time freely to try to help - we don't get paid!

If you have found an issue that you don't understand why, then ask a further detailed question.
Another self-entitled snowflake melts down.

> I need ready code, that's why I asked the question.
Then you have already failed.

Sure, you might be able to con yourself and your tutor by handing in homework answers for a few more weeks or months. But at some point, them chickens are coming home to roost.

But if you've been copy/pasting google as your homework for a few weeks now, you're too far behind to catch up with the rest of the class who've been putting in the real effort to understand this stuff.

If you want help, post YOUR OWN CODE, not some half-baked crap you found.

Frankly, it's a waste of everyone's time (yours, mine, your tutor's, your classmates) just to spoon-feed you corrections to "found" code, just so you can remain on the course for one more week.
Because you'll be back next week with the next assignment, but just as clueless how to solve that one as you are at solving this one.
I thought this one was pretty good

For those interested, this is where it comes from AFAICS - it actually runs and spits out stuff - @Vladek is evidently easily pleased.
https://github.com/VoxelPixel/CiphersInCplusplus/blob/master/ctcipher.cpp
So I understand that you can sell me the code.

Heh, for someone who is arrogant enough to accuse others of needing reading comprehension you sure don't have a fucking clue what I said or meant.

Ciao, troll.
you sure don't have a fucking clue what I said or meant
What a disgraceful outburst.
Heh, given the outbursts you ROUTINELY indulge in you really shouldn't be so eager to throw stones.
Last edited on
The difference is I have a privileged position - I have permission.
againtry wrote:
The difference is I have a privileged position - I have permission.


Is that an admission of being a traffic generator?

I wish you would just stick to posting code, you do a good job of that. Cut out the banter, which you know will generate replies.
And what permission do you have TIM?
Your replying couldn't have been further from my mind.
I have authorisation - the only one of those, fully backed up by established moral and ethical responsibilities - especially Kantian in nature.
Insofar as FG's un-gentlemanly outburst at the innocent and defenceless victim in this whole debacle, namely @Vladek, it is no understatement to describe that violent de-motivator as 'banter'. It is a very generous description :+).
QED
Et hoc genus omne
labitur et labetur in omne volubilis ævum
Pages: 12