Sample code for 30+ languages & platforms
Dart

Example: Crypt2.RandomizeIV method

Demonstrates using a random initialization vector for AES GCM encryption.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

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

  final crypt = CkCrypt2();

  crypt.cryptAlgorithm = 'aes';
  crypt.cipherMode = 'gcm';
  crypt.keyLength = 256;

  final k = '000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F';
  final aad = 'feedfacedeadbeeffeedfacedeadbeefabaddad2';
  final pt = 'This is the text to be AES-GCM encrypted.';

  // Generate a random IV.
  crypt.randomizeIV();
  final iv = crypt.getEncodedIV('hex');

  crypt.setEncodedKey(k, 'hex');

  crypt.setEncodedAad(aad, 'hex');

  // Return the encrypted bytes as base64
  crypt.encodingMode = 'base64';
  crypt.charset = 'utf-8';
  final String cipherText;
  try {
    cipherText = crypt.encryptStringENC(pt);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the GCM authenticated tag computed when encrypting.
  final authTag = crypt.getEncodedAuthTag('base64');

  print('Cipher Text: $cipherText');
  print('Auth Tag: $authTag');

  // Let's send the IV, CipherText, and AuthTag to the decrypting party.
  // We'll send them concatenated like this: [IV || Ciphertext || AuthTag]
  // In base64 format.
  final bdEncrypted = CkBinData();
  bdEncrypted.appendEncoded(iv, 'hex');
  bdEncrypted.appendEncoded(cipherText, 'base64');
  bdEncrypted.appendEncoded(authTag, 'base64');

  final concatenatedGcmOutput = bdEncrypted.getEncoded('base64');
  print('Concatenated GCM Output: $concatenatedGcmOutput');

  // Sample output so far:

  // -------------------------------------------------------------------------------------
  // Now let's GCM decrypt...
  // -------------------------------------------------------------------------------------

  final decrypt = CkCrypt2();

  // The values shared and agreed upon by both sides beforehand are: algorithm, cipher mode, secret key, and AAD.
  // Sometimes the IV can be a value already known and agreed upon, but in this case the encryptor sends the IV to the decryptor.
  decrypt.cryptAlgorithm = 'aes';
  decrypt.cipherMode = 'gcm';
  decrypt.keyLength = 256;
  decrypt.setEncodedKey(k, 'hex');
  decrypt.setEncodedAad(aad, 'hex');

  final bdFromEncryptor = CkBinData();
  bdFromEncryptor.appendEncoded(concatenatedGcmOutput, 'base64');

  final sz = bdFromEncryptor.numBytes;

  // Extract the parts.
  final extractedIV = bdFromEncryptor.getEncodedChunk(0, 16, 'hex');
  final extractedCipherText = bdFromEncryptor.getEncodedChunk(16, sz - 32, 'base64');
  final expectedAuthTag = bdFromEncryptor.getEncodedChunk(sz - 16, 16, 'base64');

  // Before GCM decrypting, we must set the authenticated tag to the value that is expected.
  // The decryption will fail if the resulting authenticated tag is not equal to the expected result.
  decrypt.setEncodedAuthTag(expectedAuthTag, 'base64');

  // Also set the IV.
  decrypt.setEncodedIV(extractedIV, 'hex');

  // Decrypt..
  decrypt.encodingMode = 'base64';
  decrypt.charset = 'utf-8';
  final String decryptedText;
  try {
    decryptedText = decrypt.decryptStringENC(extractedCipherText);
  } on ChilkatException catch (e) {
    // Failed.  The resultant authenticated tag did not equal the expected authentication tag.
    print(e.lastErrorText);
    return;
  }

  print('Decrypted: $decryptedText');
}