17 if (!CryptAcquireContext(&hProv,
nullptr, MS_DEF_PROV, PROV_RSA_FULL, 0))
20 if (!CryptAcquireContext(&hProv,
nullptr, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
27 if (!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
29 if (GetLastError() == NTE_NO_KEY)
32 if (!CryptGenKey(hProv, AT_SIGNATURE, 0, &hKey))
35 CryptReleaseContext(hProv, 0);
40 CryptDestroyKey(hKey);
46 CryptReleaseContext(hProv, 0);
52 if (!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hKey))
54 if (GetLastError() == NTE_NO_KEY)
57 if (!CryptGenKey(hProv, AT_KEYEXCHANGE, 0, &hKey))
60 CryptReleaseContext(hProv, 0);
65 CryptDestroyKey(hKey);
71 CryptReleaseContext(hProv, 0);
76 CryptReleaseContext(hProv, 0);
83 HCRYPTPROV hProv = NULL;
84 HCRYPTKEY hKey = NULL;
85 HCRYPTHASH hHash = NULL;
87 if (CryptAcquireContext(&hProv,
nullptr,
nullptr, PROV_RSA_FULL, 0))
90 if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
93 DWORD dwLength = strlen(szKey);
94 if (CryptHashData(hHash, reinterpret_cast<BYTE *>(szKey), dwLength, 0))
97 if (CryptDeriveKey(hProv,
MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))
100 dwLength = strlen(szPassword);
102 BYTE *pbBuffer =
static_cast<BYTE *
>(malloc(dwLength));
103 if (pbBuffer !=
nullptr)
105 memcpy(pbBuffer, szPassword, dwLength);
107 if (CryptEncrypt(hKey, 0,
true, 0, pbBuffer, &dwLength, dwLength))
110 memcpy(szEncryptPwd, pbBuffer, dwLength);
111 szEncryptPwd[dwLength] = 0;
124 CryptDestroyKey(hKey);
137 CryptDestroyHash(hHash);
145 CryptReleaseContext(hProv, 0);
154 HCRYPTPROV hProv = NULL;
155 HCRYPTKEY hKey = NULL;
156 HCRYPTHASH hHash = NULL;
157 char szPasswordTemp[16];
158 szPasswordTemp[0] = 0;
160 if (CryptAcquireContext(&hProv,
nullptr,
nullptr, PROV_RSA_FULL, 0))
163 if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
166 DWORD dwLength = strlen(szKey);
167 if (CryptHashData(hHash, reinterpret_cast<BYTE *>(szKey), dwLength, 0))
170 if (CryptDeriveKey(hProv,
MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))
173 dwLength = strlen(szEncryptPwd);
175 strcpy_s(szPasswordTemp, 16, szEncryptPwd);
176 if (!CryptDecrypt(hKey, 0,
true, 0, reinterpret_cast<BYTE *>(szPasswordTemp), &dwLength))
178 CryptDestroyKey(hKey);
180 strcpy_s(szPassword, 16, szPasswordTemp);
193 CryptDestroyHash(hHash);
200 CryptReleaseContext(hProv, 0);
static bool EncryptString(char *szPassword, char *szEncryptPwd, char *szKey)
static bool DecryptString(char *szEncryptPwd, char *szPassword, char *szKey)
static bool SetupCryptoClient()