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 Sign with PKCS8 Encrypted KeyDemonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).
#include <C_CkPrivateKeyW.h> #include <C_CkRsaW.h> #include <C_CkCertW.h> #include <C_CkPublicKeyW.h> void ChilkatSample(void) { HCkPrivateKeyW pkey; BOOL success; const wchar_t *pkeyXml; HCkRsaW rsa; const wchar_t *strData; const wchar_t *hexSig; HCkCertW cert; HCkPublicKeyW pubKey; const wchar_t *pubKeyXml; HCkRsaW rsa2; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. pkey = CkPrivateKeyW_Create(); // Load the private key from an RSA PEM file: success = CkPrivateKeyW_LoadPkcs8EncryptedFile(pkey,L"raul_privateKey.key",L"a0123456789"); // 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); CkRsaW_Dispose(rsa); return; } // This example will sign a string, and receive the signature // in a hex-encoded string. Therefore, set the encoding mode // to "hex": 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" and "md5". hexSig = CkRsaW_signStringENC(rsa,strData,L"sha-1"); wprintf(L"%s\n",hexSig); // Now verify with the public key. // This example shows how to use the public key from // a digital certificate (.cer file) cert = CkCertW_Create(); success = CkCertW_LoadFromFile(cert,L"raul_publicKey.cer"); if (success != TRUE) { wprintf(L"%s\n",CkCertW_lastErrorText(cert)); CkPrivateKeyW_Dispose(pkey); CkRsaW_Dispose(rsa); CkCertW_Dispose(cert); return; } pubKey = CkCertW_ExportPublicKey(cert); // Get the private key in XML format: pubKeyXml = CkPublicKeyW_getXml(pubKey); rsa2 = CkRsaW_Create(); success = CkRsaW_ImportPublicKey(rsa2,pubKeyXml); if (success != TRUE) { wprintf(L"%s\n",CkRsaW_lastErrorText(rsa2)); CkPrivateKeyW_Dispose(pkey); CkRsaW_Dispose(rsa); CkCertW_Dispose(cert); CkRsaW_Dispose(rsa2); return; } // Verify the signature against the original data: CkRsaW_putEncodingMode(rsa2,L"hex"); success = CkRsaW_VerifyStringENC(rsa2,strData,L"sha-1",hexSig); if (success != TRUE) { wprintf(L"%s\n",CkRsaW_lastErrorText(rsa2)); CkPrivateKeyW_Dispose(pkey); CkRsaW_Dispose(rsa); CkCertW_Dispose(cert); CkRsaW_Dispose(rsa2); return; } wprintf(L"Signature verified!\n"); // Verify with incorrect data: success = CkRsaW_VerifyStringENC(rsa2,L"something else",L"sha-1",hexSig); if (success != TRUE) { wprintf(L"Signature not verified! (which was expected in this case)\n"); } else { wprintf(L"Hmmm... that's not right...\n"); } CkPublicKeyW_Dispose(pubKey); CkPrivateKeyW_Dispose(pkey); CkRsaW_Dispose(rsa); CkCertW_Dispose(cert); CkRsaW_Dispose(rsa2); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.