偶尔得到' System.Security.Cryptography.CryptographicException:Bad Data'解密字符串

时间:2015-10-14 04:05:56

标签: c# encryption cryptography

我已经实现了一个如下所示的类,它负责将文本数据加密/解密到名为TextEncryptionData的实用程序类。

它适用于%99.99的时间。但有时(非常罕见)它无法解密文本,我不知道为什么会发生这种情况。我实施它的方式有问题吗?

public class SubscriptionStateEncryptionLogic : IEncryptionLogic
{
    private static byte[] KEY = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 71 };
    private static byte[] IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8};
    public EncryptionData Encrypt(EncryptionData source)
    {
        if (source == null)
        {
            return null;
        }

        try
        {
            TextEncryptionData data = source as TextEncryptionData;
            using (TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider())
            {
                cryptoProvider.KeySize = 128;
                cryptoProvider.Key = KEY;
                cryptoProvider.IV = IV;
                cryptoProvider.Mode = CipherMode.CBC;
                cryptoProvider.Padding = PaddingMode.PKCS7;
                ICryptoTransform transform = cryptoProvider.CreateEncryptor();
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
                    {
                        using (StreamWriter writer = new StreamWriter(cryptoStream))
                        {
                            writer.Write(data.Data);
                            writer.Flush();
                            cryptoStream.FlushFinalBlock();
                            writer.Flush();
                            return new TextEncryptionData() { Data = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length) };
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new SubscriptionStateEncryptionException("Error in encrypting the subscription status.", ex);
        }
    }

    public EncryptionData Decrypt(EncryptionData source)
    {
        if (source == null)
        {
            return null;
        }

        try
        {
            TextEncryptionData data = source as TextEncryptionData;
            using (TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider())
            {
                cryptoProvider.KeySize = 128;
                cryptoProvider.Key = KEY;
                cryptoProvider.IV = IV;
                cryptoProvider.Mode = CipherMode.CBC;
                cryptoProvider.Padding = PaddingMode.PKCS7;
                ICryptoTransform transform = cryptoProvider.CreateDecryptor();
                using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(data.Data)))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read))
                    {
                        using (StreamReader reader = new StreamReader(cryptoStream))
                        {
                            return new TextEncryptionData() { Data = reader.ReadToEnd() };
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new SubscriptionStateEncryptionException("Error in decrypting the subscription status.", ex);
        }
    }
}

0 个答案:

没有答案