1

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Your code is printing the encrypted value, which should be gibberish. I get the correct result when I decrypt it with `console.log(rc4Decrypt(RC4key, valueEncrypted));` – Barmar Oct 14 '19 at 11:30
  • @JamesKPolk Hi, James, thanks for the suggestion. I did compare it to a few other implementations available on this website before asking this question, but it did not fix this issue. I am very confused as to why my Rivest cipher implementation is giving me errors or not returning the plain text input correctly. – user3530957 Oct 14 '19 at 11:32
  • Could you explain the problem more clearly? As I said above, it decrypts your example correctly. – Barmar Oct 14 '19 at 11:34
  • `% 255` should be `% 256` – Barmar Oct 14 '19 at 11:35
  • @Barmar Hi, thanks for your suggestion. Yes, I know that, but it gives errors when the text input is too large, for example more than 2 or 3 paragraphs. Any other solution to solve this issue? Thank you. – user3530957 Oct 14 '19 at 11:35
  • Put an example of the problem in the question, not working code. – Barmar Oct 14 '19 at 11:36
  • Because of that, it will fail whenever the input is longer than 255 characters. – Barmar Oct 14 '19 at 11:37
  • Yes, I did try that too I originally had % 255 set at %256 and it still gave me errors. I tried both methods. Still no luck :( – user3530957 Oct 14 '19 at 11:37
  • There's pseudo-code in the [Wikipedia](https://en.wikipedia.org/wiki/RC4) article – Barmar Oct 14 '19 at 11:40
  • @Barmar thank you, I will have a look at it. – user3530957 Oct 14 '19 at 11:43

0 Answers0