Look Zaap, whatever misunderstanding you have with your encryption and decryption algorithm it works as I expected.
Keys :
1 - 01
2 - 02
3 - 01
4 - 02
So concat key is 01020102
|
Your concat key in above case is not 01020102, but 1212.
Your algorithm simply eat the 0's in front of 01 to 09.
uint64_t key = 01020102;
is equal to this
uint64_t key = 270402;
When you put 0 in front of int number, compiler think it as OCT number and make implicit conversation: 0 "Hmm, it is OCT" so 1020102 = int 270402; You are not able to make concate key starting with mini-key 01(02...) and put it in unit64_t. Of course you can make 01 in second mini-key position 1101 and next ones, but never in first. If you use string instead unit64_t, it will be ok.
This ban, of course, does not apply to the combined keyAll = key + key2 + key3+key4
uint64_t key = 0;
uint64_t key2 = 1;
uint64_t key3 = 0;
uint64_t key4 = 2;
|
This will give keyAll = "0102" starting with 01. And from 111111... you to have your 232323... result.
Original Message: 1111111111111111111111111111111111
Crypt Message: 2323232323232323232323232323232323
Ucrypt Message: 1111111111111111111111111111111111
|
This is also not true:
Keys become :
1 - 01020102
2 - 02030203
3 - 03040304
4 - 04050405
|
Your real keys are:
1 - 1212
2 - 2323
3 - 3434
4 - 4545
But this is not matter if your crypt and uncrypt function used the same keys! This is only problem of your understanding of keys forming process.
My key is 01 02 01 02
So "1 1 1 1" becomes
'1'+(01) -> '2'
'1'+(02) -> '3'
So "2 3 2 3" (without space).
I was talking about the char symbol, not the int value behind the char symbol.
Edit : '=' ascii code is 51, so the exact value added is 12. Is that a coincidence ?
(01) (02) -> (1) (2) -> 12
Edit 2 : 01030103 give >>>>> so no it's not a coincidence, there is something wrong here.
|
Here you don't have
'1'+(01) -> '2'
'1'+(02) -> '3'
|
but as I said
'1'+12
'1'+12
Ascii for '1' is 049. So you add 49 + 12 with result 61 which is exactly "=" symbol.
I was talking about the char symbol, not the int value behind the char symbol. |
Not you working exactly with int values behind the char symbols.
'1'+1 != '2' but '1'+1 = 50 which is the int value of '2'
Edit 2 : 01030103 give >>>>> so no it's not a coincidence, there is something wrong here. |
The same case. Your key is not 01030103 but 1313
'1' + 13 = 49 + 13 = 62 which is ">" symbol in Ascii.
But as I said, this is minor issues mainly related to your misunderstanding, not to bug itself.
Now IMPORTANT part :
Please make this experiment I tell you.
1. Make file contain 100 000 "1".
2. Use exactly the keys "12131213" to crypt it.
1 2 3 4
|
uint64_t key = 12131213;
uint64_t key2 = 12131213;
uint64_t key3 = 12131213;
uint64_t key4 = 12131213;
| |
3. Send and Recv crypt message via Socket several times until you get a bug during uncrypt.
4. Show me result from shifting keys problem.
I expected something like this:
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
|
#include <iostream>
#include <vector>
#include <string>
using namespace std;
std::string uncrypt(const uint64_t& key, const uint64_t& key2, const uint64_t& key3, const uint64_t& key4, std::string msg) {
std::string tk(to_string(key) + to_string(key2) + to_string(key3) + to_string(key4)); //text key
vector<int> tmp_tk;
if (tk.size() % 2 != 0) tk += tk[0];
while (tk.size() > 0) {
tmp_tk.push_back(atoi(tk.substr(0, 2).c_str()));
tk = tk.substr(2);
}
for (int i(0); i < tmp_tk.size(); ++i) tk += (char)tmp_tk[i];
while (tk.size() < msg.size()) {
tk += tk;
}
if (msg.size() <= tk.size()) {
std::string tmp = "";
for (int i(0); i < msg.size(); ++i) {
char tmp_val = msg[i] - tk[i];
if (tmp_val < -127) tmp_val += 255;
tmp += tmp_val;
}
return tmp;
}
}
std::string crypt(const uint64_t& key, const uint64_t& key2, const uint64_t& key3, const uint64_t& key4, std::string msg) {
std::string tk(to_string(key) + to_string(key2) + to_string(key3) + to_string(key4)); //text key
vector<int> tmp_tk;
if (tk.size() % 2 != 0) tk += tk[0];
while (tk.size() > 0) {
tmp_tk.push_back(atoi(tk.substr(0, 2).c_str()));
tk = tk.substr(2);
}
for (int i(0); i < tmp_tk.size(); ++i) tk += (char)tmp_tk[i];
while (tk.size() < msg.size()) {
tk += tk;
}
if (msg.size() <= tk.size()) {
std::string tmp = "";
for (int i(0); i < msg.size(); ++i) {
char tmp_val = msg[i] + tk[i];
//if (tmp_val < -127) tmp_val += 255;
tmp += tmp_val;
}
return tmp;
}
}
int main()
{
std::string msgO{ "1111111111111111111111111111111111111" };
uint64_t key = 12131213;
uint64_t key2 = 12131213;
uint64_t key3 = 12131213;
uint64_t key4 = 12131213;
std::string msgC{ crypt(key, key2, key3, key4, msgO) };
std::string msgU{ uncrypt(key, key2, key3, key4, msgC) };
std::string msgInsert{ "0" };
cout << "Original Message: "<< msgO << endl;
cout << "Crypt Message: " << msgC << endl;
cout << "Ucrypt Message: " << msgU << endl << endl;
cout << "But now we will insert two '0's inside Crypt message... " << endl;
cout << msgC << endl;
cout << "...simulating send-recv parasites '0's adding." << endl << endl;
cout << "So our destored Crypt Message become:" << endl;
msgC.insert(10, msgInsert);
msgC.insert(27, msgInsert);
cout << msgC << endl << endl;
cout << "When we try to uncrypt this distored message, result will be: " << endl;
cout << uncrypt(key, key2, key3, key4, msgC) << endl;
}
| |
Original Message: 1111111111111111111111111111111111111
Crypt Message: =>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=
Ucrypt Message: 1111111111111111111111111111111111111
But now we will insert two '0's inside Crypt message...
=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=>=
...simulating send-recv parasites '0's adding.
So our destored Crypt Message become:
=>=>=>=>=>0=>=>=>=>=>=>=>=>0=>=>=>=>=>=
When we try to uncrypt this distored message, result will be:
1111111111$0202020202020202#11111111111
|
Do you see how result
alternate? If problem is shifting it will show it clearly.
P.s. And Zaap, you must organize your brain little bit. Usually many programmers when they have any problems, they start to chaotically move in their code, to change that and that, hoping to catch the problem accidentally. In this way, they begin to change the conditions in which the problem occurs and often make the situation even more complicated. Think of bugs as knots. If you just try to untie some tangled rope without thinking about your strategy first, it will get tangled even more. So stop to change chaotically code lines, please, and do what I tell you. This your bug, I can catch and deal with it under 1 hour if I have the right conditions - you make it hard, 3 days problem - doing what you want. I know it's a matter of honor to catch it yourself - but FUCK the BUG, I rather want to teach you how to think when you have such problems in future. So do what I want, and you will see how easy things will become.