CryptoStream in WP 8.1 RT

Nov 28, 2014 at 10:58 AM
I use following code in a common library for WP8.1 RT and iOS (Android is not yet been tested).
Encoding work fine in all platforms, but in WP app, decoding always fail with an exception "Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)".
A stream encoded by WP is correctly decoded by iOS, of course, a stream encoded by iOS is correctly decoded by iOS.
The only way that I found for to make decoding working in WP is read all encoded stream in a buffer and use CryptographicEngine.Decrypt instead of CryptoStream as show in DecodeWP method.
Is that a CryptoStream implementation bug or a limitation of WinRT runtime?
There is a way to use CryptoStream method also in WP app? Input stream can be very huge, and I don't want load it all in memory.

Thanks in advance
FP
private byte[] GetHash(string data)
{
    IHashAlgorithmProvider objAlgProv = WinRTCrypto.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithm.Md5);
    return objAlgProv.HashData(System.Text.UTF8Encoding.UTF8.GetBytes(data));
}

public async Task Encode(System.IO.Stream input, System.IO.Stream output, string password)
{
    byte[] keyHash = GetHash(password);
    ISymmetricKeyAlgorithmProvider aes = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
    ICryptographicKey symetricKey = aes.CreateSymmetricKey(keyHash);

    using (CryptoStream cryptoStream = new CryptoStream(output, WinRTCrypto.CryptographicEngine.CreateEncryptor(symetricKey), CryptoStreamMode.Write))
    {
        await input.CopyToAsync(cryptoStream);
    }
}

public async Task Decode(System.IO.Stream input, System.IO.Stream output, string password)
{
    try
    {
        byte[] keyHash = GetHash(password);
        ISymmetricKeyAlgorithmProvider aes = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
        ICryptographicKey symetricKey = aes.CreateSymmetricKey(keyHash);

        using (CryptoStream cryptoStream = new CryptoStream(input, WinRTCrypto.CryptographicEngine.CreateDecryptor(symetricKey), CryptoStreamMode.Read))
        {
            await cryptoStream.CopyToAsync(output);
        }

    }
    catch (Exception ex)
    {
        throw ex; //Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)
    }
}

public async Task DecodeWP(System.IO.Stream input, System.IO.Stream output, string password)
{
    byte[] keyHash = GetHash(password);
    ISymmetricKeyAlgorithmProvider aes = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
    ICryptographicKey symetricKey = aes.CreateSymmetricKey(keyHash);

    byte[] inputBuffer = new byte[input.Length];
    input.Read(inputBuffer, 0, inputBuffer.Length);
    byte[] outputBuffer = WinRTCrypto.CryptographicEngine.Decrypt(symetricKey, inputBuffer, null);
    await output.WriteAsync(outputBuffer, 0, outputBuffer.Length);
}