此项目显示错误。“CryptographicException未被用户代码处理。不良数据“ [英] This project displays an error."CryptographicException was unhandled by user code. Bad Data"

查看:51
本文介绍了此项目显示错误。“CryptographicException未被用户代码处理。不良数据“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public static string Decrypt(string cipherString,bool useHashing)
{
byte [] keyArray;
byte [] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key =(string)settingsReader.GetValue(connection,typeof(String));
if(useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);

TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;

ICryptoTransform cTransform = tdes.CreateDecryptor();
byte [] resultArray = cTransform.TransformFinalBlock(toEncryptArray,0,toEncryptArray.Length);
tdes.Clear();
返回UTF8Encoding.UTF8.GetString(resultArray);
}
}

解决方案

我们无法告诉你错误是什么:你需要首先用调试器查看它,然后找出究竟发生了什么。

首先查看输入数据:该代码中的错误可能不是错误,而是在它尝试处理的字符串中。

无论哪种方式,我们无法帮助,因为我们无法访问您的数据或密钥字符串!



抱歉,您将不得不自己解决这个问题!

public static string Decrypt(string cipherString, bool useHashing)
    {
        byte[] keyArray;
        byte[] toEncryptArray = Convert.FromBase64String(cipherString);
        System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
        string key = (string)settingsReader.GetValue("connection", typeof(String));
        if (useHashing)
        {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
            hashmd5.Clear();
        }
        else
            keyArray = UTF8Encoding.UTF8.GetBytes(key);

        TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
        tdes.Key = keyArray;
        tdes.Mode = CipherMode.ECB;
        tdes.Padding = PaddingMode.PKCS7;

        ICryptoTransform cTransform = tdes.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        tdes.Clear();
        return UTF8Encoding.UTF8.GetString(resultArray);
    }
}

解决方案

We can't tell you from that what the error is: you need to start by looking at it with the debugger and find out exactly what is going on.
Start by looking at the input data: the chances are that isn't an error in that code, but in the string it is trying to process.
Either way, we can't help because we have no access to your data or key string!

Sorry, but you are going to have to work this out yourself!


这篇关于此项目显示错误。“CryptographicException未被用户代码处理。不良数据“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆