如何保护cordova中的localstorage和websql数据 - 离子应用

时间:2015-06-20 12:30:14

标签: html5 local-storage ionic-framework hybrid-mobile-app web-sql

存储在本地存储或WebSql数据库中的数据不受保护。 我们可以直接看到WebSql和本地存储的所有数据,因为它们以纯文本形式存储。

有什么方法可以保护数据吗?

1 个答案:

答案 0 :(得分:3)

是的,您可以使用AES或其他算法加密/解密您的数据。也许您可以尝试实施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());