Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Windows.Security.Cryptography.Core.CryptographicEngine.Encrypt() throwing InvalidCastException

Hi, I'm porting some crypto code to UWP for HoloLens. AFAIK, I've implemented it perfectly according to UWP requirements, but I get a mysterious InvalidCastException from the following code:

`public override byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV) {
#if NETFX_CORE

        IBuffer dataBuffer = WindowsRuntimeBuffer.Create(clearData, 0, clearData.Length, clearData.Length);
        IBuffer keyBuffer = WindowsRuntimeBuffer.Create(Key, 0, Key.Length, Key.Length);
        IBuffer ivBuffer = WindowsRuntimeBuffer.Create(IV, 0, IV.Length, IV.Length);

        string strAlgName = KeyDerivationAlgorithmNames.Pbkdf2Sha256;

        // Open the specified algorithm.
        KeyDerivationAlgorithmProvider objAlgProv = KeyDerivationAlgorithmProvider.OpenAlgorithm(strAlgName);

        // Create a key by using the passed buffer.
        CryptographicKey key = objAlgProv.CreateKey(keyBuffer);

         IBuffer encryptedData = CryptographicEngine.Encrypt(key, dataBuffer, ivBuffer); // throws System.InvalidCastException
         return (byte[])encryptedData.ToArray();`

I'm not really sure what the issue is. I've verified that the types of all parameters being passed into CryptographicEngine.Encrypt are the right type. I tried the AsBuffer extension method earlier on my byte arrays; I switched to WindowsRuntimeBuffer.Create thinking that might do the trick, but it makes no difference whatsoever. dataBuffer and ivBuffer are both of type WindowsRuntimeBuffer, which implements IBuffer, and both are a length that is a multiple of 8 (32 and 16, respectively). I tried casting as IBuffer; that didn't work. I even tried generating 3 random IBuffers, all of lenght 32, to see if that would at least work; it doesn't work. Is this a bug, or am I missing something really obvious? Moreover, the InvalidCastException is completely generic and only gives me the StackTrace from my own app, which tells me absolutely nothing.

Answers

  • Options

    FWIW, here's the whole stack trace:

    at Windows.Security.Cryptography.Core.CryptographicEngine.Encrypt(CryptographicKey key, IBuffer data, IBuffer iv)
    at dk.UWP.Crypto.UWPSimpleEncryption.Encrypt(Byte[] clearData, Byte[] Key, Byte[] IV)
    at dk.UWP.Crypto.UWPSimpleEncryption.Encrypt(String clearText, String Password)
    at dk.Crypto.SimpleEncryption.Encrypt(String clearText, String Password)
    at bbtb.Scene.BBTBInitialization.d__19.MoveNext()
    at UnityEngine.SetupCoroutine.InvokeMoveNext(IEnumerator enumerator, IntPtr returnValueAddress)
    at UnityEngine.SetupCoroutine.$Invoke1InvokeMoveNext(Int64 instance, Int64* args)
    at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)

  • Options

    I've submitted the following bug report on the Connect website:
    https://connect.microsoft.com/VisualStudio/feedback/details/3106919

  • Options

    It looks as if, for whatever reason, the examples MSDN provides with KeyDerivationAlgorithmProvider and KeyDerivationAlgorithmNames do not work. If you instead use SymmetricAlgorithmNames and SymmetricAlgorithmProvider, it seems works fine. I'm not sure if this is by design; if so, it is completely undocumented. I will post more information if I discover it.

  • Options

    This issue from 2012 fully explains and resolves the issue:
    http://answers.flyppdevportal.com/MVC/Post/Thread/85b1b57a-3b4b-420a-b01f-154cdaa68cbc?category=winappswithcsharp
    Although it is not clearly documented, different algorithm providers are used for key derivation and encryption.

Sign In or Register to comment.