Click here to Skip to main content
15,868,109 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am having a file that has huge content in it which was encrypted using RC4 encryption mechanism by PERL script. But i want to decrypt it in C# and i have tried all the way available solutions in the internet. all showing different errors since there was no example using file decryption/decryption in any of the websites. Anyone have any code regarding this please share it with me that would be an great help to me.

What I have tried:

i have downloaded the RC54 C# encryption but it's throwing some error. and this as well RC4-Encryption-in-C
Posted
Updated 6-Jun-16 20:37pm

1 solution

I have following code snippet [collected few years back some where from internet]
I have tried, it works nicely
C#
public static class RC4
{
   public static string Encrypt(string key, string data)
   {
      Encoding unicode = Encoding.Unicode;
 
      return Convert.ToBase64String(Encrypt(unicode.GetBytes(key), unicode.GetBytes(data)));
   }
 
   public static string Decrypt(string key, string data)
   {
      Encoding unicode = Encoding.Unicode;
 
      return unicode.GetString(Encrypt(unicode.GetBytes(key), Convert.FromBase64String(data)));
   }
 
   public static byte[] Encrypt(byte[] key, byte[] data)
   {
      return EncryptOutput(key, data).ToArray();
   }
 
   public static byte[] Decrypt(byte[] key, byte[] data)
   {
      return EncryptOutput(key, data).ToArray();
   }
 
   private static byte[] EncryptInitalize(byte[] key)
   {
      byte[] s = Enumerable.Range(0, 256)
        .Select(i => (byte)i)
        .ToArray();
 
      for (int i = 0, j = 0; i < 256; i++)
      {
         j = (j + key[i % key.Length] + s[i]) & 255;
 
         Swap(s, i, j);
      }
 
      return s;
   }
 
   private static IEnumerable<byte> EncryptOutput(byte[] key, IEnumerable<byte> data)
   {
      byte[] s = EncryptInitalize(key);
 
      int i = 0;
      int j = 0;
 
      return data.Select((b) =>
      {
         i = (i + 1) & 255;
         j = (j + s[i]) & 255;
 
         Swap(s, i, j);
 
         return (byte)(b ^ s[(s[i] + s[j]) & 255]);
      });
   }
 
   private static void Swap(byte[] s, int i, int j)
   {
      byte c = s[i];
 
      s[i] = s[j];
      s[j] = c;
   }
}</byte></byte>
 
Share this answer
 
Comments
Gold$Coin 7-Jun-16 2:45am    
I have tried that as well. but the problem here is the decrypted file from the PERL. i was not aware of PERL programming. the solution what was available in the internet will work good when we use the same solution for both encryption and decryption but the problem here is the decrypted file. how you got what i am saying correct? looking forward for you correct reply
Sergey Alexandrovich Kryukov 7-Jun-16 2:54am    
There is no any encryption/decryption code here. Where? :-)
Everything comes to non-existing methods EncryptOutput/DecryptOutput.
And all the code with Encoding and base64 is totally irrelevant.
—SA
koolprasad2003 7-Jun-16 3:05am    
I know there is no in-built method used in above code, but as per the RC4 algorithm theory 'its just generates a keystream using bit-wise exclusive-or. Basically it uses below two things to create steam
1.A permutation of all 256 possible bytes (denoted "S" below).
2.Two 8-bit index-pointers (denoted "i" and "j").
so the algorithm will be as follows [WIKI source]
for i from 0 to 255
S[i] := i
endfor
j := 0
for i from 0 to 255
j := (j + S[i] + key[i mod keylength]) mod 256
swap values of S[i] and S[j]
endfor
and same code is done in above C# source.
[I got same question when I use above code but then after reading its theory I came to know]


Sergey Alexandrovich Kryukov 7-Jun-16 3:07am    
Good, but why it all is not in your "solution" which actually shows nothing?
—SA
koolprasad2003 7-Jun-16 3:13am    
I will update it :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900