Skip to content
No description, website, or topics provided.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Ios Swift4 support added. Feb 27, 2019
www Android: Generate secure key and secure IV utility added Aug 13, 2018
.gitignore
LICENSE Cordova AES 256 encryption added. Jun 21, 2018
README.md
package.json Release version changes - v1.2.0 Feb 27, 2019
plugin.xml

README.md

Cordova-AES256 Encryption And Decryption

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.

AES Encryption Mode

AES 256 CBC mode encryption is used. For Android, PKCS5Padding is used and for iOS PKCS7Padding is used.

Getting Started

  1. Install Plugins
    ionic cordova plugin add cordova-plugin-aes256-encryption

    cordova plugin add cordova-plugin-add-swift-support --save

  2. Declare cordova variable and access the plugin after the platform get initialized

import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular/index';
declare var cordova: any;

@Injectable()
export class AES256Provider {

  secureKey: String = '12345678910123456789012345678901'; // Any string, the length should be 32
  secureIV: String = '1234567891123456'; // Any string, the length should be 16

  constructor(private platform: Platform) {
      // To generate random secure key
      this.generateSecureKey('some string');  // Optional
      
      // To generate random secure IV
      this.generateSecureIV('some string');   // Optional
      
      let data = "test";
      encrypt(this.secureKey, this.secureIV, data); 
      let encryptedData = "AE#3223==";
      decrypt(this.secureKey, this.secureIV, encryptedData);  
  }

  encrypt(secureKey, secureIV, data) {
    this.platform.ready().then(() => {
      cordova.plugins.AES256.encrypt(secureKey, secureIV, data,
        (encrypedData) => {
          console.log('Encrypted Data----', encrypedData);
        }, (error) => {
          console.log('Error----', error);
        });
    });
  }

  decrypt(secureKey, secureIV, encryptedData) {
    this.platform.ready().then(() => {
      cordova.plugins.AES256.decrypt(secureKey, secureIV, encryptedData,
        (decryptedData) => {
          console.log('Decrypted Data----', decryptedData);
        }, (error) => {
          console.log('Error----', error);
        });
    });
  }
  
  generateSecureKey(password) {
    this.platform.ready().then(() => {
      cordova.plugins.AES256.generateSecureKey(password,
        (secureKey) => {
          this.secureKey = secureKey;
          console.log('Secure Key----', secureKey);          
        }, (error) => {
          console.log('Error----', error);
        });
    });
  }
  
  generateSecureIV(password) {
    this.platform.ready().then(() => {
      cordova.plugins.AES256.generateSecureIV(password,
        (secureIV) => {
          this.secureIV = secureIV;
          console.log('Secure IV----', secureIV);          
        }, (error) => {
          console.log('Error----', error);
        });
    });
  }

}

References

https://developer.android.com/reference/javax/crypto/Cipher

https://github.com/SwiftyBeaver/AES256CBC

Known issues

Sometimes you will see a warning message in the console like this:

2018-08-31 17:59:54.204442+0200 App Name[645:64889] THREAD WARNING: ['AES256'] took '43.134766' ms. Plugin should use a background thread.

It's by design and no error. See [https://github.com/Ideas2IT/cordova-aes256/issues/4](this issue) for more information on this warning.

You can’t perform that action at this time.