使用RC4密码加密文件/图像

时间:2015-01-03 11:44:51

标签: c# rc4-cipher

我试图进行文件加密,我真的不知道如何使用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;
        }
    }


}

1 个答案:

答案 0 :(得分:1)

鉴于这个问题长期以来没有得到答案,并且已被视为相当多的时间,另一个类似于这个问题的问题已得到妥善回答。答案,应该有助于其他人查看这个问题,可以是seen here,由Nasreddine提供。