Skip to content

Instantly share code, notes, and snippets.

@Bigpet
Created February 28, 2018 23:02
Show Gist options
  • Save Bigpet/84b2b35223d5badc86ca6b2771878d46 to your computer and use it in GitHub Desktop.
Save Bigpet/84b2b35223d5badc86ca6b2771878d46 to your computer and use it in GitHub Desktop.
sqlite3 win32 crypt example
// sqtest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "sqlite3.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <Wincrypt.h>
#include<iostream>
#pragma comment(lib, "Crypt32.lib")
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void MyHandleError(const char *s);
int readCryptExample(sqlite3 *db) {
//se sqlite3_prepare / sqlite3_step / sqlite3_column_bytes / sqlite3_column_blob.
sqlite3_stmt *stmt = nullptr;
std::string str = "SELECT password_value FROM logins;";
int res = sqlite3_prepare_v2(db, str.c_str(), str.size(), &stmt, nullptr);
std::cout << "res: " << res << std::endl;
//res = sqlite3_bind_blob(stmt, 1, (char*)DataVerify.pbData, DataVerify.cbData, SQLITE_STATIC);
//res = sqlite3_exec(db, str.c_str(),nullptr,nullptr,nullptr);
if (res != SQLITE_OK) {
std::cout << "bind failed: " << sqlite3_errmsg(db) << std::endl;
}
else {
res = sqlite3_step(stmt);
if (res != SQLITE_ROW) {
std::cout << "execution failed: " << sqlite3_errmsg(db) << std::endl;
}
else {
int num_bytes = sqlite3_column_bytes(stmt, 0);
char* blobdata = (char*)sqlite3_column_blob(stmt, 0);
DATA_BLOB DataIn;
DATA_BLOB DataVerify;
BYTE *pbDataInput = (BYTE *)blobdata;
DWORD cbDataInput = num_bytes;
DataIn.pbData = pbDataInput;
DataIn.cbData = cbDataInput;
/////--------------------------------------------------------
if (CryptUnprotectData(
&DataIn,
NULL,
NULL,
NULL,
NULL,
0,
&DataVerify))
{
printf("The decrypted data is: %s\n", DataVerify.pbData);
}
else
{
MyHandleError("Decryption error!");
}
}
}
return 0;
}
int insertCryptExample(sqlite3 *db, const char* data, int len)
{
DATA_BLOB DataIn;
DATA_BLOB DataVerify;
BYTE *pbDataInput = (BYTE *)data;
DWORD cbDataInput = len;
DataIn.pbData = pbDataInput;
DataIn.cbData = cbDataInput;
/////--------------------------------------------------------
if (CryptProtectData(
&DataIn,
NULL,
NULL,
NULL,
NULL,
0,
&DataVerify))
{
printf("The encrypted data is: %d bytes long\n", DataVerify.cbData);
sqlite3_stmt *stmt = nullptr;
std::string str = "Insert into logins VALUES(1, 'test', ? );";
int res = sqlite3_prepare_v2(db, str.c_str(), str.size(), &stmt, nullptr);
std::cout << "res: " << res << std::endl;
res = sqlite3_bind_blob(stmt, 1, (char*)DataVerify.pbData, DataVerify.cbData, SQLITE_STATIC);
//res = sqlite3_exec(db, str.c_str(),nullptr,nullptr,nullptr);
if (res != SQLITE_OK) {
std::cout << "bind failed: " << sqlite3_errmsg(db) << std::endl;
}
else {
res = sqlite3_step(stmt);
if (res != SQLITE_DONE)
std::cout << "execution failed: " << sqlite3_errmsg(db) << std::endl;
}
LocalFree(DataVerify.pbData);
}
else
{
MyHandleError("Decryption error!");
}
//-------------------------------------------------------------------
return 0;
}
int main(int argc, char* argv[]) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql;
const char* data = "Callback function called";
/* Open database */
rc = sqlite3_open("./test.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return(0);
}
else {
fprintf(stderr, "Opened database successfully\n");
}
if (argc >= 2 && argv[1]==std::string("create"))
{
sql = (char*)"CREATE TABLE IF NOT EXISTS logins (id integer PRIMARY KEY, username_value text NOT NULL , password_value BLOB NOT NULL);";
rc = sqlite3_exec(db, sql, nullptr, (void*)data, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
else {
fprintf(stdout, "Operation done successfully\n");
}
std::string str = "testing1234";
insertCryptExample(db,str.c_str(),str.size()+1);
}
/* Open database */
//rc = sqlite3_open("./test.db", &db);
/* Create SQL statement */
sql = (char*)"SELECT password_value FROM logins";
/* Execute SQL statement */
//rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
readCryptExample(db);
sqlite3_close(db);
}
void MyHandleError(const char *s)
{
fprintf(stderr, "An error occurred in running the program. \n");
fprintf(stderr, "%s\n", s);
fprintf(stderr, "Error number %x.\n", GetLastError());
fprintf(stderr, "Program terminating. \n");
exit(1);
} // End of MyHandleError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment