Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Get Public Key from CSRDemonstrates how to get the public key from a CSR.
#include <C_CkPemW.h> #include <C_CkAsnW.h> #include <C_CkXmlW.h> #include <C_CkBinDataW.h> #include <C_CkPublicKeyW.h> void ChilkatSample(void) { HCkPemW pem; const wchar_t *noPassword; BOOL success; const wchar_t *strBase64; HCkAsnW asn; HCkXmlW xml; const wchar_t *strModulusHex; HCkBinDataW bd; const wchar_t *modulus64; HCkXmlW xmlPubKey; HCkPublicKeyW pubkey; BOOL preferPkcs1; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. pem = CkPemW_Create(); // No password is required. Pass an empty password string.. noPassword = L""; success = CkPemW_LoadPemFile(pem,L"qa_data/csr/csr2.pem",noPassword); if (success != TRUE) { wprintf(L"%s\n",CkPemW_lastErrorText(pem)); CkPemW_Dispose(pem); return; } strBase64 = CkPemW_getEncodedItem(pem,L"csr",L"",L"base64",0); asn = CkAsnW_Create(); success = CkAsnW_LoadEncoded(asn,strBase64,L"base64"); if (success != TRUE) { wprintf(L"%s\n",CkAsnW_lastErrorText(asn)); CkPemW_Dispose(pem); CkAsnW_Dispose(asn); return; } // Convert the ASN.1 to XML. xml = CkXmlW_Create(); success = CkXmlW_LoadXml(xml,CkAsnW_asnToXml(asn)); wprintf(L"%s\n",CkXmlW_getXml(xml)); wprintf(L"----\n"); strModulusHex = CkXmlW_getChildContent(xml,L"bits"); wprintf(L"strModulusHex = %s\n",strModulusHex); wprintf(L"----\n"); // We need the modulus as base64. bd = CkBinDataW_Create(); CkBinDataW_AppendEncoded(bd,strModulusHex,L"hex"); modulus64 = CkBinDataW_getEncoded(bd,L"base64"); wprintf(L"modulus64 = %s\n",modulus64); wprintf(L"----\n"); // Build the XML for the public key. xmlPubKey = CkXmlW_Create(); CkXmlW_putTag(xmlPubKey,L"RSAPublicKey"); CkXmlW_UpdateChildContent(xmlPubKey,L"Modulus",modulus64); // The RSA exponent will always be decimal 65537 (base64 = AQAB) CkXmlW_UpdateChildContent(xmlPubKey,L"Exponent",L"AQAB"); wprintf(L"RSA public key as XML:\n"); wprintf(L"%s\n",CkXmlW_getXml(xmlPubKey)); wprintf(L"----\n"); // Load the XML into a Chilkat public key object. pubkey = CkPublicKeyW_Create(); success = CkPublicKeyW_LoadFromString(pubkey,CkXmlW_getXml(xmlPubKey)); if (success != TRUE) { wprintf(L"%s\n",CkPublicKeyW_lastErrorText(pubkey)); CkPemW_Dispose(pem); CkAsnW_Dispose(asn); CkXmlW_Dispose(xml); CkBinDataW_Dispose(bd); CkXmlW_Dispose(xmlPubKey); CkPublicKeyW_Dispose(pubkey); return; } // Show the public key as PEM. preferPkcs1 = TRUE; wprintf(L"%s\n",CkPublicKeyW_getPem(pubkey,preferPkcs1)); CkPemW_Dispose(pem); CkAsnW_Dispose(asn); CkXmlW_Dispose(xml); CkBinDataW_Dispose(bd); CkXmlW_Dispose(xmlPubKey); CkPublicKeyW_Dispose(pubkey); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.