Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 ?
Posted 15-Nov-12 0:24am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try the following article : RC4 Encryption Algorithm: C# Version[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 420
1 CHill60 410
2 Sergey Alexandrovich Kryukov 354
3 BillWoodruff 190
4 PIEBALDconsult 160


Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 15 Nov 2012
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