"不良数据"尝试解密字符串时出现异常

时间:2018-02-06 13:46:16

标签: c# encryption

我试图找出我收到错误数据的原因"当我尝试解密一个字符串时出现异常。当我从文本框中拉出键串时,它可以工作

示例:string key = textbox1.text.tostring();

但是当我只是手动将密钥放入代码中时它就无法工作,如附图所示。

示例:string key =" Test"

它适用于双向加密,但不适用于解密

Error showing

不起作用。

 private void decrypt(string input)
    {
        string key = "test";
        TripleDESCryptoServiceProvider desCryptoProvider = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider();

        byte[] byteHash;
        byte[] byteBuff;

        byteHash = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));
        desCryptoProvider.Key = byteHash;
        desCryptoProvider.Mode = CipherMode.ECB; //CBC, CFB
        byteBuff = Convert.FromBase64String(input);
        string plaintext = Encoding.UTF8.GetString(desCryptoProvider.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
        InputText.Text = plaintext;

    }

是否有效。

  private void decrypt(string input, string key)
    {

        TripleDESCryptoServiceProvider desCryptoProvider = new TripleDESCryptoServiceProvider();
        MD5CryptoServiceProvider hashMD5Provider = new MD5CryptoServiceProvider();

        byte[] byteHash;
        byte[] byteBuff;

        byteHash = hashMD5Provider.ComputeHash(Encoding.UTF8.GetBytes(key));
        desCryptoProvider.Key = byteHash;
        desCryptoProvider.Mode = CipherMode.ECB; //CBC, CFB
        byteBuff = Convert.FromBase64String(input);
        string plaintext = Encoding.UTF8.GetString(desCryptoProvider.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
        InputText.Text = plaintext;

    }

0 个答案:

没有答案