Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

MFC Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(MFC) RSA Signature with Certificate's Private Key from PFX

Demonstrates how to use a certificate's private key from a PFX file to create an RSA signature.

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

// Needs #include <CkCertStore.h>
// Needs #include <CkCert.h>
// Needs #include <CkPrivateKey.h>
// Needs #include <CkRsa.h>

    CkString strOut;

    bool success;

    //  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.)
    CkCertStore certStore;

    //  The 1st argument is the filename, the 2nd arg is the
    //  PFX file's password:
    success = certStore.LoadPfxFile("chilkat.pfx","test");
    if (success != true) {
        strOut.append(certStore.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    CkCert *cert = 0;
    cert = certStore.FindCertBySubject("Chilkat Software, Inc.");
    if (cert == 0 ) {
        strOut.append(certStore.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    CkPrivateKey *pkey = 0;
    pkey = cert->ExportPrivateKey();
    if (pkey == 0 ) {
        strOut.append(cert->lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    const char *pkeyXml = 0;
    //  Get the private key in XML format:
    pkeyXml = pkey->getXml();

    CkRsa rsa;

    //  Any string argument automatically begins the 30-day trial.
    bool success = rsa.UnlockComponent("30-day trial");
    if (success != true) {
        strOut.append("RSA component unlock failed");
        strOut.append("\r\n");
        delete pkey;
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    //  Import the private key into the RSA component:
    success = rsa.ImportPrivateKey(pkeyXml);
    if (success != true) {
        strOut.append(rsa.lastErrorText());
        strOut.append("\r\n");
        delete pkey;
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    //  This example will sign a string, and receive the signature
    //  in a hex-encoded string.  Therefore, set the encoding mode
    //  to "hex":
    rsa.put_EncodingMode("hex");

    //  If some other non-Chilkat application or web service is going to be verifying
    //  the signature, it is important to match the byte-ordering.
    //  The LittleEndian property may be set to true
    //  for little-endian byte ordering,
    //  or false  for big-endian byte ordering.
    //  Microsoft apps typically use little-endian, while
    //  OpenSSL and other services (such as Amazon CloudFront)
    //  use big-endian.
    rsa.put_LittleEndian(false);

    const char *strData = "This is the string to be signed.";

    //  Sign the string using the sha-1 hash algorithm.
    //  Other valid choices are "md2" and "md5".
    const char *hexSig = rsa.signStringENC(strData,"sha-1");

    strOut.append(hexSig);
    strOut.append("\r\n");

    delete pkey;

    strOut.append("Success!");
    strOut.append("\r\n");


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.