private const int N = 256;
public string Password { get; set; }
public string Text { get; set; }
public RC4(string password, string text)
this.Password = password;
return StrToHexStr(EnDeCrypt(Text));
return EnDeCrypt(HexStrToStr(Text));
public string EnDeCrypt()
StringBuilder cipher = new StringBuilder();
for (int a = 0; a < Text.Length; a++)
k = sbox[(sbox[i] + sbox[j]) % N];
int cipherBy = ((int)Text[a]) ^ k;
cipher.Append(Convert.ToChar(cipherBy));
return cipher.ToString();
public string EnDeCrypt(String text)
StringBuilder cipher = new StringBuilder();
for (int a = 0; a < text.Length; a++)
k = sbox[(sbox[i] + sbox[j]) % N];
int cipherBy = ((int)text[a]) ^ k;
cipher.Append(Convert.ToChar(cipherBy));
return cipher.ToString();
public static string StrToHexStr(string str)
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
int v = Convert.ToInt32(str[i]);
sb.Append(string.Format("{0:X2}", v));
public static string HexStrToStr(string hexStr)
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hexStr.Length; i += 2)
int n = Convert.ToInt32(hexStr.Substring(i, 2), 16);
sb.Append(Convert.ToChar(n));
private void RC4Initialize()
for (int a = 0; a < N; a++)
key[a] = (int)Password[a % n];
for (int a = 0; a < N; a++)
b = (b + sbox[a] + key[a]) % N;
public static void Main()
Console.WriteLine(new RC4("Bouti", "1ac5a3b879e68bf6e8dc3f510aa1aa8e5091674605f29a7f1910c40a67effe30e8ff0807b6134704cad7f828758821bb78296677ba83ce946db15e646fd1a1e67f26f1726d6d3d59b0d6223668a32426").Decrypt());