surega
surega

Reputation: 715

Getting null when trying to get AsymmetricCipherKeyPair

I have this piece of code whose equivalents I have found in multiple places. But it is returning null when I am using it.

  using Org.BouncyCastle.OpenSsl;
  using Org.BouncyCastle.Crypto;
  using Org.BouncyCastle.Security;  

 string aToBeEncrypted= "asdfghikoksadjfkjsdfjsljfsadjf";
 string pemFilename = @"M:\ConnectivityPackage_meltemi_KeyStore.pem";

 byte[] plaintext = System.Text.Encoding.UTF8.GetBytes(aToBeEncrypted);

 AsymmetricCipherKeyPair keyPair;

 using (var reader = File.OpenText(pemFilename))
    keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();

keyPair is a null object. What is it that I am doing wrong or is it something with the pem file?

Upvotes: 1

Views: 4848

Answers (4)

David C.
David C.

Reputation: 33

I tested PemReader.ReadObject() using an RSA private key I created using OpenSSL. There were two version of the same key, one encrypted with a password, the other not.

My finding is that PemReader.ReadObject() will return a different object depending on if the stream being read in by the PemReader contains an encrypted key or not. If the private key is encrypted, ReadObject will return a AsymmetricCipherKeyPair object, and if the private key is not encrypted the output will be a simple RsaPrivateCrtKeyParameters object containing only the private key.

Upvotes: 1

ceztko
ceztko

Reputation: 15217

The API of PemReader appears to be very bad. Basically it returns null every time it can't parse an object, but that doesn't mean there aren't more objects in the reader. You have to track yourself the end of the stream. The following worked for me while trying to read a private key:

        string pemFile = "...";
        using (var strReader = new StringReader(pemFile))
        {
            var pemReader = new PemReader(strReader);
            while (strReader.Peek() != -1)
            {
                var parameter = pemReader.ReadObject() as RsaPrivateCrtKeyParameters;
                if (parameter != null)
                {
                    // Do something with the found private key
                }
            }
        } 

Upvotes: 4

Paprikawurst
Paprikawurst

Reputation: 1

Even though this is not the wanted answer, you might be able to skip the AsymmetricCipherKeyPair step.

I fixed this by skipping the AsymmetricCipherKeyPair part and directly casting the PemReader return value to a RsaPrivateCrtKeyParameter object.

RSAParameters rsaParams;
            using (var stringReader = new StringReader(privateRsaKey))
            {
                var pemReader = new PemReader(stringReader);
                var privateRsaParams = pemReader.ReadObject() as RsaPrivateCrtKeyParameters;
                if (privateRsaParams == null)
                {
                    throw new Exception("Could not read RSA private key");
                }
                rsaParams = DotNetUtilities.ToRSAParameters(privateRsaParams);

Upvotes: 0

Steve A
Steve A

Reputation: 21

I've recently spent days trying to get this to work, I too was getting a nul returned. I just changed StringReader to StreamReader and it worked.

Upvotes: 2

Related Questions