AES Decrypt with Universal Windows App

1

I have data that is encrypted in a SQL database using the method below. This works fine in my web application. I'm adding a Universal windows app to the project but can't use the same Decrypt method as the web application uses (As System.Security.Cryptography can't be referenced). I have tried to implement this using Windows.Security.Cryptography (from this example) but i get the following error

CryptograhicEngine.Decrypt(symmKey, cipherBuffer, saltMaterial); "Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)"

I have tried changing some of the proprieties in the Decrypt method but I'm at my wits end as to why it won't work and not an expert in this. Any help would be greatly appreciated.

Method in web app to encrypt and decrypt

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);
    }
}

//Method to decypt on Universal App

public static class CryptoHelper {

    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;
        }

    }  
}
c#
encryption
cryptography
asked on Stack Overflow Jan 6, 2016 by Mark • edited May 23, 2017 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0