Sample code for 30+ languages & platforms
Dart

Encrypt / Decrypt Secure Strings

See more Encryption Examples

Demonstrates how to use the EncryptSecureENC and DecryptSecureENC methods to encrypt/decrypt secure strings. These methods were added in Chilkat v9.5.0.71 (released January 2018).

Chilkat Dart Downloads

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

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

  // Load the secure string with some text.
  final secStr1 = CkSecureString();
  try {
    secStr1.loadFile('qa_data/txt/helloWorld.txt', 'utf-8');
  } on ChilkatException {
    print('Failed to load helloWorld.txt');
    return;
  }

  final crypt = CkCrypt2();

  crypt.cryptAlgorithm = 'aes';
  crypt.cipherMode = 'cbc';
  crypt.keyLength = 128;
  crypt.setEncodedKey('000102030405060708090A0B0C0D0E0F', 'hex');
  crypt.setEncodedIV('000102030405060708090A0B0C0D0E0F', 'hex');
  crypt.encodingMode = 'base64';

  // Return the base64 encoded encrypted contents of secStr1.
  final encryptedStr = crypt.encryptSecureENC(secStr1);
  print('Encrypted string: $encryptedStr');

  // Output:
  // Encrypted string: qiq+IFhcjTkEIkZyf31V/g==

  // Decrypt to secStr2:
  final secStr2 = CkSecureString();
  crypt.decryptSecureENC(encryptedStr, secStr2);

  // Access the contents of secStr2
  print('Decrypted string: ${secStr2.access()}');

  // Output:
  // Decrypted string: Hello World!
}