How do I use a private key in C#? “Cannot find the requested object.”

后端 未结 2 1471
孤街浪徒
孤街浪徒 2020-12-21 17:21

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

https://developer.mastercard.

相关标签:
2条回答
  • 2020-12-21 17:58

    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);
            }
    
    0 讨论(0)
  • 2020-12-21 18:00

    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.

    0 讨论(0)
提交回复
热议问题