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 256 CBC mode encryption is used. For Android, PKCS5Padding is used and for iOS PKCS7Padding is used.
-
Install Plugins
ionic cordova plugin add cordova-plugin-aes256-encryption
cordova plugin add cordova-plugin-add-swift-support --save
-
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 = '123456789101234567890123456789011'; // Any string, the length should be 32
secureIV: String = '1234567891123456'; // Any string, the length should be 16
constructor(private platform: Platform) {
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.encrypt(secureKey, secureIV, encryptedData,
(decryptedData) => {
console.log('Decrypted Data----', decryptedData);
}, (error) => {
console.log('Error----', error);
});
});
}
}