C#中的RC4文件加密和解密 [英] RC4 file encryption and decryption in C#

查看:114
本文介绍了C#中的RC4文件加密和解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件内容很丰富,使用 RC4加密机制加密通过PERL脚本。但我想用C#解密它,我已经在互联网上尝试了所有可用的解决方案。由于在任何网站中都没有使用文件解密/解密的示例,因此显示出不同的错误。任何人都有任何相关的代码请与我分享,这将对我有很大的帮助。



我尝试过:



i已下载 RC54 C#加密但它会引发一些错误。这也是 RC4-Encryption-in-C

解决方案

我有以下代码片段[几年前收集一些来自互联网的地方]

我试过,它很好用

  public   static   class  RC4 
{
public static string 加密(字符串键,字符串数据)
{
编码unicode = Encoding.Unicode;

return Convert.ToBase64String(Encrypt(unicode.GetBytes(key),unicode.GetBytes(data)));
}

public static 字符串解密(字符串键,字符串数据)
{
编码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
。选择(i = > byte )i)
.ToArray();

for int i = 0 ,j = 0 ; i < 256 ; i ++)
{
j =(j + key [i%key.Length] + s [i])& 255 ;

掉期(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 ;

掉期(s,i,j);

return byte )(b ^ s [(s [i] + s [j])& 255 ]);
});
}

private static void 交换( byte [] s, int i, int j)
{
byte c = s [i];

s [i] = s [j];
s [j] = c;
}
} < / byte > < / byte >


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

解决方案

I have following code snippet [collected few years back some where from internet]
I have tried, it works nicely

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>


这篇关于C#中的RC4文件加密和解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆