19 if (!CryptAcquireContext(&hProv,
nullptr, MS_DEF_PROV, PROV_RSA_FULL, 0))
22 if (!CryptAcquireContext(&hProv,
nullptr, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
29 if (!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
31 if (GetLastError() == NTE_NO_KEY)
34 if (!CryptGenKey(hProv, AT_SIGNATURE, 0, &hKey))
37 CryptReleaseContext(hProv, 0);
42 CryptDestroyKey(hKey);
48 CryptReleaseContext(hProv, 0);
54 if (!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hKey))
56 if (GetLastError() == NTE_NO_KEY)
59 if (!CryptGenKey(hProv, AT_KEYEXCHANGE, 0, &hKey))
62 CryptReleaseContext(hProv, 0);
67 CryptDestroyKey(hKey);
73 CryptReleaseContext(hProv, 0);
78 CryptReleaseContext(hProv, 0);
85 HCRYPTPROV hProv = NULL;
86 HCRYPTKEY hKey = NULL;
87 HCRYPTHASH hHash = NULL;
89 if (CryptAcquireContext(&hProv,
nullptr,
nullptr, PROV_RSA_FULL, 0))
93 if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
96 DWORD dwLength = strlen(szKey);
97 if (CryptHashData(hHash,
reinterpret_cast<BYTE*
>(szKey), dwLength, 0))
100 if (CryptDeriveKey(hProv,
MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))
103 dwLength = strlen(szPassword);
105 if (!CryptEncrypt(hKey, 0,
true, 0,
nullptr, &dwLength, 0))
111 std::vector<BYTE> buffer(dwLength);
113 memcpy(buffer.data(), szPassword, strlen(szPassword));
114 DWORD dwDataLen = strlen(szPassword);
115 if (CryptEncrypt(hKey, 0,
true, 0, buffer.data(), &dwDataLen, dwLength))
117 memcpy(szEncryptPwd, buffer.data(), dwDataLen);
118 szEncryptPwd[dwDataLen] = 0;
119 SecureZeroMemory(buffer.data(), dwLength);
126 CryptDestroyKey(hKey);
139 CryptDestroyHash(hHash);
147 CryptReleaseContext(hProv, 0);
155 bool bResult =
false;
156 HCRYPTPROV hProv = NULL;
157 HCRYPTKEY hKey = NULL;
158 HCRYPTHASH hHash = NULL;
159 std::array<char, 16> szPasswordTemp{};
161 if (CryptAcquireContext(&hProv,
nullptr,
nullptr, PROV_RSA_FULL, 0))
165 if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
168 DWORD dwLength = strlen(szKey);
169 if (CryptHashData(hHash,
reinterpret_cast<BYTE*
>(szKey), dwLength, 0))
172 if (CryptDeriveKey(hProv,
MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))
175 dwLength = strlen(szEncryptPwd);
177 strcpy_s(szPasswordTemp.data(), szPasswordTemp.size(), szEncryptPwd);
178 if (!CryptDecrypt(hKey, 0,
true, 0,
reinterpret_cast<BYTE*
>(szPasswordTemp.data()), &dwLength))
180 CryptDestroyKey(hKey);
182 strcpy_s(szPassword, 16, szPasswordTemp.data());
183 SecureZeroMemory(szPasswordTemp.data(), szPasswordTemp.size());
196 CryptDestroyHash(hHash);
203 CryptReleaseContext(hProv, 0);