![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) RSA Signature with Certificate's Private Key from PFXSee more RSA ExamplesDemonstrates how to use a certificate's private key from a PFX file to create an RSA signature.Note: This example requires Chilkat v10.1.2 or greater.
#include <C_CkCertStoreW.h> #include <C_CkJsonObjectW.h> #include <C_CkCertW.h> #include <C_CkPrivateKeyW.h> #include <C_CkRsaW.h> void ChilkatSample(void) { HCkCertStoreW certStore; BOOL success; HCkJsonObjectW jsonCN; HCkCertW cert; HCkPrivateKeyW pkey; const wchar_t *pkeyXml; HCkRsaW rsa; const wchar_t *strData; const wchar_t *hexSig; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Create an instance of a certificate store object, load a PFX file, // locate the certificate we need, and use it for signing. // (a PFX file may contain more than one certificate.) certStore = CkCertStoreW_Create(); // The 1st argument is the filename, the 2nd arg is the // PFX file's password: success = CkCertStoreW_LoadPfxFile(certStore,L"chilkat.pfx",L"test"); if (success != TRUE) { wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore)); CkCertStoreW_Dispose(certStore); return; } // Find the certificate by the subject common name: jsonCN = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(jsonCN,L"CN",L"cert common name"); cert = CkCertW_Create(); success = CkCertStoreW_FindCert(certStore,jsonCN,cert); if (success == FALSE) { wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore)); CkCertStoreW_Dispose(certStore); CkJsonObjectW_Dispose(jsonCN); CkCertW_Dispose(cert); return; } pkey = CkCertW_ExportPrivateKey(cert); if (CkCertW_getLastMethodSuccess(cert) == FALSE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkCertStoreW_Dispose(certStore); CkJsonObjectW_Dispose(jsonCN); CkCertW_Dispose(cert); return; } // Get the private key in XML format: pkeyXml = CkPrivateKeyW_getXml(pkey); rsa = CkRsaW_Create(); // Import the private key into the RSA component: success = CkRsaW_ImportPrivateKey(rsa,pkeyXml); if (success != TRUE) { wprintf(L"%s\n",CkRsaW_lastErrorText(rsa)); CkPrivateKeyW_Dispose(pkey); CkCertStoreW_Dispose(certStore); CkJsonObjectW_Dispose(jsonCN); CkCertW_Dispose(cert); CkRsaW_Dispose(rsa); return; } // Encode the signature as a hex string CkRsaW_putEncodingMode(rsa,L"hex"); strData = L"This is the string to be signed."; // Sign the string using the sha-1 hash algorithm. // Other valid choices are "sha-256", "md2" and "md5". hexSig = CkRsaW_signStringENC(rsa,strData,L"sha-1"); wprintf(L"%s\n",hexSig); CkPrivateKeyW_Dispose(pkey); wprintf(L"Success!\n"); CkCertStoreW_Dispose(certStore); CkJsonObjectW_Dispose(jsonCN); CkCertW_Dispose(cert); CkRsaW_Dispose(rsa); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.