Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. #pragma comment(lib, "crypt32.lib")
  2.  
  3. #include <stdio.h>
  4. #include <tchar.h>
  5. #include <windows.h>
  6. #include <wincrypt.h>
  7.  
  8. void MyHandleError(TCHAR* s);
  9. void Wait(TCHAR* s);
  10.  
  11. int _tmain(int argc, _TCHAR* argv[])
  12. {
  13. // Declare and initialize variables.
  14. HCRYPTPROV hProv;
  15. LPTSTR pszName;
  16. DWORD dwType;
  17. DWORD cbName;
  18. DWORD dwIndex = 0;
  19. BYTE* ptr;
  20. ALG_ID aiAlgid;
  21. DWORD dwBits;
  22. DWORD dwNameLen;
  23. CHAR szName[100];
  24. BYTE pbData[1024];
  25. DWORD cbData = 1024;
  26. DWORD dwIncrement = sizeof(DWORD);
  27. DWORD dwFlags = CRYPT_FIRST;
  28. DWORD dwParam = PP_CLIENT_HWND;
  29. CHAR* pszAlgType = NULL;
  30. BOOL fMore = TRUE;
  31. LPTSTR pbProvName;
  32. DWORD cbProvName;
  33. CryptSetProviderExW(
  34. MS_ENHANCED_PROV_W,
  35. PROV_RSA_FULL,
  36. NULL,
  37. CRYPT_MACHINE_DEFAULT
  38. );
  39. //--------------------------------------------------------------
  40. // Print header lines for provider types.
  41. _tprintf(TEXT("Listing Available Provider Types.\n"));
  42. _tprintf(TEXT("Provider type Provider Type Name\n"));
  43. _tprintf(TEXT("_____________ ")
  44. TEXT("_____________________________________\n"));
  45.  
  46. // Loop through enumerating provider types.
  47. dwIndex = 0;
  48. while (CryptEnumProviderTypes(
  49. dwIndex,
  50. NULL,
  51. 0,
  52. &dwType,
  53. NULL,
  54. &cbName))
  55. {
  56. //-----------------------------------------------------------
  57. // cbName is the length of the name of the next provider
  58. // type.
  59.  
  60. // Allocate memory in a buffer to retrieve that name.
  61. if (!(pszName = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, cbName)))
  62. {
  63. MyHandleError(TEXT("ERROR - LocalAlloc failed!"));
  64. }
  65.  
  66. //-----------------------------------------------------------
  67. // Get the provider type name.
  68. if (CryptEnumProviderTypes(
  69. dwIndex++,
  70. NULL,
  71. NULL,
  72. &dwType,
  73. pszName,
  74. &cbName))
  75. {
  76. _tprintf(TEXT(" %4.0d %s\n"),
  77. dwType,
  78. pszName);
  79. }
  80. else
  81. {
  82. MyHandleError(TEXT("ERROR - CryptEnumProviders"));
  83. }
  84.  
  85. LocalFree(pszName);
  86. }
  87.  
  88. //--------------------------------------------------------------
  89. // Print header lines for providers.
  90. _tprintf(TEXT("\n\nListing Available Providers.\n"));
  91. _tprintf(TEXT("Provider type Provider Name\n"));
  92. _tprintf(TEXT("_____________ ")
  93. TEXT("_____________________________________\n"));
  94.  
  95. //---------------------------------------------------------------
  96. // Loop through enumerating providers.
  97. dwIndex = 0;
  98. while (CryptEnumProviders(
  99. dwIndex,
  100. NULL,
  101. 0,
  102. &dwType,
  103. NULL,
  104. &cbName))
  105. {
  106. //-----------------------------------------------------------
  107. // cbName is the length of the name of the next provider.
  108. // Allocate memory in a buffer to retrieve that name.
  109. if (!(pszName = (LPTSTR)LocalAlloc(LMEM_ZEROINIT, cbName)))
  110. {
  111. MyHandleError(TEXT("ERROR - LocalAlloc failed!"));
  112. }
  113.  
  114. //-----------------------------------------------------------
  115. // Get the provider name.
  116. if (CryptEnumProviders(
  117. dwIndex++,
  118. NULL,
  119. 0,
  120. &dwType,
  121. pszName,
  122. &cbName))
  123. {
  124. _tprintf(TEXT(" %4.0d %s\n"),
  125. dwType,
  126. pszName);
  127. }
  128. else
  129. {
  130. MyHandleError(TEXT("ERROR - CryptEnumProviders"));
  131. }
  132.  
  133. LocalFree(pszName);
  134. } // End while loop.
  135.  
  136. //---------------------------------------------------------------
  137. // Get the name of the default CSP specified for the
  138. // PROV_RSA_FULL type for the computer.
  139.  
  140. //---------------------------------------------------------------
  141. // Get the length of the RSA_FULL default provider name.
  142. if (!(CryptGetDefaultProvider(
  143. PROV_RSA_FULL,
  144. NULL,
  145. CRYPT_MACHINE_DEFAULT,
  146. NULL,
  147. &cbProvName)))
  148. {
  149. MyHandleError(TEXT("Error getting the length of the ")
  150. TEXT("default provider name."));
  151. }
  152.  
  153. //---------------------------------------------------------------
  154. // Allocate local memory for the name of the default provider.
  155. if (!(pbProvName = (LPTSTR)LocalAlloc(
  156. LMEM_ZEROINIT,
  157. cbProvName)))
  158. {
  159. MyHandleError(TEXT("Error during memory allocation ")
  160. TEXT("for provider name."));
  161. }
  162. CryptSetProviderExW(
  163. MS_ENHANCED_PROV_W,
  164. PROV_RSA_FULL,
  165. NULL,
  166. CRYPT_MACHINE_DEFAULT
  167. );
  168.  
  169. // Specify the default PROV_RSA_SIG provider. Note that this assumes
  170. // that a CSP with a type of PROV_RSA_SIG and named "Joe's Provider"
  171. // has already been installed.
  172. if (!CryptSetProvider(TEXT("Microsoft Base Cryptographic Provider v1.0"), PROV_RSA_FULL)) {
  173. printf("Error %x during CryptSetProvider!\n", GetLastError());
  174. return 5;
  175. }
  176. // Get handle to the provider that we just made default.
  177. if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
  178. printf("Error %x during CryptAcquireContext!\n", GetLastError());
  179. return 5;
  180. }
  181.  
  182. // Release provider handle.
  183. if (!CryptReleaseContext(hProv, 0)) {
  184. printf("Error %x during CryptReleaseContext!\n", GetLastError());
  185. return 5;
  186. }
  187. //---------------------------------------------------------------
  188. // Get the name of the default PROV_RSA_FULL provider.
  189. if (CryptGetDefaultProvider(
  190. PROV_RSA_FULL,
  191. NULL,
  192. CRYPT_MACHINE_DEFAULT,
  193. pbProvName,
  194. &cbProvName))
  195. {
  196. _tprintf(TEXT("\nThe default provider name is \"%s\"\n"),
  197. pbProvName);
  198. }
  199. else
  200. {
  201. MyHandleError(TEXT("Getting the provider name failed."));
  202. }
  203.  
  204. LocalFree(pbProvName);
  205.  
  206. //-----------------------------------------------------
  207. // Acquire a cryptographic context.
  208. if (!CryptAcquireContext(
  209. &hProv,
  210. NULL,
  211. NULL,
  212. PROV_RSA_FULL,
  213. NULL))
  214. {
  215. MyHandleError(TEXT("Error during CryptAcquireContext!"));
  216. }
  217.  
  218. //------------------------------------------------------
  219. // Enumerate the supported algorithms.
  220.  
  221. //------------------------------------------------------
  222. // Print header for algorithm information table.
  223. _tprintf(TEXT("\nEnumerating the supported ")
  224. TEXT("algorithms\n\n"));
  225. _tprintf(TEXT(" Algid Bits Type ")
  226. TEXT("Name Algorithm\n"));
  227. _tprintf(TEXT(" Length")
  228. TEXT(" Name\n"));
  229. _tprintf(TEXT(" _______________________________________")
  230. TEXT("_________________\n"));
  231. while (fMore)
  232. {
  233. //------------------------------------------------------
  234. // Retrieve information about an algorithm.
  235. if (CryptGetProvParam(
  236. hProv,
  237. PP_ENUMALGS,
  238. pbData,
  239. &cbData,
  240. dwFlags))
  241. {
  242. //-------------------------------------------------------
  243. // Extract algorithm information from the pbData buffer.
  244. dwFlags = 0;
  245. ptr = pbData;
  246. aiAlgid = *(ALG_ID *)ptr;
  247. ptr += sizeof(ALG_ID);
  248. dwBits = *(DWORD *)ptr;
  249. ptr += dwIncrement;
  250. dwNameLen = *(DWORD *)ptr;
  251. ptr += dwIncrement;
  252. strncpy(szName, (char *)ptr, sizeof(szName));
  253.  
  254. //-------------------------------------------------------
  255. // Determine the algorithm type.
  256. switch (GET_ALG_CLASS(aiAlgid))
  257. {
  258. case ALG_CLASS_DATA_ENCRYPT:
  259. pszAlgType = "Encrypt ";
  260. break;
  261.  
  262. case ALG_CLASS_HASH:
  263. pszAlgType = "Hash ";
  264. break;
  265.  
  266. case ALG_CLASS_KEY_EXCHANGE:
  267. pszAlgType = "Exchange ";
  268. break;
  269.  
  270. case ALG_CLASS_SIGNATURE:
  271. pszAlgType = "Signature";
  272. break;
  273.  
  274. default:
  275. pszAlgType = "Unknown ";
  276. break;
  277. }
  278.  
  279. //--------------------------------------------------------
  280. // Print information about the algorithm.
  281. printf(" %8.8xh %-4d %s %-2d %s\n",
  282. aiAlgid,
  283. dwBits,
  284. pszAlgType,
  285. dwNameLen,
  286. szName);
  287. }
  288. else
  289. {
  290. fMore = FALSE;
  291. }
  292. }
  293.  
  294. Wait(TEXT("\nPress Enter to continue."));
  295.  
  296. if (!(CryptReleaseContext(hProv, 0)))
  297. {
  298. MyHandleError(TEXT("Error during CryptReleaseContext."));
  299. }
  300.  
  301. if (GetLastError() == ERROR_NO_MORE_ITEMS)
  302. {
  303. _tprintf(TEXT("\nThe program completed without error.\n"));
  304. }
  305. else
  306. {
  307. MyHandleError(TEXT("Error reading algorithm!"));
  308. }
  309.  
  310. } // End main.
  311.  
  312. //-------------------------------------------------------------------
  313. // This example uses the function MyHandleError, a simple error
  314. // handling function, to print an error message and exit
  315. // the program.
  316. // For most applications, replace this function with one
  317. // that does more extensive error reporting.
  318.  
  319. void MyHandleError(TCHAR *s)
  320. {
  321. _tprintf(TEXT("An error occurred in running the program.\n"));
  322. _tprintf(TEXT("%s\n"), s);
  323. _tprintf(TEXT("Error number %x\n."), GetLastError());
  324. _tprintf(TEXT("Program terminating.\n"));
  325. exit(1);
  326. }
  327.  
  328. void Wait(TCHAR *s)
  329. {
  330. char x;
  331. _tprintf(s);
  332. x = getchar();
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement