using CryptUnprotectData

i have some encrypted data (char * data)
i wanna decrypt it with cryptunprotectdata function

i found something like this but how can i do this DataIn = char * data
please give me an example
thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
DATA_BLOB DataIn ;
DATA_BLOB DataVerify;
BYTE *pbDataInput = (BYTE *);
DWORD cbDataInput = strlen((char *)pbDataInput) + 1;
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!");
}


BYTE *pbDataInput = (BYTE *)data;
i did that but it gaves me error number 57
Last edited on
i did that but it gaves me error number 57



What, pray tell, is error number 57? A cut & paste of the actual error message would be much more helpful than an error number which means nothing to most of us.
thanks for the answer

this is my HandleError (got from https://msdn.microsoft.com/en-us/library/windows/desktop/aa382377(v=vs.85).aspx)


1
2
3
4
5
6
7
8
void MyHandleError(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 


and its print some string
1
2
3
4
An error occurred in running the program.
Decryption error!
Error number 57.
Program terminating.
Last edited on
I did a Google search on GetLastError codes, and found this.

ERROR_INVALID_PARAMETER

87 (0x57)

The parameter is incorrect.



Without knowing what data is or how it is populated or what the constraints are on DATA_BLOB, I can't tell you why your parameter is invalid.
thanks alot
trying to decrypt from sqllite database
login data of chrome its already encrypted with CryptprotectData
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.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(char *s);

static int callback(void *data, int argc, char **argv, char **azColName) {
	//std::cout << azColName[1] << std::endl;
	DATA_BLOB DataIn ;
	DATA_BLOB DataVerify;
	BYTE *pbDataInput = (BYTE *)azColName[1];
	DWORD cbDataInput = strlen((char *)pbDataInput) + 1;
	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 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");
	}

	/* Create SQL statement */
	sql = "SELECT password_value FROM logins";

	/* Execute SQL statement */
	rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);

	if (rc != SQLITE_OK) {
		fprintf(stderr, "SQL error: %s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	}
	else {
		fprintf(stdout, "Operation done successfully\n");
	}
	sqlite3_close(db);
}


void MyHandleError(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
Last edited on
Sorry I can't provide an example since I haven't done anything with it.

Wondering if the passed data is base64 encoded? If so you need to decode it beforehand.

Is line 15 correct? Why azColName[1] and ColName doesn't sound right either.
Topic archived. No new replies allowed.