Asking to write a program is stupid (well, I myself want to write it), but I stumbled on the moment when I need to encrypt data, I can’t figure out how to do it, can someone tell me what to push off (I know the encryption rules, I can't write code)
- Well, that's a good description. - VladD
- And what exactly does not work? What did you try? If it is difficult, try to start, for example, to write a simple permutation. - VladD
- I read the wiki, I wrote that I can decrypt manually, but I can’t write, well, I already have a 5x5 matrix filled in with a keyword, and then the remaining alphabet, there is a final word to encrypt ... I just don’t know how catch bigrams and change their letters programmatically - MaximPro
|
1 answer
Well, here's the code.
Assuming that you correctly filled out the 5x5 matrix in the two-dimensional array mat[5, 5] :
using namespace std; string Encode(string cleartext) { string result; char prev; bool hasPrev = false; for (char curr : cleartext) { if (!hasPrev) // ожидаем конца биграммы { prev = curr; hasPrev = true; continue; } char c1, c2; // правило 1 if (prev == curr) { EncodePair(curr, 'X', result); } else { EncodePair(prev, curr, result); hasPrev = false; } } if (hasPrev) EncodePair(prev, 'X', result); return result; } void EncodePair(char prev, char curr, string& s) { Index idxPrev = indexByChar[prev]; Index idxCurr = indexByChar[curr]; assert(idxPrev.Row != idxCurr.Row || idxPrev.Col != idxCurr.Col); Index resPrev, resNext; if (idxPrev.Row == idxCurr.Row) // правило 2 { resPrev = idxPrev.NextCol(); resNext = idxNext.NextCol(); } else if (idxPrev.Col == idxCurr.Col) // правило 3 { resPrev = idxPrev.NextRow(); resNext = idxNext.NextRow(); } else // правило 4 { resPrev = Index(idxPrev.Row, idxNext.Col); resNext = Index(idxNext.Row, idxPrev.Col); } s += matrix[resPrev.Row, resPrev.Col]; s += matrix[resNext.Row, resNext.Col]; } You will have to fill in the map<char, Index> indexByChar in order to find its row and column in the matrix by a symbol (understand how?). Index structure is obvious:
struct Index { int Row; int Col; Index(int row, int col) { Row = row; Col = col; } static Index NextRow() { int r = (Row + 1) % 5; return Index(r, Col); } static Index NextCol() { int c = (Col + 1) % 5; return Index(Row, c); } }; - too advanced with ++ for me for now, a lot of misunderstandings ... I don’t understand the structure yet, why are we needed, the public static combination is also not clear, this also doesn’t know what var is? what string Encode is (always used char) Of course, it’s clear that this is not a forum problem, why I don’t know, but the code is too cool for me so far - MaximPro
- @MaximPro: Oh damn. I wrote in C # by chance. I will correct now. - VladD
- @MaximPro: redid Now it is better? - VladD
- Better, but I do not understand the declaration string, I always use char, and the questions are almost the same, I understand that it is written advanced, but alas, I can not figure it out yet, but since you helped and sat fooling around with this, I accept your answer - MaximPro
- @MaximPro: In C ++, you should use
std::stringinstead ofchar*. Be sure to master it. - VladD 5:19 pm
|