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) AES GCM Encrypt and Decrypt a FileSee more Encryption ExamplesDemonstrates how to AES GCM encrypt and decrypt a file.
#include <C_CkCrypt2W.h> void ChilkatSample(void) { HCkCrypt2W crypt; const wchar_t *K; const wchar_t *IV; const wchar_t *AAD; BOOL success; const wchar_t *inFile; const wchar_t *outFile; const wchar_t *authTag; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. crypt = CkCrypt2W_Create(); // Set the encryption algorithm to "AES" CkCrypt2W_putCryptAlgorithm(crypt,L"aes"); // Indicate that the Galois/Counter Mode (GCM) should be used: CkCrypt2W_putCipherMode(crypt,L"gcm"); // KeyLength may be 128, 192, 256 CkCrypt2W_putKeyLength(crypt,256); // This is the 256-bit AES secret key (in hex format) K = L"000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"; // This is the 16-byte initialization vector (in hex format) IV = L"000102030405060708090A0B0C0D0E0F"; // This is the OPTIONAL additional data (in hex format) to be used as input to the GCM AEAD algorithm, // but is not included in the output. It plays a role in the computation of the // resulting authenticated tag. AAD = L"feedfacedeadbeeffeedfacedeadbeefabaddad2"; // Set the secret key and IV CkCrypt2W_SetEncodedIV(crypt,IV,L"hex"); CkCrypt2W_SetEncodedKey(crypt,K,L"hex"); // Set the additional authenticated data (AAD) success = CkCrypt2W_SetEncodedAad(crypt,AAD,L"hex"); // Encrypt a file. inFile = L"qa_data/hamlet.xml"; outFile = L"c:/temp/qa_output/hamlet_aes_gcm.enc"; success = CkCrypt2W_CkEncryptFile(crypt,inFile,outFile); if (success == FALSE) { wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt)); CkCrypt2W_Dispose(crypt); return; } // Get the authentication tag in hex format authTag = CkCrypt2W_getEncodedAuthTag(crypt,L"hex"); wprintf(L"authentication tag = %s\n",authTag); // Decrypt.. // Before decrypting, you must provide the expected authentication tag. // The decrypt will fail if the resulting authentication tag computed while decrypting is not equal to the // expected authentication tag. CkCrypt2W_SetEncodedAuthTag(crypt,authTag,L"hex"); inFile = outFile; outFile = L"c:/temp/qa_output/hamlet_restored.xml"; success = CkCrypt2W_CkDecryptFile(crypt,inFile,outFile); if (success == FALSE) { wprintf(L"%s\n",CkCrypt2W_lastErrorText(crypt)); CkCrypt2W_Dispose(crypt); return; } wprintf(L"Success.\n"); // -------------------------------------------------------------------------------------------- // About AES-GCM: // AES-GCM (Advanced Encryption Standard - Galois/Counter Mode) is a widely-used // encryption mode that provides both confidentiality (encryption) and // integrity/authentication (data integrity verification) in one operation. It is // commonly used in secure communications due to its efficiency and strong security // properties. // // Key Concepts: // // AES (Advanced Encryption Standard): // // AES is a symmetric encryption algorithm, meaning the same key is used // for both encryption and decryption. // // It operates on fixed-size blocks of data (128 bits) using key sizes of // 128, 192, or 256 bits. // // In AES-GCM, AES is used to perform the actual data encryption. // // GCM (Galois/Counter Mode): // // Counter Mode (CTR): GCM uses counter mode for encryption. In this mode, // a nonce (or initialization vector, IV) and a counter are combined and encrypted // with AES. The result is XORed with the plaintext to produce the ciphertext. // // Galois Mode (GMAC): GCM also includes an authentication mechanism based // on a Galois field. It generates an authentication tag, which ensures the // integrity of both the ciphertext and any additional data (called AAD - // Additional Authenticated Data). This tag is verified during decryption to ensure // that the data hasn't been tampered with. // // Key Features: // // Confidentiality (Encryption): // // The plaintext is encrypted using AES in counter mode. Each block of // plaintext is XORed with the output of AES applied to a combination of the IV and // an incremented counter. // // Integrity (Authentication): // // In addition to encryption, GCM provides authentication for both the // encrypted data (ciphertext) and any Additional Authenticated Data (AAD), such as // headers or metadata that need to be protected but not encrypted. // // The authentication tag is generated using a Galois field multiplication // of the ciphertext and AAD. This ensures that any changes to the encrypted // message or the AAD will be detected during decryption. // // Key Components: // // - Plaintext: The data you want to encrypt. // - Ciphertext: The encrypted data. // - Key: A symmetric key used for both encryption and decryption. // - Nonce/IV: A unique value used for each encryption to ensure security. It is not secret but should never be reused with the same key. // - AAD (Additional Authenticated Data): Optional data that is not encrypted but needs to be authenticated (e.g., headers). // - Authentication Tag: A tag generated to verify the integrity and authenticity of the ciphertext and AAD CkCrypt2W_Dispose(crypt); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.