Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) Get Public Key from CSRDemonstrates how to get the public key from a CSR.
#include <CkPem.h> #include <CkAsn.h> #include <CkXml.h> #include <CkBinData.h> #include <CkPublicKey.h> void ChilkatSample(void) { CkString strOut; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkPem pem; // No password is required. Pass an empty password string.. const char *noPassword = ""; bool success = pem.LoadPemFile("qa_data/csr/csr2.pem",noPassword); if (success != true) { strOut.append(pem.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } const char *strBase64 = pem.getEncodedItem("csr","","base64",0); CkAsn asn; success = asn.LoadEncoded(strBase64,"base64"); if (success != true) { strOut.append(asn.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Convert the ASN.1 to XML. CkXml xml; success = xml.LoadXml(asn.asnToXml()); strOut.append(xml.getXml()); strOut.append("\r\n"); strOut.append("----"); strOut.append("\r\n"); const char *strModulusHex = xml.getChildContent("bits"); strOut.append("strModulusHex = "); strOut.append(strModulusHex); strOut.append("\r\n"); strOut.append("----"); strOut.append("\r\n"); // We need the modulus as base64. CkBinData bd; bd.AppendEncoded(strModulusHex,"hex"); const char *modulus64 = bd.getEncoded("base64"); strOut.append("modulus64 = "); strOut.append(modulus64); strOut.append("\r\n"); strOut.append("----"); strOut.append("\r\n"); // Build the XML for the public key. CkXml xmlPubKey; xmlPubKey.put_Tag("RSAPublicKey"); xmlPubKey.UpdateChildContent("Modulus",modulus64); // The RSA exponent will always be decimal 65537 (base64 = AQAB) xmlPubKey.UpdateChildContent("Exponent","AQAB"); strOut.append("RSA public key as XML:"); strOut.append("\r\n"); strOut.append(xmlPubKey.getXml()); strOut.append("\r\n"); strOut.append("----"); strOut.append("\r\n"); // Load the XML into a Chilkat public key object. CkPublicKey pubkey; success = pubkey.LoadFromString(xmlPubKey.getXml()); if (success != true) { strOut.append(pubkey.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // Show the public key as PEM. bool preferPkcs1 = true; strOut.append(pubkey.getPem(preferPkcs1)); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.