Re: mle sample "RsaKeyManagement algorithm expects key...."

QRTIT
Helper

mle sample "RsaKeyManagement algorithm expects key...."

in the mle sample, i'm gettting this when encoding the jwt "RsaKeyManagement algorithm expects key to be of CngKey or RSACryptoServiceProvider types."

I would think the sample would just work.  I'm in vs2015 on .net 4.6.

2 REPLIES 2
SyedSa
Community Moderator

Re: mle sample "RsaKeyManagement algorithm expects key...."

Hi @QRTIT, Thank you for reaching out. An agent will get back to you as soon as possible. Until then, if any community member has information that may be helpful, feel free to reply in this thread.

QRTIT
Helper

Re: mle sample "RsaKeyManagement algorithm expects key...."

I figured it out.  I had to change your sample code.  The below seems to be working.

        private String getEncryptedPayload(String requestBody)
        {
            RSA clientCertificate = new X509Certificate2(mleServerPublicCertificate).GetRSAPublicKey();
            // Convert RSA to RSACryptoServiceProvider
            RSACryptoServiceProvider rsaCryptoServiceProvider = clientCertificate as RSACryptoServiceProvider;
            if (rsaCryptoServiceProvider == null)
            {
                rsaCryptoServiceProvider = new RSACryptoServiceProvider();
                rsaCryptoServiceProvider.ImportParameters(clientCertificate.ExportParameters(false));
            }
            DateTime now = DateTime.UtcNow;
            long unixTimeMilliseconds = new DateTimeOffset(now).ToUnixTimeMilliseconds();
            IDictionary<string, object> extraHeaders = new Dictionary<string, object>{
                {"kid", keyId},{"iat",unixTimeMilliseconds}
            };
            string token = JWT.Encode(requestBody, rsaCryptoServiceProvider, JweAlgorithm.RSA_OAEP_256, JweEncryption.A128GCM, null, extraHeaders);
            return "{\"encData\":\"" + token + "\"}";
        }