Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
913 views
in Technique[技术] by (71.8m points)

encryption - How do I use a private key in C#? "Cannot find the requested object."

I'm trying to implement authentication for MasterCard Match, as part of their following documentation, they have a sample private key:

https://developer.mastercard.com/portal/display/api/OAuth+Validation

On that page, they have two versions of the key, one in base64 encoded text, visible on the page, and a .p12 file downloadable.

How do I import this key to use as an x509certificate2?

Whatever I try I get the message "Cannot find the requested object.".

I tried digging into it with the .net source, but I get a dead end at an imported object

[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern uint _QueryCertFileType(string fileName);

I've tried the following, and all of them fail with the same aforementioned message

new X509Certificate2(@"c:estmc-openapi-csr.pem")
new X509Certificate2(@"c:estmc-openapi-csr.pem", "mcapi")
new X509Certificate2(@"c:estmc-openapi-csr.pem", "mckp")

so I copied the text block into "copied.txt", and tried using that file, I've also tried reading the bytes in, and passing them in manually, I've also tried using

X509Certificate.CreateFromCertFile(fileName)

with both files.

Any ideas? Is the certificate bad? Am I using the wrong class? What does that error message mean?

--Update-- At Bad Zombie's suggestion, I tried BouncyCastle:

    var pem = new Org.BouncyCastle.OpenSsl.PemReader(File.OpenText(fileName));
    RsaPrivateCrtKeyParameters rsaParameters = (RsaPrivateCrtKeyParameters)pem.ReadObject();
    using (var rsa = new RSACryptoServiceProvider())
    {
        rsa.ImportParameters(new RSAParameters
            {
                DP = rsaParameters.DP.ToByteArray(),
                DQ = rsaParameters.DQ.ToByteArray(),
                Exponent = rsaParameters.Exponent.ToByteArray(),
                InverseQ = rsaParameters.QInv.ToByteArray(),
                Modulus = rsaParameters.Modulus.ToByteArray(),
                P = rsaParameters.P.ToByteArray(),
                Q = rsaParameters.Q.ToByteArray(),
            });
    }

on the "ImportParameters" call, I get "Bad Data". Am I doing something wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can't remember exactly but I believe pem files can not be used. You'll need something like bouncy castle.

I don't remember much but it loads PEM files. Occasionally it will not like it but its somewhat rare. I don't know if it can be used but I do know i successfully used private/public keys with it. I have no idea how to convert it to something that works with .NET ssl library.

However I suggest you convert it to pem to something that is more compatible with .NET implementation if you are using .net implementation rather then bouncy castle. I used bouncycastle and it worked for my project which didnt need to interface with another library.

using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.OpenSsl;
using System.IO;
using System.Security.Cryptography;

//elsewhere

        using (var reader = File.OpenText(fileName))
        {
            var pemReader = new PemReader(reader);
            var bouncyRsaParameters = (RsaPrivateCrtKeyParameters)pemReader.ReadObject();
            var rsaParameters = DotNetUtilities.ToRSAParameters(bouncyRsaParameters);
            this.PrivateKey = new RSACryptoServiceProvider();
            this.PrivateKey.ImportParameters(rsaParameters);
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...