A JBoss Project
Red Hat

Aerogear Guides Tutorials to help get you off and running.

AeroGear Cordova Crypto

Before we start please install Apache Cordova CLI, which is the command line tool to build, deploy and manage Cordova-based applications.

Read about how OTP works on this AeroGear OTP guide

Example

First let’s create a new project and install the plugin.

cordova create <app name>
cd <app name>
cordova platform add android
cordova platform add ios
cordova plugin add https://github.com/aerogear/aerogear-crypto-cordova.git

for use in ios, you’ll need to run pod install

Password based key derivation support (PBKDF2)

var agCrypto = new AeroGear.Crypto();
agCrypto.deriveKey('my password', null, {
    success: function (rawPassword) {
        console.log(rawPassword);
    }
});

Symmetric encryption support (GCM)

  • Encryption

$.when(agCrypto.deriveKey('my password'), agCrypto.getRandomValue())
    .then(function (rawPassword, IV) {
            var options = {
                IV: IV[0],
                key: rawPassword[0],
                data: "My Bonnie lies over the ocean, my Bonnie lies over the sea"
            };
            agCrypto.encrypt(options).then(function (cipherText) {
                    console.log(cipherText);
                }
            });
        },
        function (error) {
            console.log('error ' + error);
        });

// or with regular callbacks

agCrypto.deriveKey('my password', null, {
    success: function (rawPassword) {
        agCrypto.getRandomValue({
            success: function (generatedIV) {
                var options = {
                    IV: generatedIV,
                    key: rawPassword,
                    data: "My Bonnie lies over the ocean, my Bonnie lies over the sea"
                };
                agCrypto.encrypt(options {
                    success: function (cipherText) {
                        console.log(cipherText);
                    }
                });
            }
        });
    }
});
  • Decryption

var options = {
    IV: "69696ee955b62b73cd62bda875fc73d68219e0036b7a0b37",
    AAD: "feedfacedeadbeeffeedfacedeadbeefabaddad2",
    key: rawPassword,
    data: cipherText
};
agCrypto.decrypt(options, {
    success: function (text) {
        console.log(text);
    }
});

Asymmetric encryption support (ECC) / iOS not supported

$.when(agCrypto.KeyPair(), agCrypto.KeyPair(), agCrypto.getRandomValue())
    .then(function (keyPair, keyPairPandora, IV) {
        var options = {
            IV: IV[0],
            key: new agCrypto.KeyPair(keyPair[0].privateKey, keyPairPandora[0].publicKey),
            data: "My bonnie lies over the ocean"
        };
        agCrypto.encrypt(options).then(function (cipherText) {
            options.key = new agCrypto.KeyPair(keyPairPandora[0].privateKey, keyPair[0].publicKey);
            options.data = cipherText;
            agCrypto.decrypt(options).then(function (plainText) {
                console.log('plainText ' + plainText);
            });
        });

    });

redhatlogo-wite