Chilkat Examples

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

MFC Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
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
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
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(MFC) AES XTS Mode where Tweak Value is Data Unit Number

Demonstrates the AES-XTS mode of operation where the tweak value is specified as a data unit number.

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

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkBinData.h>
#include <CkCrypt2.h>

void ChilkatSample(void)
    {
    CkString strOut;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    bool success;

    // IEEE 1619 test vector 10
    const char *key = "2718281828459045235360287471352662497757247093699959574966967627";
    const char *tweakKey = "3141592653589793238462643383279502884197169399375105820974944592";
    // The data unit number is 255
    // We'll specify this numerically by calling XtsSetDataUnitNumber with 255, 0 (see below)
    const char *dataUnit = "00000000000000ff";

    CkBinData bd;

    bd.AppendEncoded("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","hex");
    bd.AppendEncoded("202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f","hex");
    bd.AppendEncoded("404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f","hex");
    bd.AppendEncoded("606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f","hex");
    bd.AppendEncoded("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f","hex");
    bd.AppendEncoded("a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf","hex");
    bd.AppendEncoded("c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf","hex");
    bd.AppendEncoded("e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff","hex");
    bd.AppendEncoded("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f","hex");
    bd.AppendEncoded("202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f","hex");
    bd.AppendEncoded("404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f","hex");
    bd.AppendEncoded("606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f","hex");
    bd.AppendEncoded("808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f","hex");
    bd.AppendEncoded("a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf","hex");
    bd.AppendEncoded("c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf","hex");
    bd.AppendEncoded("e0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff","hex");

    strOut.append("Size of plain-text: ");
    strOut.appendInt(bd.get_NumBytes());
    strOut.append("\r\n");

    // This is the expected ciphertext:
    // cipherText =             "1c3b3a102f770386e4836c99e370cf9bea00803f5e482357a4ae12d414a3e63b"
    //                        + "5d31e276f8fe4a8d66b317f9ac683f44680a86ac35adfc3345befecb4bb188fd"
    //                        + "5776926c49a3095eb108fd1098baec70aaa66999a72a82f27d848b21d4a741b0"
    //                        + "c5cd4d5fff9dac89aeba122961d03a757123e9870f8acf1000020887891429ca"
    //                        + "2a3e7a7d7df7b10355165c8b9a6d0a7de8b062c4500dc4cd120c0f7418dae3d0"
    //                        + "b5781c34803fa75421c790dfe1de1834f280d7667b327f6c8cd7557e12ac3a0f"
    //                        + "93ec05c52e0493ef31a12d3d9260f79a289d6a379bc70c50841473d1a8cc81ec"
    //                        + "583e9645e07b8d9670655ba5bbcfecc6dc3966380ad8fecb17b6ba02469a020a"
    //                        + "84e18e8f84252070c13e9f1f289be54fbc481457778f616015e1327a02b140f1"
    //                        + "505eb309326d68378f8374595c849d84f4c333ec4423885143cb47bd71c5edae"
    //                        + "9be69a2ffeceb1bec9de244fbe15992b11b77c040f12bd8f6a975a44a0f90c29"
    //                        + "a9abc3d4d893927284c58754cce294529f8614dcd2aba991925fedc4ae74ffac"
    //                        + "6e333b93eb4aff0479da9a410e4450e0dd7ae4c6e2910900575da401fc07059f"
    //                        + "645e8b7e9bfdef33943054ff84011493c27b3429eaedb4ed5376441a77ed4385"
    //                        + "1ad77f16f541dfd269d50d6a5f14fb0aab1cbb4c1550be97f7ab4066193c4caa"
    //                        + "773dad38014bd2092fa755c824bb5e54c4f36ffda9fcea70b9c6e693e148c151";

    CkCrypt2 crypt;

    crypt.put_CryptAlgorithm("aes");
    crypt.put_CipherMode("xts");
    crypt.put_KeyLength(256);

    crypt.SetEncodedKey(key,"hex");
    crypt.XtsSetEncodedTweakKey(tweakKey,"hex");

    // Here we set the tweak value as a data unit number (a 64-bit integer passed in 2 32-bit values)
    crypt.XtsSetDataUnitNumber(255,0);

    // In-place encrypt the contents of bd.
    success = crypt.EncryptBd(bd);

    strOut.append("CT = ");
    strOut.append(bd.getEncoded("hexlower"));
    strOut.append("\r\n");

    // Decrypt to revert back to the unencrypted content:
    success = crypt.DecryptBd(bd);

    strOut.append("PT = ");
    strOut.append(bd.getEncoded("hexlower"));
    strOut.append("\r\n");


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

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