Rise
The Vieneo Province
Loading...
Searching...
No Matches
Cryptography.cpp
Go to the documentation of this file.
1#include "Cryptography.h"
2
3#include <WinSock2.h>
4#include <WS2tcpip.h>
5#include <Windows.h>
6
7#include <cstdlib>
8#include <vector>
9#include <array>
10
11#include <wincrypt.h>
12
14{
15 // Ensure that the default cryptographic client is set up.
16 HCRYPTPROV hProv;
17 HCRYPTKEY hKey;
18 // Attempt to acquire a handle to the default key container.
19 if (!CryptAcquireContext(&hProv, nullptr, MS_DEF_PROV, PROV_RSA_FULL, 0))
20 {
21 // Some sort of error occured, create default key container.
22 if (!CryptAcquireContext(&hProv, nullptr, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
23 {
24 // Error creating key container!
25 return false;
26 }
27 }
28 // Attempt to get handle to signature key.
29 if (!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
30 {
31 if (GetLastError() == NTE_NO_KEY)
32 {
33 // Create signature key pair.
34 if (!CryptGenKey(hProv, AT_SIGNATURE, 0, &hKey))
35 {
36 // Error during CryptGenKey!
37 CryptReleaseContext(hProv, 0);
38 return false;
39 }
40 else
41 {
42 CryptDestroyKey(hKey);
43 }
44 }
45 else
46 {
47 // Error during CryptGetUserKey!
48 CryptReleaseContext(hProv, 0);
49 return false;
50 }
51 }
52
53 // Attempt to get handle to exchange key.
54 if (!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hKey))
55 {
56 if (GetLastError() == NTE_NO_KEY)
57 {
58 // Create key exchange key pair.
59 if (!CryptGenKey(hProv, AT_KEYEXCHANGE, 0, &hKey))
60 {
61 // Error during CryptGenKey!
62 CryptReleaseContext(hProv, 0);
63 return false;
64 }
65 else
66 {
67 CryptDestroyKey(hKey);
68 }
69 }
70 else
71 {
72 // Error during CryptGetUserKey!
73 CryptReleaseContext(hProv, 0);
74 return false;
75 }
76 }
77
78 CryptReleaseContext(hProv, 0);
79 return true;
80}
81
82bool Cryptography::EncryptString(char* szPassword, char* szEncryptPwd, char* szKey)
83{
84 bool bResult = false;
85 HCRYPTPROV hProv = NULL;
86 HCRYPTKEY hKey = NULL;
87 HCRYPTHASH hHash = NULL;
88 // Get handle to user default provider.
89 if (CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL, 0))
90 {
91 bResult = true;
92 // Create hash object.
93 if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
94 {
95 // Hash password string.
96 DWORD dwLength = strlen(szKey);
97 if (CryptHashData(hHash, reinterpret_cast<BYTE*>(szKey), dwLength, 0))
98 {
99 // Create block cipher session key based on hash of the password.
100 if (CryptDeriveKey(hProv, MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))
101 {
102 // Determine number of bytes to encrypt at a time.
103 dwLength = strlen(szPassword);
104 // Determine required buffer size for encrypted data
105 if (!CryptEncrypt(hKey, 0, true, 0, nullptr, &dwLength, 0))
106 {
107 bResult = false;
108 }
109 else
110 {
111 std::vector<BYTE> buffer(dwLength);
112 // Copy plaintext into buffer
113 memcpy(buffer.data(), szPassword, strlen(szPassword));
114 DWORD dwDataLen = strlen(szPassword);
115 if (CryptEncrypt(hKey, 0, true, 0, buffer.data(), &dwDataLen, dwLength))
116 {
117 memcpy(szEncryptPwd, buffer.data(), dwDataLen);
118 szEncryptPwd[dwDataLen] = 0;
119 SecureZeroMemory(buffer.data(), dwLength);
120 }
121 else
122 {
123 bResult = false;
124 }
125 }
126 CryptDestroyKey(hKey); // Release provider handle.
127 }
128 else
129 {
130 // Error during CryptDeriveKey!
131 bResult = false;
132 }
133 }
134 else
135 {
136 // Error during CryptHashData!
137 bResult = false;
138 }
139 CryptDestroyHash(hHash);
140 // Destroy session key.
141 }
142 else
143 {
144 // Error during CryptCreateHash!
145 bResult = false;
146 }
147 CryptReleaseContext(hProv, 0);
148 }
149
150 return bResult;
151}
152
153bool Cryptography::DecryptString(char* szEncryptPwd, char* szPassword, char* szKey)
154{
155 bool bResult = false;
156 HCRYPTPROV hProv = NULL;
157 HCRYPTKEY hKey = NULL;
158 HCRYPTHASH hHash = NULL;
159 std::array<char, 16> szPasswordTemp{};
160 // Get handle to user default provider.
161 if (CryptAcquireContext(&hProv, nullptr, nullptr, PROV_RSA_FULL, 0))
162 {
163 bResult = true;
164 // Create hash object.
165 if (CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
166 {
167 // Hash password string.
168 DWORD dwLength = strlen(szKey);
169 if (CryptHashData(hHash, reinterpret_cast<BYTE*>(szKey), dwLength, 0))
170 {
171 // Create block cipher session key based on hash of the password.
172 if (CryptDeriveKey(hProv, MY_ENCRYPT, hHash, CRYPT_EXPORTABLE, &hKey))
173 {
174 // we know the encrypted password and the length
175 dwLength = strlen(szEncryptPwd);
176 // copy encrypted password to temporary buffer
177 strcpy_s(szPasswordTemp.data(), szPasswordTemp.size(), szEncryptPwd);
178 if (!CryptDecrypt(hKey, 0, true, 0, reinterpret_cast<BYTE*>(szPasswordTemp.data()), &dwLength))
179 bResult = false;
180 CryptDestroyKey(hKey); // Release provider handle.
181 // copy decrypted password to outparameter
182 strcpy_s(szPassword, 16, szPasswordTemp.data());
183 SecureZeroMemory(szPasswordTemp.data(), szPasswordTemp.size());
184 }
185 else
186 {
187 // Error during CryptDeriveKey!
188 bResult = false;
189 }
190 }
191 else
192 {
193 // Error during CryptHashData!
194 bResult = false;
195 }
196 CryptDestroyHash(hHash); // Destroy session key.
197 }
198 else
199 {
200 // Error during CryptCreateHash!
201 bResult = false;
202 }
203 CryptReleaseContext(hProv, 0);
204 }
205
206 return bResult;
207}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
#define MY_ENCRYPT
Definition Cryptography.h:5
static bool DecryptString(char *szEncryptPwd, char *szPassword, char *szKey)
static bool EncryptString(char *szPassword, char *szEncryptPwd, char *szKey)
static bool SetupCryptoClient()