I tryied to crypt decrypted infromation by using CryptProtectData on C++. But in result i getted different results. My code:
FILE *one =fopen("firstencrypt", "wb+");
FILE *two = fopen("secondencrypt", "wb+");
DATA_BLOB encryptdata , decryptdata ,secondendata;
string data = "Hello world.\0";
decryptdata.cbData = (DWORD)data.length();
decryptdata.pbData = (BYTE*)data.c_str();
CryptProtectData(&decryptdata, NULL, NULL, NULL, NULL, 0, &encryptdata); // first protect data
fwrite(encryptdata.pbData, (int)encryptdata.cbData, 1, one); fclose(one); // writing result in file
CryptUnprotectData(&encryptdata, NULL, NULL, NULL, NULL, 0, &decryptdata); // unprotect data
cout << decryptdata.pbData << endl; // log
CryptProtectData(&decryptdata, NULL, NULL, NULL, NULL, 0, &secondendata); // second protect data
fwrite(secondendata.pbData, (int)secondendata.cbData, 1, two); fclose(two); // writing result in file`
In console i getted:
Hello world.LMEM
And i notice with each launch of the program i getting different result. Why?
Every time you run CryptProtectData the result is (almost surely) different, because the encryption key is derived from a masterkey together with randomly generated "salts" of 16 bytes, that are all stored in the blob. This randomisation is actually good practice and it's no problem for decryption.