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/Verify with .key and .cerSee more RSA ExamplesDemonstrates how to use a .key file (private key) and digital certificate (.cer, public key) to create and verify an RSA signature.
#include <C_CkPrivateKeyW.h> #include <C_CkRsaW.h> #include <C_CkCertW.h> #include <C_CkPublicKeyW.h> void ChilkatSample(void) { HCkPrivateKeyW privKey; BOOL success; const wchar_t *privKeyXml; HCkRsaW rsa; const wchar_t *strData; const wchar_t *hexSig; HCkCertW cert; HCkPublicKeyW pubKey; HCkRsaW rsa2; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. privKey = CkPrivateKeyW_Create(); // Load the private key from an RSA .key file: success = CkPrivateKeyW_LoadPemFile(privKey,L"privateKey.key"); if (success != TRUE) { wprintf(L"%s\n",CkPrivateKeyW_lastErrorText(privKey)); CkPrivateKeyW_Dispose(privKey); return; } // Get the private key in XML format: privKeyXml = CkPrivateKeyW_getXml(privKey); rsa = CkRsaW_Create(); // Import the private key into the RSA component: success = CkRsaW_ImportPrivateKey(rsa,privKeyXml); if (success != TRUE) { wprintf(L"%s\n",CkRsaW_lastErrorText(rsa)); CkPrivateKeyW_Dispose(privKey); CkRsaW_Dispose(rsa); return; } // Create 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 "md2", "sha256", "sha384", // "sha512", and "md5". hexSig = CkRsaW_signStringENC(rsa,strData,L"sha-1"); wprintf(L"%s\n",hexSig); // Load a digital certificate from a .cer file: cert = CkCertW_Create(); success = CkCertW_LoadFromFile(cert,L"myCert.cer"); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkPrivateKeyW_Dispose(privKey); CkRsaW_Dispose(rsa); CkCertW_Dispose(cert); return; } pubKey = CkCertW_ExportPublicKey(cert); // Now verify using a separate instance of the RSA object: rsa2 = CkRsaW_Create(); // Import the public key into the RSA object: success = CkRsaW_ImportPublicKey(rsa2,CkPublicKeyW_getXml(pubKey)); if (success != TRUE) { wprintf(L"%s\n",CkRsaW_lastErrorText(rsa2)); CkPrivateKeyW_Dispose(privKey); CkRsaW_Dispose(rsa); CkCertW_Dispose(cert); CkRsaW_Dispose(rsa2); return; } CkPublicKeyW_Dispose(pubKey); // The signature is a hex string, so make sure the EncodingMode is correct: CkRsaW_putEncodingMode(rsa2,L"hex"); // Verify the signature: success = CkRsaW_VerifyStringENC(rsa2,strData,L"sha-1",hexSig); if (success != TRUE) { wprintf(L"%s\n",CkRsaW_lastErrorText(rsa2)); CkPrivateKeyW_Dispose(privKey); CkRsaW_Dispose(rsa); CkCertW_Dispose(cert); CkRsaW_Dispose(rsa2); return; } wprintf(L"Success.\n"); CkPrivateKeyW_Dispose(privKey); CkRsaW_Dispose(rsa); CkCertW_Dispose(cert); CkRsaW_Dispose(rsa2); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.