Search code examples
htmllocal-storageionic-frameworkhybrid-mobile-appweb-sql

How to protect localstorage and websql data in cordova - ionic application


data stored in local-storage or in WebSql database is not protected. we can directly see all the data of WebSql and local-storage because they are stored as plain text.

is there any way to protect data?


Solution

  • yes, you can encrypt/decrypt your data using something like AES or other Algorithm. Maybe you can try implementation https://github.com/digitalbazaar/forge#md5

    // generate a random key and IV
    // Note: a key size of 16 bytes will use AES-128, 24 => AES-192, 32 => AES-256
    var key = forge.random.getBytesSync(16);
    var iv = forge.random.getBytesSync(16);
    
    /* alternatively, generate a password-based 16-byte key
    var salt = forge.random.getBytesSync(128);
    var key = forge.pkcs5.pbkdf2('password', salt, numIterations, 16);
    */
    
    // encrypt some bytes using CBC mode
    // (other modes include: CFB, OFB, CTR, and GCM)
    var cipher = forge.cipher.createCipher('AES-CBC', key);
    cipher.start({iv: iv});
    cipher.update(forge.util.createBuffer(someBytes));
    cipher.finish();
    var encrypted = cipher.output;
    // outputs encrypted hex
    console.log(encrypted.toHex());
    
    // decrypt some bytes using CBC mode
    // (other modes include: CFB, OFB, CTR, and GCM)
    var decipher = forge.cipher.createDecipher('AES-CBC', key);
    decipher.start({iv: iv});
    decipher.update(encrypted);
    decipher.finish();
    // outputs decrypted hex
    console.log(decipher.output.toHex());