Encrypting files / image using RC4 cipher [duplicate]

。_饼干妹妹 提交于 2020-01-07 07:48:10

问题


I'm trying to make a file encryption and I really had no clue how to do it with RC4 with these code , text encryption and decryption are fine. button1 is for text encryption and button3 is for text decryption. The problem comes when I tried to encrypt an image. button2 is use to open and encrypt/decrypt a file

    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();  

    }


}

these are the encryption code I took from google

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:


In view of that this question hasn't received an answer for so long, and has been viewed quite a fair bit of times, another question similar to this question has been answered properly. The answer, which should help any others viewing this question, can be seen here, provided by Nasreddine.



来源:https://stackoverflow.com/questions/27754457/encrypting-files-image-using-rc4-cipher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!