locked
Rijndael decryption in WinRT

    Question

  • I'm trying to write a method that decrypts a byte array.

    • the key is fixed and 24 bytes long

    I get an exception The supplied user buffer is not valid for the requested operationwhen I do the actual decryption of the data, I hope someone can help me!

            // Get the key and put into IBuffers
    
            IBuffer keyBuffer = CryptographicBuffer.CreateFromByteArray(cKey);
            IBuffer plainText = CryptographicBuffer.CreateFromByteArray(cData);
            byte[] decryptedData;
    
            // Setup an AES key, using AES in CBC mode and applying PKCS#7 padding on the input
            SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
            CryptographicKey aesKeySymm = aesProvider.CreateSymmetricKey(keyBuffer);
    
    
            // Decrypt the data and convert it to byte array
    
            // EXCEPTION ON THIS LINE: "The supplied user buffer is not valid for the requested operation."
            IBuffer decrypted = CryptographicEngine.Decrypt(aesKeySymm, plainText, null);
            CryptographicBuffer.CopyToByteArray(decrypted, out decryptedData);
            return decryptedData;
    Tuesday, August 12, 2014 9:31 PM

Answers

  • I have similar code aswell. Same Exception! I decrypt an DES encrypted string.

    What is the problem?

    String strSymmetricAlgName = SymmetricAlgorithmNames.DesEcbPkcs7; SymmetricKeyAlgorithmProvider objAlgProv = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strSymmetricAlgName); CryptographicKey keyPair = objAlgProv.CreateSymmetricKey(CryptographicBuffer.ConvertStringToBinary("xxx", BinaryStringEncoding.Utf8));

    IBuffer buf = CryptographicEngine.Decrypt(keyPair, encryptedText, null); <---Here it throws the exception:

    'Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)'. 

    I just solved my problem.

    I used CryptographicBuffer.CreateFromByteArray(byteArr) to Convert my byteArr to IBuffer

    and then Decrypt it instead of 

    CryptographicBuffer.ConvertStringToBinary(encodedData, BinaryStringEncoding.Utf8)


    Wednesday, September 10, 2014 12:18 PM

All replies

  • See this:

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.security.cryptography.core.symmetrickeyalgorithmprovider.createsymmetrickey.aspx

      if ((buffMsg.Length % objAlg.BlockLength) != 0)
            {
                throw new Exception("Message buffer length must be multiple of block length.");
            }

    Make sure your buffer is big enough and meets this criteria


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Wednesday, August 13, 2014 12:43 PM
    Moderator
  • Thanks for your answer! That was indeed the problem. Its my first time implementing encryption...

    Now I get the exception: 'Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)'. 

    Wednesday, August 13, 2014 4:20 PM
  • On what line. Post you new code as well please!

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, August 14, 2014 6:38 PM
    Moderator
  • Here is the code, exception is on the same line as the previous. Thanks for helping!

    // Get the key and iv and put all into IBuffers
                IBuffer keyBuffer = CryptographicBuffer.CreateFromByteArray(cKey);
                IBuffer plainText = CryptographicBuffer.CreateFromByteArray(cData);
    
                byte[] decryptedData;
    
                // Setup an AES key, using AES in ECB mode and applying PKCS#7 padding on the input
                SymmetricKeyAlgorithmProvider aesProvider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
                CryptographicKey aesKeySymm = aesProvider.CreateSymmetricKey(keyBuffer);
    
    
    
    if ((plainText.Length % aesProvider.BlockLength) != 0)
            {
                throw new Exception("Message buffer length must be multiple of block length.");
            }
    
    
                    // Decrypt the data and convert it to byte array
    
    		// EXCEPTION ON THIS LINE:  'Data error (cyclic redundancy check).
                  IBuffer decrypted = CryptographicEngine.Decrypt(aesKeySymm, plainText, null);
                    CryptographicBuffer.CopyToByteArray(decrypted, out decryptedData);
    
                return decryptedData;
    
                                     



    • Edited by Niko B Thursday, August 14, 2014 9:19 PM
    Thursday, August 14, 2014 9:01 PM
  • I have similar code and i'm also seeing the same exception. This occurs when I send a 5k cipher text to be decrypted. Works when cipher text is 16 bytes long. How can I decrypt large encrypted data?

    Thanks

    Martin

    Friday, September 05, 2014 8:37 PM
  • I have similar code aswell. Same Exception! I decrypt an DES encrypted string.

    What is the problem?

    String strSymmetricAlgName = SymmetricAlgorithmNames.DesEcbPkcs7; SymmetricKeyAlgorithmProvider objAlgProv = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strSymmetricAlgName); CryptographicKey keyPair = objAlgProv.CreateSymmetricKey(CryptographicBuffer.ConvertStringToBinary("xxx", BinaryStringEncoding.Utf8));

    IBuffer buf = CryptographicEngine.Decrypt(keyPair, encryptedText, null); <---Here it throws the exception:

    'Data error (cyclic redundancy check). (Exception from HRESULT: 0x80070017)'. 

    I just solved my problem.

    I used CryptographicBuffer.CreateFromByteArray(byteArr) to Convert my byteArr to IBuffer

    and then Decrypt it instead of 

    CryptographicBuffer.ConvertStringToBinary(encodedData, BinaryStringEncoding.Utf8)


    Wednesday, September 10, 2014 12:18 PM
  • I had the same Exception, but the problem was not in the decryption code. I wrote the encrypted buffer into a file that existed already, and which was larger. This meant that only the first x bytes of the file were overwritten during encryption. 

    When I tried to decrypt that file, it was not a valid cryptostream, because bytes from previous encryptions were still lurking at the end of the file. 

    I overwrote the file on encryption, which solved my problem.

    Hope this helps

    Tuesday, January 27, 2015 1:01 PM