Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

RSACryptoServiceProvider CspParameters are null and CryptoKeySecurity is null using X509Certificate2

KastaKasta CZMember ✭✭

Hi guys,
can't find why my RSACryptoServiceProvider returning null values and null CryptoKeySecurity using X509Certificate2 and CspParameters for provider "Microsoft Enhanced RSA and AES Cryptographic Provider".

This code works well on Windows application and signs correctly, but on Xamarin.Android I can't figure out what is wrong. Any help would be nice.

Here is code I'm using:

private RSACryptoServiceProvider ComputeKey()
    {
        var certificateCollection = new X509Certificate2Collection();
        certificateCollection.Import(Data, Password, KeyStorageFlags);
        foreach (var certificate in certificateCollection)
        {
            if (!certificate.HasPrivateKey)
            {
                continue;
            }

            var key = certificate.PrivateKey as RSACryptoServiceProvider;
            var exportParameters = key.ExportParameters(includePrivateParameters: true);
            var cspParameters = new CspParameters
            {
                ProviderName = "Microsoft Enhanced RSA and AES Cryptographic Provider",
                Flags = UseMachineKeyStore ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags
            };
            var result = new RSACryptoServiceProvider(cspParameters);
            result.ImportParameters(exportParameters);
            return result;
        }

        throw new ArgumentException("The provided certificate does not have any private keys.");
    }

Here is android result:

And here is working windows result:

Answers

  • KastaKasta CZMember ✭✭

    Still can't find solution ... can anyone help me please?

Sign In or Register to comment.