![]() |
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 v11.0.0 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) { BOOL success; HCkCertStoreW certStore; HCkJsonObjectW jsonCN; HCkCertW cert; HCkPrivateKeyW privKey; HCkRsaW rsa; const wchar_t *strData; const wchar_t *hexSig; success = FALSE; // 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 == FALSE) { 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; } privKey = CkPrivateKeyW_Create(); success = CkCertW_GetPrivateKey(cert,privKey); if (success == FALSE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkCertStoreW_Dispose(certStore); CkJsonObjectW_Dispose(jsonCN); CkCertW_Dispose(cert); CkPrivateKeyW_Dispose(privKey); return; } rsa = CkRsaW_Create(); success = CkRsaW_UsePrivateKey(rsa,privKey); if (success == FALSE) { wprintf(L"%s\n",CkRsaW_lastErrorText(rsa)); CkCertStoreW_Dispose(certStore); CkJsonObjectW_Dispose(jsonCN); CkCertW_Dispose(cert); CkPrivateKeyW_Dispose(privKey); 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); wprintf(L"Success!\n"); CkCertStoreW_Dispose(certStore); CkJsonObjectW_Dispose(jsonCN); CkCertW_Dispose(cert); CkPrivateKeyW_Dispose(privKey); CkRsaW_Dispose(rsa); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.