在解密密钥时,我得到了错误:
CryptographicException未被用户代码处理。 要解密的数据长度无效。
现在我要做的是,检查给定的字符串是否能够解密。如果它能够解密,那么我想执行下面的代码。这样我就不能得到一个错误。
var byteBuff = Convert.FromBase64String(value);
var strDecrypted = Encoding.ASCII.GetString(
objDesCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));有什么办法检查吗?
发布于 2015-01-20 13:36:36
我找到了解决办法。必须检查有效的base64字符串:
if ((value.Length % 4 == 0) && Regex.IsMatch(value, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None))
{
var byteBuff = Convert.FromBase64String(value);
decryptedString =
Encoding.ASCII.GetString(
objDesCrypto.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
}发布于 2013-10-30 07:43:06
我认为下面的问题可能是:如果它不能解密,您的代码想要做什么。无论如何,考虑到这一点,您总是可以像这样使用try...catch:
try { /* your code */ }
catch (CryptographicException e) { /* whatever you need to if it is not able to */ }https://stackoverflow.com/questions/19675103
复制相似问题