jayralencar / sqlite-cipher.js

Node.js Module to handle encrypted sqlite databases
MIT License
37 stars 12 forks source link

Using Initialization Vector #5

Closed jayralencar closed 8 years ago

jayralencar commented 8 years ago

@asanchezgomez install version 0.3.1.

And use

var sqlite = require("sqlite-cipher");
sqlite.iv = "Your key";
asanchezgomez commented 8 years ago

Okey thanks I will try it ASAP during this week and I will let you know. It is necessary to change in this new version the source code that you mentioned in the "Uncaught abort: Cannot enlarge memory arrays" issue?

Thanks for your daily effort!

asanchezgomez commented 8 years ago

Hi! I think that we have to reopen this issue. I've tested this functionality and I think it does not work. I'm trying with the next code, and it allows me to insert two registers into database through two database connections with different initialization vectors:

//Database
var sqlite = require('sqlite-cipher');

//Keys creation
var rand = require('random-seed').create();
rand.seed("alex");
var keyAES = rand.string(32);
rand.seed("alex");
var iv = rand.string(16);

//Correct conexion
try{
    sqlite.iv = iv;
    sqlite.connect('encrypted.db',keyAES,'aes-256-cbc');
    sqlite.run('PRAGMA foreign_keys = ON');
    console.log("CONNECTION1 OK");
}catch(x){
    console.log("CONNECTION1 ERR");
    console.log(x)
}

var appID = 1234;
var userPassword ="mypass";

//Create  table
sqlite.run("CREATE TABLE MyTable (appID integer PRIMARY KEY, userPassword text);");

//Insert row
sqlite.insert('MyTable',{appID: appID, userPassword: userPassword});

//SELECT
var rows = sqlite.run("SELECT * from MyTable WHERE userPassword = ?",[userPassword]);
console.log("SELECT1: "+rows.length);
for(var i = 0; i < rows.length; i++)
    console.log(rows[i].appID);

//New iv
rand = require('random-seed').create();
rand.seed("pepe");
var new_iv = rand.string(16);

//New connection
try{
    sqlite.iv = new_iv;
    sqlite.connect('encrypted.db',keyAES,'aes-256-cbc');
    sqlite.run('PRAGMA foreign_keys = ON');
    console.log("CONNECTION3 OK");
}catch(x){
    console.log("CONNECTION3 ERR");
    console.log(x)
}

//New insert
sqlite.insert('MyTable',{appID: 2345, userPassword: 'myPass2'});
var rows = sqlite.run("SELECT * from MyTable");
console.log("SELECT2: "+rows.length);
for(var i = 0; i < rows.length; i++)
    console.log(rows[i].appID);

sqlite.close();

Best regards

jayralencar commented 8 years ago

Hello @asanchezgomez I did not understand what the problem is.

asanchezgomez commented 8 years ago

Hi @jayralencar ! In the second connection (//New connection), I use a different Initialization Vector (new_iv) in order to connect to the database again, which was encrypted before with keyAES and iv. Therefore, if you change the Initialization Vector, you can not decrypt the database because was encrypted previously with a different Initialization Vector. The same would happen if you change the AES key. AES 256 CBC scheme

jayralencar commented 8 years ago

Hi @asanchezgomez I found that problem. I'm fixing it. But, I need to know what you think about the form which the initialization vector is entered. What do you prefer, 1 or 2? 1 - Today

sqlite.iv = "YOUR_IV";
sqlite.connect('DATABASE','PASSWORD','ALGORITHM');

or 2 - Possibility

sqlite.connect('DATABASE','PASSWORD','ALGORITHM','YOUR_IV');

Thanks.