Click here to Skip to main content

Member 9552449 asked:

Open original thread
private void button1_Click(object sender, EventArgs e)
{
    byte[] key = UnicodeEncoding.Unicode.GetBytes("a");
    byte[] data = UnicodeEncoding.Unicode.GetBytes(richTextBox1.Text);
    RC4(ref data, key);
    richTextBox1.Text = UnicodeEncoding.Unicode.GetString(data);
}
 
private void button2_Click(object sender, EventArgs e)
{
    byte[] key = UnicodeEncoding.Unicode.GetBytes("a");
    byte[] data = UnicodeEncoding.Unicode.GetBytes(richTextBox1.Text);
    RC4(ref data, key);
    richTextBox1.Text = UnicodeEncoding.Unicode.GetString(data);
}
 
public void RC4(ref Byte[] bytes, Byte[] key)
{
    Byte[] s = new Byte[256];
    Byte[] k = new Byte[256];
    Byte temp;
    int i, j;
 
    for (i = 0; i < 256; i++)
    {
        s[i] = (Byte)i;
        k[i] = key[i % key.GetLength(0)];
    }
 
    j = 0;
    for (i = 0; i < 256; i++)
    {
        j = (j + s[i] + k[i]) % 256;
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
 
    i = j = 0;
    for (int x = 0; x < bytes.GetLength(0); x++)
    {
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
        int t = (s[i] + s[j]) % 256;
        bytes[x] ^= s[t];
    }
}
 
I am doing this RC4 encryption, i don't know why but when i do the decryption for some reason it doesnt go back to the original text (most of the text does, but some letter doesnt). Do you have any idea why ?
Tags: C#

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100