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