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 RSA Key Modulus from .cer or .keyDemonstrates how to get the RSA key modulus from either the certificate (.cer) or RSA key (.key). OpenSSL commands to do the same would be: openssl x509 -inform DER -in "test.cer" -modulus -nooutor openssl pkcs8 -inform DER -inβ "test.key"β -outform PEM -passin pass:"12345β678aβ" | openssl rsa -inform PEM -modulus -noout
#include <C_CkPrivateKeyW.h> #include <C_CkXmlW.h> #include <C_CkBinDataW.h> #include <C_CkCertW.h> #include <C_CkPublicKeyW.h> void ChilkatSample(void) { HCkPrivateKeyW privKey; const wchar_t *password; BOOL success; HCkXmlW xml; const wchar_t *modulus; HCkBinDataW binDat; const wchar_t *hexModulus; HCkCertW cert; HCkPublicKeyW pubKey; privKey = CkPrivateKeyW_Create(); password = L"12345678a"; success = CkPrivateKeyW_LoadPkcs8EncryptedFile(privKey,L"qa_data/certs/test_12345678a.key",password); if (success != TRUE) { wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey)); CkPrivateKeyW_Dispose(privKey); return; } xml = CkXmlW_Create(); CkXmlW_LoadXml(xml,CkPrivateKeyW_getXml(privKey)); // The XML contains the parts of the key in base64. wprintf(L"Private Key XML:\n"); wprintf(L"%s\n",CkXmlW_getXml(xml)); // We can get the base64 modulus like this: modulus = CkXmlW_getChildContent(xml,L"Modulus"); wprintf(L"base64 modulus = %s\n",modulus); // To convert to hex: binDat = CkBinDataW_Create(); CkBinDataW_AppendEncoded(binDat,modulus,L"base64"); hexModulus = CkBinDataW_getEncoded(binDat,L"hex"); wprintf(L"hex modulus = %s\n",hexModulus); // Now get the modulus from the cert: cert = CkCertW_Create(); success = CkCertW_LoadFromFile(cert,L"qa_data/certs/test_12345678a.cer"); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkPrivateKeyW_Dispose(privKey); CkXmlW_Dispose(xml); CkBinDataW_Dispose(binDat); CkCertW_Dispose(cert); return; } // The cert contains the public key, which is composed of the // modulus + exponent (for RSA keys). pubKey = CkCertW_ExportPublicKey(cert); CkXmlW_LoadXml(xml,CkPublicKeyW_getXml(pubKey)); wprintf(L"Public Key XML:\n"); wprintf(L"%s\n",CkXmlW_getXml(xml)); // Proceed in the same way as before.... modulus = CkXmlW_getChildContent(xml,L"Modulus"); wprintf(L"base64 modulus = %s\n",modulus); // To convert to hex: CkBinDataW_Clear(binDat); CkBinDataW_AppendEncoded(binDat,modulus,L"base64"); hexModulus = CkBinDataW_getEncoded(binDat,L"hex"); wprintf(L"hex modulus = %s\n",hexModulus); CkPublicKeyW_Dispose(pubKey); CkPrivateKeyW_Dispose(privKey); CkXmlW_Dispose(xml); CkBinDataW_Dispose(binDat); CkCertW_Dispose(cert); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.