Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode C Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
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)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Unicode C) RSAES-OAEP Encrypt/Decrypt Binary Data with AES-128 and SHA56

Demonstrates the use of the new EncryptBd and DecryptBd methods introduced in Chilkat v9.5.0.67 to create a PKCS7/CMS (Cryptographic Message Syntax) message using RSAES-OAEP with AES-128 and SHA256.

Note: This example requires Chilkat v9.5.0.67 or greater.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <C_CkBinDataW.h>
#include <C_CkCrypt2W.h>
#include <C_CkCertW.h>

void ChilkatSample(void)
    {
    HCkBinDataW jpgBytes;
    BOOL success;
    HCkCrypt2W crypt;
    HCkCertW cert;

    // This example requires the Chilkat Crypt API to have been previously unlocked.
    // See Unlock Chilkat Crypt for sample code.

    // Load a small JPG file to be encrypted/decrypted.
    jpgBytes = CkBinDataW_Create();
    success = CkBinDataW_LoadFile(jpgBytes,L"qa_data/jpg/starfish20.jpg");
    if (success != TRUE) {
        wprintf(L"Failed to load JPG file.\n");
        CkBinDataW_Dispose(jpgBytes);
        return;
    }

    // Show the unencrypted JPG bytes in Base64 format.
    // (The "base64_mime" encoding was added in Chilkat v9.5.0.67.
    // The "base64" encoding emits a single line of base64, whereas
    // "base64_mime" will emit multi-line base64 as it would appear
    // in MIME.)
    wprintf(L"%s\n",CkBinDataW_getEncoded(jpgBytes,L"base64_mime"));

    // Sample base64_mime JPG data:

    // /9j/4AAQSkZJRgABAQEASABIAAD//gAmRmlsZSB3cml0dGVuIGJ5IEFkb2JlIFBob3Rvc2hvcD8g
    // NC4w/9sAQwAQCwwODAoQDg0OEhEQExgoGhgWFhgxIyUdKDozPTw5Mzg3QEhcTkBEV0U3OFBtUVdf
    // YmdoZz5NcXlwZHhcZWdj/9sAQwEREhIYFRgvGhovY0I4QmNjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj
    // Y2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2Nj/8IAEQgAFAAUAwERAAIRAQMRAf/EABcAAAMBAAAA
    // AAAAAAAAAAAAAAIDBAX/xAAYAQADAQEAAAAAAAAAAAAAAAABAgMEAP/aAAwDAQACEAMQAAAB2kZY
    // NNEijWKddfTmLgALWH//xAAbEAACAgMBAAAAAAAAAAAAAAABAgMRAAQSE//aAAgBAQABBQL0XqN+
    // pM2aqJGMiqFFCyg7z//EABwRAAICAgMAAAAAAAAAAAAAAAERAAIQIQMSUf/aAAgBAwEBPwHqU5aq
    // Axx+y1tMQl4elj//xAAcEQEAAQUBAQAAAAAAAAAAAAABEQACEBIhA1H/2gAIAQIBAT8B3Bhqy7Zc
    // enyiwmGgDhiOzj//xAAdEAABAwUBAAAAAAAAAAAAAAABAAIREBIhIkFR/9oACAEBAAY/ArZyn+Cg
    // xtxWuJaoCnqDuin/xAAcEAABBAMBAAAAAAAAAAAAAAABABEhYRAxQVH/2gAIAQEAAT8hkEwPUUR9
    // DYfE4nxtRpIkBTsayuALIiuY/9oADAMBAAIAAwAAABDWPTsf/8QAGhEAAwADAQAAAAAAAAAAAAAA
    // AAEREDFBIf/aAAgBAwEBPxC0DVPcWm+Ce4OesrkE6bjH/8QAGBEBAQEBAQAAAAAAAAAAAAAAAREA
    // QRD/2gAIAQIBAT8QahMiOc8YgSrnTY3ELclHXn//xAAcEAEBAAIDAQEAAAAAAAAAAAABEQAhMUFx
    // EFH/2gAIAQEAAT8Qn3igmSZSj+c4N4zapMy9IjFV98wncN2iuLFsCEbDGxQkI6RO/n//2Q==

    crypt = CkCrypt2W_Create();

    // Specify the encryption to be used.
    // "pki" indicates "Public Key Infrastructure" and will create a PKCS7/CMS message.
    CkCrypt2W_putCryptAlgorithm(crypt,L"pki");
    CkCrypt2W_putPkcs7CryptAlg(crypt,L"aes");
    CkCrypt2W_putKeyLength(crypt,128);
    CkCrypt2W_putOaepHash(crypt,L"sha256");
    CkCrypt2W_putOaepPadding(crypt,TRUE);

    // A certificate is needed as the encryption key..
    cert = CkCertW_Create();
    success = CkCertW_LoadFromFile(cert,L"qa_data/rsaes-oaep/cert.pem");
    if (success != TRUE) {
        wprintf(L"%s\n",CkCertW_lastErrorText(cert));
        CkBinDataW_Dispose(jpgBytes);
        CkCrypt2W_Dispose(crypt);
        CkCertW_Dispose(cert);
        return;
    }

    // Tell the crypt object to use the certificate.
    CkCrypt2W_SetEncryptCert(crypt,cert);

    // Do the in-place RSAES-OAEP encryption.
    // The contents of jpgBytes are replaced with the CMS message.
    success = CkCrypt2W_EncryptBd(crypt,jpgBytes);
    if (success != TRUE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkBinDataW_Dispose(jpgBytes);
        CkCrypt2W_Dispose(crypt);
        CkCertW_Dispose(cert);
        return;
    }

    // Examine the JPG bytes again.  The bytes should be different because they are encrypted:
    wprintf(L"%s\n",CkBinDataW_getEncoded(jpgBytes,L"base64_mime"));

    // Sample CMS message:
    // This CMS message can be copy-and-pasted into the online web form
    // at https://lapo.it/asn1js/   to verify the algorithms used.

    // MIIFDAYJKoZIhvcNAQcDoIIE/TCCBPkCAQAxggGgMIIBnAIBADB1MGgxCzAJBgNVBAYTAlVTMQsw
    // CQYDVQQIDAJJTDEQMA4GA1UEBwwHV2hlYXRvbjEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQ
    // dHkgTHRkMRcwFQYDVQQDDA5DaGlsa2F0V2lkZ2V0cwIJAMRwugDmvniwMBwGCSqGSIb3DQEBBzAP
    // oA0wCwYJYIZIAWUDBAIBBIIBAHyWLgkJfIvoA3cYEAR/uHfA7uoi4eQXHl2woQAd6W5BbUNVBcYD
    // zuCTTOTWo1e7Uh0j0AhMZvVQf3+cngTiimzKwIZ2LNuNAgYOhrO/7coHyB22ImVetncUpCsAv/u7
    // 2tYp1dO36T56K+2hMELwcQXTj6v5ODO6a3emdCjITCjHhlYePvq2l0HyU2ALG5RFB6ldk9imhKzn
    // 6gfcijfH65S+KfMRlFBCXFu5nCBKmi8Ywo8Ue0rFssUDKLCjCeQNY52symiDbN/d06K/luOUwVhY
    // 1KQ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apgUXYj6AVVvMiC1pRld9wSuHH0vW0x5VsNbmXy
    // EY0NJlJY6II/1szy3bpiP6MsqFveCyCX8rM7UgGgpMNbvWPnsX0F/0eJywIrXrnQYXpvfgghIrlr
    // qu/ftXWypfcfvATxme+cN2EBsCDhq4VcMgB6JY3ykv6P8PK/QpMTbu4Y

    // To decrypt, we'll need the cert with private key.  
    // (The certificate alone contains only the public key.)
    // Provide the required cert + private key from a PFX (.pfx/.p12) file.
    success = CkCrypt2W_AddPfxSourceFile(crypt,L"qa_data/rsaes-oaep/cert_plus_privatekey.pfx",L"PFX_PASSWORD");
    if (success != TRUE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkBinDataW_Dispose(jpgBytes);
        CkCrypt2W_Dispose(crypt);
        CkCertW_Dispose(cert);
        return;
    }

    // Decrypt to restore back to the original:
    success = CkCrypt2W_DecryptBd(crypt,jpgBytes);
    if (success != TRUE) {
        wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt));
        CkBinDataW_Dispose(jpgBytes);
        CkCrypt2W_Dispose(crypt);
        CkCertW_Dispose(cert);
        return;
    }

    wprintf(L"%s\n",CkBinDataW_getEncoded(jpgBytes,L"base64_mime"));


    CkBinDataW_Dispose(jpgBytes);
    CkCrypt2W_Dispose(crypt);
    CkCertW_Dispose(cert);

    }

 

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