本文介绍了C#解密中的RC4实现不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button1_Click(object sender, EventArgs e)
{
    byte[] key = UnicodeEncoding.Unicode.GetBytes("a");
    byte[] data = UnicodeEncoding.Unicode.GetBytes(richTextBox1.Text);
    RC4(ref data, key);
    richTextBox1.Text = UnicodeEncoding.Unicode.GetString(data);
}

private void button2_Click(object sender, EventArgs e)
{
    byte[] key = UnicodeEncoding.Unicode.GetBytes("a");
    byte[] data = UnicodeEncoding.Unicode.GetBytes(richTextBox1.Text);
    RC4(ref data, key);
    richTextBox1.Text = UnicodeEncoding.Unicode.GetString(data);
}

public void RC4(ref Byte[] bytes, Byte[] key)
{
    Byte[] s = new Byte[256];
    Byte[] k = new Byte[256];
    Byte temp;
    int i, j;

    for (i = 0; i < 256; i++)
    {
        s[i] = (Byte)i;
        k[i] = key[i % key.GetLength(0)];
    }

    j = 0;
    for (i = 0; i < 256; i++)
    {
        j = (j + s[i] + k[i]) % 256;
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }

    i = j = 0;
    for (int x = 0; x < bytes.GetLength(0); x++)
    {
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;
        temp = s[i];
        s[i] = s[j];
        s[j] = temp;
        int t = (s[i] + s[j]) % 256;
        bytes[x] ^= s[t];
    }
}



我正在执行RC4加密,我不知道为什么,但是当我出于某种原因进行解密时,它不会返回原始文本(大多数文本会恢复,但是有些字母不会).您知道为什么吗?



I am doing this RC4 encryption, i don''t know why but when i do the decryption for some reason it doesnt go back to the original text (most of the text does, but some letter doesnt). Do you have any idea why ?

推荐答案


这篇关于C#解密中的RC4实现不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 04:08