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) 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.
#include <C_CkCertStoreW.h> #include <C_CkCertW.h> #include <C_CkPrivateKeyW.h> #include <C_CkRsaW.h> void ChilkatSample(void) { HCkCertStoreW certStore; BOOL success; 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; } cert = CkCertStoreW_FindCertBySubject(certStore,L"Chilkat Software, Inc."); if (CkCertStoreW_getLastMethodSuccess(certStore) == FALSE) { wprintf(L"%s\n",CkCertStoreW_lastErrorText(certStore)); CkCertStoreW_Dispose(certStore); return; } pkey = CkCertW_ExportPrivateKey(cert); if (CkCertW_getLastMethodSuccess(cert) == FALSE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkCertW_Dispose(cert); CkCertStoreW_Dispose(certStore); return; } CkCertW_Dispose(cert); // 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); 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); CkRsaW_Dispose(rsa); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.