· 8 years ago · Mar 11, 2018, 01:46 AM
1// sqtest.cpp : Defines the entry point for the console application.
2//
3
4#include "stdafx.h"
5#include "sqlite3.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <windows.h>
10#include <Wincrypt.h>
11#include<iostream>
12#pragma comment(lib, "Crypt32.lib")
13#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
14void MyHandleError(const char *s);
15
16
17int readCryptExample(sqlite3 *db) {
18
19 //se sqlite3_prepare / sqlite3_step / sqlite3_column_bytes / sqlite3_column_blob.
20 sqlite3_stmt *stmt = nullptr;
21 std::string str = "SELECT password_value FROM logins;";
22 int res = sqlite3_prepare_v2(db, str.c_str(), str.size(), &stmt, nullptr);
23 std::cout << "res: " << res << std::endl;
24 //res = sqlite3_bind_blob(stmt, 1, (char*)DataVerify.pbData, DataVerify.cbData, SQLITE_STATIC);
25 //res = sqlite3_exec(db, str.c_str(),nullptr,nullptr,nullptr);
26 if (res != SQLITE_OK) {
27 std::cout << "bind failed: " << sqlite3_errmsg(db) << std::endl;
28 }
29 else {
30 res = sqlite3_step(stmt);
31 if (res != SQLITE_ROW) {
32 std::cout << "execution failed: " << sqlite3_errmsg(db) << std::endl;
33 }
34 else {
35 int num_bytes = sqlite3_column_bytes(stmt, 0);
36 char* blobdata = (char*)sqlite3_column_blob(stmt, 0);
37 DATA_BLOB DataIn;
38 DATA_BLOB DataVerify;
39 BYTE *pbDataInput = (BYTE *)blobdata;
40 DWORD cbDataInput = num_bytes;
41 DataIn.pbData = pbDataInput;
42 DataIn.cbData = cbDataInput;
43
44 /////--------------------------------------------------------
45 if (CryptUnprotectData(
46 &DataIn,
47 NULL,
48 NULL,
49 NULL,
50 NULL,
51 0,
52 &DataVerify))
53 {
54
55 printf("The decrypted data is: %s\n", DataVerify.pbData);
56
57 }
58
59 else
60 {
61 MyHandleError("Decryption error!");
62 }
63 }
64
65 }
66
67 return 0;
68
69}
70
71int insertCryptExample(sqlite3 *db, const char* data, int len)
72{
73 DATA_BLOB DataIn;
74 DATA_BLOB DataVerify;
75 BYTE *pbDataInput = (BYTE *)data;
76 DWORD cbDataInput = len;
77 DataIn.pbData = pbDataInput;
78 DataIn.cbData = cbDataInput;
79
80 /////--------------------------------------------------------
81 if (CryptProtectData(
82 &DataIn,
83 NULL,
84 NULL,
85 NULL,
86 NULL,
87 0,
88 &DataVerify))
89 {
90
91 printf("The encrypted data is: %d bytes long\n", DataVerify.cbData);
92 sqlite3_stmt *stmt = nullptr;
93 std::string str = "Insert into logins VALUES(1, 'test', ? );";
94 int res = sqlite3_prepare_v2(db, str.c_str(), str.size(), &stmt, nullptr);
95 std::cout << "res: " << res << std::endl;
96 res = sqlite3_bind_blob(stmt, 1, (char*)DataVerify.pbData, DataVerify.cbData, SQLITE_STATIC);
97 //res = sqlite3_exec(db, str.c_str(),nullptr,nullptr,nullptr);
98 if (res != SQLITE_OK) {
99 std::cout << "bind failed: " << sqlite3_errmsg(db) << std::endl;
100 }
101 else {
102 res = sqlite3_step(stmt);
103 if (res != SQLITE_DONE)
104 std::cout << "execution failed: " << sqlite3_errmsg(db) << std::endl;
105 }
106 LocalFree(DataVerify.pbData);
107 }
108
109 else
110 {
111 MyHandleError("Decryption error!");
112 }
113
114 //-------------------------------------------------------------------
115
116 return 0;
117
118}
119
120int main(int argc, char* argv[]) {
121 sqlite3 *db;
122 char *zErrMsg = 0;
123 int rc;
124 char *sql;
125 const char* data = "Callback function called";
126
127 /* Open database */
128 rc = sqlite3_open("./test.db", &db);
129 if (rc) {
130 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
131 return(0);
132 }
133 else {
134 fprintf(stderr, "Opened database successfully\n");
135 }
136
137 if (argc >= 2 && argv[1]==std::string("create"))
138 {
139 sql = (char*)"CREATE TABLE IF NOT EXISTS logins (id integer PRIMARY KEY, username_value text NOT NULL , password_value BLOB NOT NULL);";
140 rc = sqlite3_exec(db, sql, nullptr, (void*)data, &zErrMsg);
141
142 if (rc != SQLITE_OK) {
143 fprintf(stderr, "SQL error: %s\n", zErrMsg);
144 sqlite3_free(zErrMsg);
145 }
146 else {
147 fprintf(stdout, "Operation done successfully\n");
148 }
149 std::string str = "testing1234";
150 insertCryptExample(db,str.c_str(),str.size()+1);
151 }
152
153 /* Open database */
154 //rc = sqlite3_open("./test.db", &db);
155
156
157 /* Create SQL statement */
158 sql = (char*)"SELECT password_value FROM logins";
159
160 /* Execute SQL statement */
161 //rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
162 readCryptExample(db);
163
164 sqlite3_close(db);
165}
166
167void MyHandleError(const char *s)
168{
169 fprintf(stderr, "An error occurred in running the program. \n");
170 fprintf(stderr, "%s\n", s);
171 fprintf(stderr, "Error number %x.\n", GetLastError());
172 fprintf(stderr, "Program terminating. \n");
173 exit(1);
174} // End of MyHandleError