Saturday 14 May 2011

RC4

I've been reading up on the PDF file format which lead me to reading about RC4. It looked pretty simple (and is), so I decided to knock up a quick implementation in C# based on the information on Wikipedia. The results can be found here.

To use it, include the namespace

using randomworks;

Then do something like this to turn a plaintext string into a ciphertext int array:

string MyKeyString = "Key";
string MyPlaintext = "Plaintext";

RC4 myRC4 = new RC4();
myRC4.SetKey(MyKeyString);
int[] ciphertext = myRC4.Encrypt(MyPlaintext);

If you want to turn the int array into a nice hex string, do something like this:

StringBuilder sb = new StringBuilder();
for (int i=0; i<ciphertext.length; i++)
{
string s = ciphertext[i].ToString("x2");
sb.Append(s);
}
txtCiphertext = sb.ToString();


To decrypt, just pass the decrypt method an encrypted int array and it'll return a decrypted string.

If you find this useful or notice any mistakes, please let me know.

No comments:

Post a Comment