C# 使用通用Windows应用程序解密AES

C# 使用通用Windows应用程序解密AES,c#,encryption,cryptography,C#,Encryption,Cryptography,我使用下面的方法在SQL数据库中加密数据。这在我的web应用程序中运行良好。我正在将通用windows应用程序添加到项目中,但无法使用与web应用程序相同的解密方法(如System.Security.Cryptography无法引用)。我曾尝试使用Windows.Security.Cryptography(由此)实现此功能,但出现以下错误 加密引擎。解密(symmKey、cipherBuffer、saltMaterial);“数据错误(循环冗余检查)。(HRESULT异常:0x80070017)

我使用下面的方法在SQL数据库中加密数据。这在我的web应用程序中运行良好。我正在将通用windows应用程序添加到项目中,但无法使用与web应用程序相同的
解密
方法(如
System.Security.Cryptography
无法引用)。我曾尝试使用
Windows.Security.Cryptography
(由此)实现此功能,但出现以下错误

加密引擎。解密(symmKey、cipherBuffer、saltMaterial);“数据错误(循环冗余检查)。(HRESULT异常:0x80070017)”

我尝试过改变
Decrypt
方法中的一些规则,但我不知道为什么它不起作用,而且我不是这方面的专家。任何帮助都将不胜感激

用于加密和解密的web应用程序中的方法

public static class Cryptography
{


    private static int _iterations = 2;
    private static int _keySize = 256;

    private static string _hash = "SHA1";
    private static string _salt = "mysaltpassword"; // Random
    private static string _vector = "8947az34awl34kjq"; // Random
    private static string pw = "MyPassword";




    public static string Encrypt(string value)
    {
        return Encrypt<AesManaged>(value);
    }
    public static string Encrypt<T>(string value)
            where T : SymmetricAlgorithm, new()
    {
        ASCIIEncoding Encoder = new ASCIIEncoding();

        byte[] vectorBytes = Encoder.GetBytes(_vector);
        byte[] saltBytes = Encoder.GetBytes(_salt);
        byte[] valueBytes = Encoder.GetBytes(value);

        byte[] encrypted;
        using (T cipher = new T())
        {
            PasswordDeriveBytes _passwordBytes =
                new PasswordDeriveBytes(pw, saltBytes, _hash, _iterations);
            byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);

            cipher.Mode = CipherMode.CBC;

            using (ICryptoTransform encryptor = cipher.CreateEncryptor(keyBytes, vectorBytes))
            {
                using (MemoryStream to = new MemoryStream())
                {
                    using (CryptoStream writer = new CryptoStream(to, encryptor, CryptoStreamMode.Write))
                    {
                        writer.Write(valueBytes, 0, valueBytes.Length);
                        writer.FlushFinalBlock();
                        encrypted = to.ToArray();
                    }
                }
            }
            cipher.Clear();
        }
        return Convert.ToBase64String(encrypted);
    }

    public static string Decrypt(string value)
    {
        return Decrypt<AesManaged>(value);
    }
    public static string Decrypt<T>(string value) where T : SymmetricAlgorithm, new()
    {
        ASCIIEncoding Encoder = new ASCIIEncoding();

        byte[] vectorBytes = Encoder.GetBytes(_vector);
        byte[] saltBytes = Encoder.GetBytes(_salt);
        byte[] valueBytes = Convert.FromBase64String(value);

        byte[] decrypted;
        int decryptedByteCount = 0;

        using (T cipher = new T())
        {
            PasswordDeriveBytes _passwordBytes = new PasswordDeriveBytes(pw, saltBytes, _hash, _iterations);
            byte[] keyBytes = _passwordBytes.GetBytes(_keySize / 8);

            cipher.Mode = CipherMode.CBC;

            try
            {
                using (ICryptoTransform decryptor = cipher.CreateDecryptor(keyBytes, vectorBytes))
                {
                    using (MemoryStream from = new MemoryStream(valueBytes))
                    {
                        using (CryptoStream reader = new CryptoStream(from, decryptor, CryptoStreamMode.Read))
                        {
                            decrypted = new byte[valueBytes.Length];
                            decryptedByteCount = reader.Read(decrypted, 0, decrypted.Length);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return String.Empty;
            }

            cipher.Clear();
        }
        return Encoding.UTF8.GetString(decrypted, 0, decryptedByteCount);
    }
}

我也有类似的问题:你能用你的旧代码解密数据,然后用安全密码重新加密吗?嗨,蒂姆,你引用的页面中的解决方案对你有用吗?我看到你在那里写了关于AES管理如何在通用应用程序中不可用的文章。如果它不起作用,我宁愿不要改变一切。ThanksHi CodesInChaos,您是否有或知道安全加密将与通用Windows应用程序一起使用的示例?谢谢,MarkI也有类似的问题:你能用你的旧代码解密数据并用安全密码重新加密吗?嗨,Tim,你提到的页面中的解决方案对你有用吗?我看到你在那里写了关于AES管理如何在通用应用程序中不可用的文章。如果它不起作用,我宁愿不要改变一切。ThanksHi CodesInChaos,您是否有或知道安全加密将与通用Windows应用程序一起使用的示例?谢谢马克
    public static string Decrypt(string cipherText)
    {

        try
        {
            var pw = "MyPassword";
            var salt = "mysaltpassword";

            IBuffer pwBuffer = CryptographicBuffer.ConvertStringToBinary(pw, BinaryStringEncoding.Utf8);
            IBuffer saltBuffer = CryptographicBuffer.ConvertStringToBinary(salt, BinaryStringEncoding.Utf8);
            IBuffer cipherBuffer = CryptographicBuffer.DecodeFromBase64String(cipherText);

            KeyDerivationAlgorithmProvider keyDerivationProvider = KeyDerivationAlgorithmProvider.OpenAlgorithm("PBKDF2_SHA1");

            KeyDerivationParameters pbkdf2Parms = KeyDerivationParameters.BuildForPbkdf2(saltBuffer, 1000);

            CryptographicKey keyOriginal = keyDerivationProvider.CreateKey(pwBuffer);
            IBuffer keyMaterial = CryptographicEngine.DeriveKeyMaterial(keyOriginal, pbkdf2Parms, 32);

            CryptographicKey derivedPwKey = keyDerivationProvider.CreateKey(pwBuffer);

            IBuffer saltMaterial = CryptographicEngine.DeriveKeyMaterial(derivedPwKey, pbkdf2Parms, 16);

            SymmetricKeyAlgorithmProvider symProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7");

            CryptographicKey symmKey = symProvider.CreateSymmetricKey(keyMaterial);

            IBuffer resultBuffer = CryptographicEngine.Decrypt(symmKey, cipherBuffer, saltMaterial);

            byte[] asd;
            CryptographicBuffer.CopyToByteArray(resultBuffer, out asd);
            string result = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, resultBuffer);
            return result;
        }
        catch (Exception ex)
        {

            var message = ex.Message;
            return string.Empty;
        }

    }  
}