Sample code for 30+ languages & platforms
React Native

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 React Native Downloads

React Native
import { Crypt2, SecureString } from '@chilkat/react-native'

function chilkatExample() {
  // 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.
  const secStr1 = new SecureString();
  try {
    secStr1.loadFile('qa_data/txt/helloWorld.txt', 'utf-8');
  } catch {
    console.log('Failed to load helloWorld.txt');
    return;
  }

  const crypt = new Crypt2();

  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.
  const encryptedStr = crypt.encryptSecureENC(secStr1);
  console.log(`Encrypted string: ${encryptedStr}`);

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

  // Decrypt to secStr2:
  const secStr2 = new SecureString();
  crypt.decryptSecureENC(encryptedStr, secStr2);

  // Access the contents of secStr2
  console.log(`Decrypted string: ${secStr2.access()}`);

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