Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1838428
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:28:34+00:00 2026-05-17T06:28:34+00:00

I need to load a PEM encoded X.509 certificate into a Windows Crypto API

  • 0

I need to load a PEM encoded X.509 certificate into a Windows Crypto API context to use with C++. They are the ones that have -----BEGIN RSA XXX KEY----- and -----END RSA XXX KEY-----. I found examples for Python and .NET but they use specific functions I can’t relate to the plain Windows Crypto API.

I understand how to encrypt/decrypt once I’ve got a HCRYPTKEY.
BUT, I just don’t get how to import the Base64 blob in the .PEM file(s) and get a HCRYPTKEY that I can use out of it.

I have that strange feeling that there is more to it than simply calling CryptDecodeObject().

Any pointers that can put me on track? I’ve already lost two days doing “trial & error” programming and getting nowhere.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:28:35+00:00Added an answer on May 17, 2026 at 6:28 am

    KJKHyperion said in his answer:

    I discovered the “magic” sequence of calls to import a RSA public key in PEM format. Here you go:

    1. decode the key into a binary blob with CryptStringToBinary; pass CRYPT_STRING_BASE64HEADER in dwFlags
    2. decode the binary key blob into a CERT_PUBLIC_KEY_INFO with CryptDecodeObjectEx; pass X509_ASN_ENCODING in dwCertEncodingType and X509_PUBLIC_KEY_INFO in lpszStructType
    3. decode the PublicKey blob from the CERT_PUBLIC_KEY_INFO into a RSA key blob with CryptDecodeObjectEx; pass X509_ASN_ENCODING in dwCertEncodingType and RSA_CSP_PUBLICKEYBLOB in lpszStructType
    4. import the RSA key blob with CryptImportKey

    This sequence really helped me understand what’s going on, but it didn’t work for me as-is. The second call to CryptDecodeObjectEx gave me an error:
    “ASN.1 bad tag value met”.
    After many attempts at understanding Microsoft documentation, I finally realized that the output of the fist decode cannot be decoded as ASN again, and that it is actually ready for import. With this understanding I found the answer in the following link:

    http://www.ms-news.net/f2748/problem-importing-public-key-4052577.html

    Following is my own program that imports a public key from a .pem file to a CryptApi context:

    int main()
    {
        char           pemPubKey[2048];
        int            readLen;
        char           derPubKey[2048];
        size_t         derPubKeyLen = 2048;
        CERT_PUBLIC_KEY_INFO *publicKeyInfo;
        int            publicKeyInfoLen;
        HANDLE         hFile;
        HCRYPTPROV     hProv = 0;
        HCRYPTKEY      hKey = 0;
    
        /*
         * Read the public key cert from the file
         */
        hFile = CreateFileA( "c:\\pub.pem", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
        if ( hFile == INVALID_HANDLE_VALUE )
        {
            fprintf( stderr, "Failed to open file. error: %d\n", GetLastError() );
        }
    
        if ( !ReadFile( hFile, pemPubKey, 2048, &readLen, NULL ) )
        {
            fprintf( stderr, "Failed to read file. error: %d\n", GetLastError() );
        }
    
        /*
         * Convert from PEM format to DER format - removes header and footer and decodes from base64
         */
        if ( !CryptStringToBinaryA( pemPubKey, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen, NULL, NULL ) )
        {
            fprintf( stderr, "CryptStringToBinary failed. Err: %d\n", GetLastError() );
        }
    
        /*
         * Decode from DER format to CERT_PUBLIC_KEY_INFO
         */
        if ( !CryptDecodeObjectEx( X509_ASN_ENCODING, X509_PUBLIC_KEY_INFO, derPubKey, derPubKeyLen, 
                                   CRYPT_ENCODE_ALLOC_FLAG, NULL, &publicKeyInfo, &publicKeyInfoLen ) )
        {
            fprintf( stderr, "CryptDecodeObjectEx 1 failed. Err: %p\n", GetLastError() );
            return -1;
        }
    
        /*
         * Acquire context 
         */
        if( !CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) )
        {
            {
                printf( "CryptAcquireContext failed - err=0x%x.\n", GetLastError() );
                return -1;
            }
        }
    
        /*
         * Import the public key using the context
         */
        if ( !CryptImportPublicKeyInfo( hProv, X509_ASN_ENCODING, publicKeyInfo, &hKey ) )
        {
            fprintf( stderr, "CryptImportPublicKeyInfo failed. error: %d\n", GetLastError() );
            return -1;
        }
        LocalFree( publicKeyInfo );
    
        /*
         * Now use hKey to encrypt whatever you need.
         */
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.