# Encryption

# Introduction

ExpressWebJs encryption services provide a simple, convenient way for encrypting and decrypting text via OpenSSL using AES-256 and AES-128 encryption. All of ExpressWebJs encrypted values are signed using a message authentication code (MAC) so that their underlying value can not be modified or tampered with once encrypted.

# Configuration

Before using ExpressWebJs encrypter, you must set the key configuration option in your APP_KEY environment variable in .env file.

# Encrypting A Value

The encryption module generates a unique iv (opens new window) for every encryption call. Hence encrypting the same value twice will result in a different visual output.

import Encryption from "Elucidate/Encryption";

new Encryption().encrypt("Welcome to ExpressWebJs");
Copied!

# Also You can encrypt the following data types.

// Object
new Encryption.encrypt({
  firstName: "Alex",
  lastName: "Igbokwe",
});

// Nested Object:
new Encryption.encrypt({
  foo: {
    bar: [1, "baz"],
  },
});

// Array
new Encryption.encrypt([1, 2, 3, 4]);

// Boolean
new Encryption.encrypt(true);

// Number
new Encryption.encrypt(10);

// Date objects are converted to ISO string
new Encryption.encrypt(new Date());
Copied!

# Decrypt A Value

The Encryption.decrypt method decrypts the encrypted value. Returns null when unable to decrypt the value.

import Encryption from "Elucidate/Encryption";

new Encryption().decrypt(encryptedValue);
Copied!

# Using a custom secret key

ExpressWebJs Encryption module uses the APP_KEY inside .env environment file as the secret for encrypting values. However, you can create a custom secret key as well.

import Encryption from "Elucidate/Encryption";

let encryption = new Encryption();
encryption.setKey("My_custom_key");
Copied!