问题
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:\test\mc-openapi-csr.pem")
new X509Certificate2(@"c:\test\mc-openapi-csr.pem", "mcapi")
new X509Certificate2(@"c:\test\mc-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?
回答1:
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);
}
回答2:
You could try to use a relative path instead of an absolute one?
Just put the file in the root of your project and try to use only the filename, without the path.
来源:https://stackoverflow.com/questions/13754685/how-do-i-use-a-private-key-in-c-cannot-find-the-requested-object