2015-01-03 77 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