【问题标题】:Decrypt string using Bouncy Castle RC4 algorithm in C#在 C# 中使用 Bouncy Castle RC4 算法解密字符串
【发布时间】:2016-04-07 13:40:59
【问题描述】:

我正在尝试使用库 Bouncy Castle 进行解密。但解密后的字符串不正确。我得到的值是 Aa1ŽYY445Ló,但正确的值应该是 Aa11YY445LL。我做错了什么?当我尝试在http://rc4.online-domain-tools.com/ 上解密字符串时,我得到了正确的结果。

代码示例:

string textToDecrypt = HexDumper.FromHex("E5497380DC724B28284D80");
var key = Encoding.UTF8.GetBytes("heslo");
var cipher = new RC4Engine();
cipher.Init(true, new KeyParameter(key));

byte[] inBytes = UTF8Encoding.GetEncoding(1252).GetBytes(textToDecrypt);
byte[] outBuffer = new byte[1024 * 4];
cipher.ProcessBytes(inBytes, 0, inBytes.Length, outBuffer, 0);

// Output must be 41 61 31 31 59 59 34 34 35 4c 4c -> Aa11YY445LL
var textDecrypted = ASCIIEncoding.GetEncoding(1252).GetString(outBuffer);
int indexOf0 = textDecrypted.IndexOf("\0");
if (indexOf0 > 0)
{
    textDecrypted = textDecrypted.Substring(0, indexOf0);
    MessageBox.Show(textDecrypted);
}

public static string FromHex(string hexString)
{
    string StrValue = "";
    while (hexString.Length > 0)
    {
        StrValue += System.Convert.ToChar(System.Convert.ToUInt32(hexString.Substring(0, 2), 16)).ToString();
        hexString = hexString.Substring(2, hexString.Length - 2);
    }
    return StrValue;
}

【问题讨论】:

    标签: c# encryption bouncycastle rc4-cipher


    【解决方案1】:

    问题在于您的 FromHex 函数。

    将其与How can I convert a hex string to a byte array? 的最佳答案交换并得到正确结果。

    但不确定您的 FromHex 出了什么问题(在功能方面)——您可能应该弄清楚这一点。

    【讨论】:

      猜你喜欢
      • 2011-08-20
      • 2015-06-24
      • 2012-04-25
      • 2012-06-15
      • 1970-01-01
      • 2018-01-13
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多