2015-01-03 42 views
0

我试图做一个文件加密,我真的不知道如何与RC4 与这些代码,文本加密和解密罚款。 button1用于文本加密,button3用于文本解密。 我试图加密图像时出现问题。 BUTTON2是用来打开和加密/解密文件使用RC4密码加密文件/图像

private void button1_Click(object sender, EventArgs e) 
    { 
     RC4 rc4 = new RC4(txtPassword.Text, txtText.Text); 
     txtHexDump.Text = RC4.StrToHexStr(rc4.EnDeCrypt()); 


    } 



    private void button2_Click(object sender, EventArgs e) 
    { 
     openFileDialog1.Title = "Open Word or Text File"; 
     openFileDialog1.Filter = "All Files (*.*)|*.*"; 
     if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 

      RC4 rc4 = new RC4(txtPassword.Text, Encoding.UTF32.GetString(GetBytesFromFile(openFileDialog1.FileName))); 
      SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 

      saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; 
      saveFileDialog1.FilterIndex = 2; 
      saveFileDialog1.RestoreDirectory = true; 
      Stream mystream; 
      if (saveFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       if ((mystream = saveFileDialog1.OpenFile()) != null) 
       { 
        StreamWriter wText = new StreamWriter(mystream); 

        wText.Write(rc4.EnDeCrypt()); 

        mystream.Close(); 
       } 
      } 
     } 




    } 


    public static byte[] GetBytesFromFile(string fullFilePath) 
    { 
     // this method is limited to 2^32 byte files (4.2 GB) 

     FileStream fs = null; 
     try 
     { 
      fs = File.OpenRead(fullFilePath); 

      byte[] bytes = new byte[fs.Length]; 

      fs.Read(bytes, 0, Convert.ToInt32(fs.Length)); 
      return bytes; 
     } 
     finally 
     { 

      if (fs != null) 
      { 

       fs.Close(); 
       fs.Dispose(); 
      } 
     } 

    } 

    private void saveFileDialog1_FileOk(object sender, CancelEventArgs e) 
    { 

    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     RC4 rc4 = new RC4(txtPassword.Text, txtHexDump.Text); 
     rc4.Text = RC4.HexStrToStr(txtHexDump.Text); 
     txtText.Text = rc4.EnDeCrypt(); 

    } 


} 

这些都是加密的代码,我从谷歌

public class RC4 
{ 
    private const int N = 256; 
    private int[] sbox; 
    private string password; 
    private string text; 

    public RC4(string password, string text) 
    { 
     this.password = password; 
     this.text = text; 
    } 

    public RC4(string password) 
    { 
     this.password = password; 
    } 

    public string Text 
    { 
     get { return text; } 
     set { text = value; } 
    } 

    public string Password 
    { 
     get { return password; } 
     set { password = value; } 
    } 

    public string EnDeCrypt() 
    { 
     RC4Initialize(); 

     int i = 0, j = 0, k = 0; 
     StringBuilder cipher = new StringBuilder(); 
     for (int a = 0; a < text.Length; a++) 
     { 
      i = (i + 1) % N; 
      j = (j + sbox[i]) % N; 
      int tempSwap = sbox[i]; 
      sbox[i] = sbox[j]; 
      sbox[j] = tempSwap; 

      k = sbox[(sbox[i] + sbox[j]) % N]; 
      int cipherBy = ((int)text[a])^k; //xor operation 
      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)); 
     } 
     return sb.ToString(); 
    } 

    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)); 
     } 
     return sb.ToString(); 
    } 

    private void RC4Initialize() 
    { 
     sbox = new int[N]; 
     int[] key = new int[N]; 
     int n = password.Length; 
     for (int a = 0; a < N; a++) 
     { 
      key[a] = (int)password[a % n]; 
      sbox[a] = a; 
     } 

     int b = 0; 
     for (int a = 0; a < N; a++) 
     { 
      b = (b + sbox[a] + key[a]) % N; 
      int tempSwap = sbox[a]; 
      sbox[a] = sbox[b]; 
      sbox[b] = tempSwap; 
     } 
    } 


} 
+1

欢迎来到StackOverflow。请更好地描述你的问题。你只是提到你有一个。但究竟是什么呢?你期望发生什么?会发生什么呢?试图将图像文件数据读入字符串的 – Codo

+0

将是一个问题。请参阅http://stackoverflow.com/q/7217627/1070452其中很多人在这里RC4方法的工作 – Plutonix

+0

感谢您的帮助,让程序运行通过更改你给出的代码的一点:D –

回答

1

了针对这个问题一直没有得到答复了这么久,并且已经被看好相当一段时间,另一个类似于这个问题的问题已经得到了妥善的回答。答案应该可以帮助其他人查看这个问题,Nasriddine提供了seen here