I've tried everything from changing a few variables, to the integers and also trying to look for answers online. I know that RC4 is not that secure, but I only need it for a simple project.
The issue is whenever I try to encrypt a huge character count of text and when I decrypt it, it returns gibberish as in the decrypted text is not the same as the original text as some of it is missing.
My Code:
var RC4key = "Arc"; //My secret key.
var contentValue = "The-Message"; //My plaintext message that I wish to encrypt.
function rc4Encrypt(key, pt) {
s = new Array();
for (var i = 0; i < 256; i++) {
s[i] = i;
}
var j = 0;
var x;
for (i = 0; i < 256; i++) {
j = (j + s[i] + key.charCodeAt(i % key.length)) % 255;
x = s[i];
s[i] = s[j];
s[j] = x;
}
i = 0;
j = 0;
var ct = '';
for (var y = 0; y < pt.length; y++) {
i = (i + 1) % 255;
j = (j + s[i]) % 255;
x = s[i];
s[i] = s[j];
s[j] = x;
ct += String.fromCharCode(pt.charCodeAt(y) ^ s[(s[i] + s[j]) % 255]);
}
return ct;
}
/*rc4Decrypt is used to decrypt the encrypted text to the original as long as the key is correct.*/
function rc4Decrypt(key, ct) {
return rc4Encrypt(key, ct);
}
/*I am trying to encrypt it and placing the value in "valueEncrypted" variable.*/
var valueEncrypted = rc4Encrypt(RC4key, contentValue);
/*I am outputting the value to the console.*/
console.log(valueEncrypted);
Please don't apply base64 or any other sort of encoding schemes as it is not required.
Yes, I know I can use CryptoJS or other libraries, but I prefer to use it in vanilla (pure) JavaScript as it is required for my project to not use any dependencies to achieve this.
Thank you to everyone who has read my post and given it a thought. I really appreciate it.