Answer the question
In order to leave comments, you need to log in
RC4, what did I do wrong?
I found an implementation of rc4 for JS, by analogy I wrote for C ++. In js I encrypt 1234 with key 1, I get the result. I copy the result from the browser tab, paste it into the C ++ algorithm, try to decrypt it, but always an error of one character. I can not find where I made a mistake and why the error. Could it be a coding issue?
function rc4(key, str) {
var s = [], j = 0, x, res = '';
for (var i = 0; i < 256; i++) {
s[i] = i;
}
for (i = 0; i < 256; i++) {
j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
}
i = 0;
j = 0;
for (var y = 0; y < str.length; y++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
res += String.fromCharCode(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
}
return res;
}
var key = "1";
var str = "1234";
int main()
{
string key = "1";
string text = "Q(ñY";
string code = string();
code = my_rc4((char*)key.c_str(), key.size(), (char*)text.c_str(), text.size());
cout << "text: " << text << "\n";
cout << "code: " << code << "\n";
system("pause");
return 0;
}
string my_rc4(char* key, size_t key_size, char* text, size_t text_size)
{
int s[256];
int j = 0;
int x = int();
string res = string();
for (size_t i = 0; i < 256; ++i)
{
s[i] = i;
}
for (size_t i = 0; i < 256; ++i)
{
j = (j + s[i] + (int)key[i % key_size]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
}
int i = 0;
j = 0;
for (int y = 0; y < text_size; ++y)
{
i = (i + 1) % 256;
j = (j + s[i]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question