Hi, my encryption code works very well, but i cannot decrypt it .please help me.
my encryption code is below
var db: OpaquePointer? = nil
let databasePath = Util.getPath("demo.sqlite")
if (sqlite3_open(databasePath, &db) == SQLITE_OK) {
let aStr = String(format: "%@/Documents/encrypted1.sqlite",NSHomeDirectory())
print("Database Path: ", aStr)
let newQuery = "ATTACH DATABASE '\(aStr)' AS encrypted KEY 'ganesh';"
print("NewQuery: ", newQuery)
if sqlite3_exec(db, newQuery, nil, nil, nil) == SQLITE_OK {
if sqlite3_exec(db, "SELECT sqlcipher_export('encrypted');", nil, nil, nil) == SQLITE_OK{
if sqlite3_exec(db, "DETACH DATABASE encrypted;", nil, nil, nil) == SQLITE_OK{
print("Encryption succesful")
}else{
let errmsg = String(cString: sqlite3_errmsg(db))
NSLog("DETACH DATABASE encrypted: \(errmsg)")
}
}else{
let errmsg = String(cString: sqlite3_errmsg(db))
NSLog("Error sqlcipher_export: \(errmsg)")
}
}else{
let errmsg = String(cString: sqlite3_errmsg(db))
NSLog("Error opening database: \(errmsg)")
}
sqlite3_close(db);
}else{
sqlite3_close(db);
sqlite3_errmsg(db);
}
/////// here is my decryption code
var db: OpaquePointer? = nil
var stmt: OpaquePointer? = nil
var rc: Int32
// decryption code
let databasePath_NewFile = Util.getPath("demo.sqlite")
let newDstPath11 = String(format: "%@/Documents/encrypted1.sqlite",NSHomeDirectory())
let newFullDestPath = NSURL(fileURLWithPath: newDstPath11)
let newDstPath = newFullDestPath.path
// first open encrypted database
if (sqlite3_open(newDstPath11, &db) == SQLITE_OK) {
print("Database open successfully.")
let result = sqlite3_exec(db, "PRAGMA key = 'ganesh';", nil, nil, nil);
print("Result: ", result)
rc = sqlite3_prepare(db, "PRAGMA cipher_version;", -1, &stmt, nil)
if (rc != SQLITE_OK) {
let errmsg = String(cString: sqlite3_errmsg(db))
NSLog("Error preparing SQL: \(errmsg)")
}
/*
sqlite> PRAGMA key = 'testkey';
sqlite> ATTACH DATABASE 'plaintext.db' AS plaintext KEY ''; -- empty key will disable encryption
sqlite> SELECT sqlcipher_export('plaintext');
sqlite> DETACH DATABASE plaintext;
*/
let newQuery1 = "ATTACH DATABASE '\(newDstPath11)' AS plaintext KEY '';"
print("NewQuery: ", newQuery1)
if sqlite3_exec(db, newQuery1, nil, nil, nil) == SQLITE_OK {
if sqlite3_exec(db, "SELECT sqlcipher_export('plaintext');", nil, nil, nil) == SQLITE_OK{
if sqlite3_exec(db, "DETACH DATABASE plaintext;", nil, nil, nil) == SQLITE_OK{
print("Everything ok:")
}else{
let errmsg = String(cString: sqlite3_errmsg(db))
NSLog("DETACH DATABASE encrypted: \(errmsg)")
}
}else{
let errmsg = String(cString: sqlite3_errmsg(db))
NSLog("Error sqlcipher_export: \(errmsg)")
}
}else{
let errmsg = String(cString: sqlite3_errmsg(db))
NSLog("Error opening database: \(errmsg)")
}
}
///// it gives error like
Database open successfully.
Result: 0
NewQuery: ATTACH DATABASE ‘/var/mobile/Containers/Data/Application/9D4EA875-1F99-4642-AFC3-DE5E69F4B47D/Documents/encrypted1.sqlite’ AS plaintext KEY ‘’;
2017-11-28 10:29:32.604385+0530 SQLCipherDemo_21nov[4365:2318614] Error opening database: file is encrypted or is not a database
please help me.