Ionic Framework 4 is almost here. Preview the beta docs and try it out now! Try Ionic 4 Beta

AES256

Improve this doc

This cordova ionic plugin allows you to perform AES 256 encryption and decryption on the plain text. It's a cross-platform plugin which supports both Android and iOS. The encryption and decryption are performed on the device native layer so that the performance is much faster.

Repo: https://github.com/Ideas2IT/cordova-aes256

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add cordova-plugin-aes256-encryption
    $ npm install --save @ionic-native/aes-256
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { AES256 } from '@ionic-native/aes-256';

private secureKey: string;
private secureIV: string;

constructor(private aes256: AES256) {
   this.generateSecureKeyAndIV(); // To generate the random secureKey and secureIV
}

...

async generateSecureKeyAndIV() {
   this.secureKey = await this.aes256.generateSecureKey('random password 12345'); // Returns a 32 bytes string
   this.secureIV = await this.aes256.generateSecureIV('random password 12345'); // Returns a 16 bytes string
}

this.aes256.encrypt(this.secureKey, this.secureIV, 'testdata')
  .then(res => console.log('Encrypted Data: ',res))
  .catch((error: any) => console.error(error));

this.aes256.decrypt(this.secureKey, this.secureIV, 'encryptedData')
  .then(res => console.log('Decrypted Data : ',res))
  .catch((error: any) => console.error(error));


* this.aes256.generateSecureKey('random password 12345')
  .then(res => console.log('Secure Key : ',res))
  .catch((error: any) => console.error(error));


* this.aes256.generateSecureIV('random password 12345')
  .then(res => console.log('Secure IV : ',res))
  .catch((error: any) => console.error(error));

Instance Members

encrypt(secureKey, secureIV, data)

This function used to perform the aes256 encryption

Param Type Details
secureKey string

A 32 bytes string, which will used as input key for AES256 encryption.

secureIV string

A 16 bytes string, which will used as initial vector for AES256 encryption.

data string

A string which will be encrypted

Returns: Promise<string> Returns a promise that resolves when encryption happens. The success response will returns encrypted data.

decrypt(secureKey, secureIV, data)

This function used to perform the aes256 decryption

Param Type Details
secureKey string

A 32 bytes string, which will used as input key for AES256 decryption.

secureIV string

A 16 bytes string, which will used as initial vector for AES256 decryption.

data string

An AES256 encrypted data which will be decrypted.

Returns: Promise<string> Returns a promise that resolves when decryption happens. The success response will returns decrypted data.

generateSecureKey(password)

This function used to generate a secure key based on an password. Perfect if you want to delegate the key generation for encryption to the plugin. Make sure to save the return value of this function somewhere so your encrypted data can be decrypted in the future.

Param Type Details
password string

A random string, which will be used as input for a PBKDF2 function

Returns: Promise<string> Returns a promise that resolves when key is generated.

generateSecureIV(password)

This function used to generate a secure IV based on an password. Perfect if you want to delegate the IV generation for encryption to the plugin. Make sure to save the return value of this function somewhere so your encrypted data can be decrypted in the future.

Param Type Details
password string

A random string, which will be used as input for a PBKDF2 function

Returns: Promise<string> Returns a promise that resolves when IV is generated.

API

Native

General