0

我正在尝试进行文件加密,但我真的不知道如何使用 RC4 使用这些代码进行加密,文本加密和解密都很好。button1 用于文本加密,而 button3 用于文本解密。当我尝试加密图像时出现问题。button2 用于打开和加密/解密文件

    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;
        }
    }


}
4

1 回答 1

1

鉴于这个问题很久没有得到答案,并且已经被查看了相当多的时间,另一个类似这个问题的问题已经得到了正确的回答。Nasreddine 提供的答案应该可以帮助其他任何人查看此问题

于 2015-08-02T16:34:38.057 回答