Guest User

RSA utils

a guest
Nov 22nd, 2012
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.17 KB | None | 0 0
  1. //---------------------------------------------------------------------------
  2.  
  3. #include <vcl.h>
  4. #pragma hdrstop
  5.  
  6. #include <tchar.h>
  7. //---------------------------------------------------------------------------
  8.  
  9.  
  10. // INCLUDES
  11. #include "stdio.h"
  12. #include "conio.h"
  13. #include "windows.h"
  14. #include "wincrypt.h"
  15. #include "tchar.h"
  16.  
  17. // FUNCTIONS
  18. int Keys(_TCHAR* strPublicKeyFile, _TCHAR* strPrivateKeyFile);
  19. int Encrypt(_TCHAR* strPublicKeyFile, _TCHAR* strPlainFile, _TCHAR* strEncryptedFile);
  20. int Decrypt(_TCHAR* strPrivateKeyFile, _TCHAR* strEncryptedFile, _TCHAR* strPlainFile);
  21.  
  22. // Main
  23. int _tmain(int argc, _TCHAR* argv[])
  24. {
  25.       int iResult = 0;
  26.  
  27.       if ((argc == 4) && (_tcscmp(argv[1], _T("k")) == 0))
  28.       {
  29.             // Generate a new key pair
  30.             iResult = Keys(argv[2], argv[3]);
  31.       }
  32.       else if ((argc == 5) && (_tcscmp(argv[1], _T("e")) == 0))
  33.       {
  34.             // Encrypt
  35.             iResult = Encrypt(argv[2], argv[3], argv[4]);
  36.       }
  37.       else if ((argc == 5) && (_tcscmp(argv[1], _T("d"))== 0))
  38.       {
  39.             // Decrypt
  40.             iResult = Decrypt(argv[2], argv[3], argv[4]);
  41.       }
  42.       else
  43.       {
  44.             // Show usage
  45.             _tprintf(_T("Usage:\n"));
  46.             _tprintf(_T("   - New key pair: EncryptDecrypt k public_key_file private_key_file\n"));
  47.             _tprintf(_T("   - Encrypt:      EncryptDecrypt e public_key_file plain_file encrypted_file\n"));
  48.             _tprintf(_T("   - Decrypt:      EncryptDecrypt d private_key_file encrypted_file plain_file\n"));
  49.             iResult = 1;
  50.       }
  51.  
  52.       _tprintf(_T("\n<< Press any key to continue >>\n"));
  53.       _getch();
  54.       return iResult;
  55. }
  56. // End of Main
  57.  
  58. // Keys
  59. int Keys(_TCHAR* strPublicKeyFile, _TCHAR* strPrivateKeyFile)
  60. {
  61.       // Variables
  62.       HCRYPTPROV hCryptProv = NULL;
  63.       HCRYPTKEY hKey = NULL;
  64.       DWORD dwPublicKeyLen = 0;
  65.       DWORD dwPrivateKeyLen = 0;
  66.       BYTE* pbPublicKey = NULL;
  67.       BYTE* pbPrivateKey = NULL;
  68.       HANDLE hPublicKeyFile = NULL;
  69.       HANDLE hPrivateKeyFile = NULL;
  70.       DWORD lpNumberOfBytesWritten = 0;
  71.  
  72.       __try
  73.       {
  74.             // Acquire access to key container
  75.             _tprintf(_T("CryptAcquireContext...\n"));
  76.             if (!CryptAcquireContext(&hCryptProv, _T("AlejaCMa.EncryptDecrypt"), NULL, PROV_RSA_FULL, 0))
  77.             {
  78.                   // Error
  79.                   _tprintf(_T("CryptAcquireContext error 0x%x\n"), GetLastError());
  80.  
  81.                   // Try to create a new key container
  82.                   if (!CryptAcquireContext(&hCryptProv, _T("AlejaCMa.EncryptDecrypt"), NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
  83.                   {
  84.                         // Error
  85.                         _tprintf(_T("CryptAcquireContext error 0x%x\n"), GetLastError());
  86.                         return 1;
  87.                   }
  88.             }
  89.  
  90.             // Generate new key pair
  91.             _tprintf(_T("CryptGenKey...\n"));
  92.             if (!CryptGenKey(hCryptProv, AT_KEYEXCHANGE,  CRYPT_ARCHIVABLE, &hKey))
  93.             {
  94.                   // Error
  95.                   _tprintf(_T("CryptGenKey error 0x%x\n"), GetLastError());
  96.                   return 1;
  97.             }
  98.  
  99.             // Get public key size
  100.             _tprintf(_T("CryptExportKey...\n"));
  101.             if (!CryptExportKey(hKey, NULL, PUBLICKEYBLOB, 0, NULL, &dwPublicKeyLen))
  102.             {
  103.                   // Error
  104.                   _tprintf(_T("CryptExportKey error 0x%x\n"), GetLastError());
  105.                   return 1;
  106.             }
  107.  
  108.             // Create a buffer for the public key
  109.             _tprintf(_T("malloc...\n"));
  110.             if (!(pbPublicKey = (BYTE *)malloc(dwPublicKeyLen)))
  111.             {
  112.                   // Error
  113.                   _tprintf(_T("malloc error 0x%x\n"), GetLastError());
  114.                   return 1;
  115.             }
  116.  
  117.             // Get public key
  118.             _tprintf(_T("CryptExportKey...\n"));
  119.             if (!CryptExportKey(hKey, NULL, PUBLICKEYBLOB, 0, pbPublicKey, &dwPublicKeyLen))
  120.             {
  121.                   // Error
  122.                   _tprintf(_T("CryptExportKey error 0x%x\n"), GetLastError());
  123.                   return 1;
  124.             }
  125.  
  126.             // Get private key size
  127.             _tprintf(_T("CryptExportKey...\n"));
  128.             if (!CryptExportKey(hKey, NULL, PRIVATEKEYBLOB, 0, NULL, &dwPrivateKeyLen))
  129.             {
  130.                   // Error
  131.                   _tprintf(_T("CryptExportKey error 0x%x\n"), GetLastError());
  132.                   return 1;
  133.             }
  134.  
  135.             // Create a buffer for the private key
  136.             _tprintf(_T("malloc...\n"));
  137.             if (!(pbPrivateKey = (BYTE *)malloc(dwPrivateKeyLen)))
  138.             {
  139.                   // Error
  140.                   _tprintf(_T("malloc error 0x%x\n"), GetLastError());
  141.                   return 1;
  142.             }
  143.  
  144.             // Get private key
  145.             _tprintf(_T("CryptExportKey...\n"));
  146.             if (!CryptExportKey(hKey, NULL, PRIVATEKEYBLOB, 0, pbPrivateKey, &dwPrivateKeyLen))
  147.             {
  148.                   // Error
  149.                   _tprintf(_T("CryptExportKey error 0x%x\n"), GetLastError());
  150.                   return 1;
  151.             }
  152.  
  153.             // Create a file to save the public key
  154.             _tprintf(_T("CreateFile...\n"));
  155.             if ((hPublicKeyFile = CreateFile(
  156.             strPublicKeyFile,
  157.             GENERIC_WRITE,
  158.             0,
  159.             NULL,
  160.             CREATE_ALWAYS,
  161.             FILE_ATTRIBUTE_NORMAL,
  162.             NULL
  163.             )) == INVALID_HANDLE_VALUE)
  164.             {
  165.                   // Error
  166.                   _tprintf(_T("CreateFile error 0x%x\n"), GetLastError());
  167.                   return 1;
  168.             }
  169.  
  170.             // Write the public key to the file
  171.             _tprintf(_T("WriteFile...\n"));
  172.             if (!WriteFile(
  173.                   hPublicKeyFile,
  174.                   (LPCVOID)pbPublicKey,
  175.                   dwPublicKeyLen,
  176.                   &lpNumberOfBytesWritten,
  177.                   NULL
  178.             ))
  179.             {
  180.                   // Error
  181.                   _tprintf(_T("WriteFile error 0x%x\n"), GetLastError());
  182.                   return 1;
  183.             }
  184.  
  185.             // Create a file to save the private key
  186.             _tprintf(_T("CreateFile...\n"));
  187.             if ((hPrivateKeyFile = CreateFile(
  188.             strPrivateKeyFile,
  189.             GENERIC_WRITE,
  190.             0,
  191.             NULL,
  192.             CREATE_ALWAYS,
  193.             FILE_ATTRIBUTE_NORMAL,
  194.             NULL
  195.             )) == INVALID_HANDLE_VALUE)
  196.             {
  197.                   // Error
  198.                   _tprintf(_T("CreateFile error 0x%x\n"), GetLastError());
  199.                   return 1;
  200.             }
  201.  
  202.             // Write the private key to the file
  203.             _tprintf(_T("WriteFile...\n"));
  204.             if (!WriteFile(
  205.                   hPrivateKeyFile,
  206.                   (LPCVOID)pbPrivateKey,
  207.                   dwPrivateKeyLen,
  208.                   &lpNumberOfBytesWritten,
  209.                   NULL
  210.             ))
  211.             {
  212.                   // Error
  213.                   _tprintf(_T("WriteFile error 0x%x\n"), GetLastError());
  214.                   return 1;
  215.             }
  216.  
  217.             return 0;
  218.       }
  219.       __finally
  220.       {
  221.             // Clean up
  222.             if (!pbPublicKey) {
  223.                   _tprintf(_T("free...\n"));
  224.                   free(pbPublicKey);
  225.             }
  226.             if (!pbPrivateKey) {
  227.                   _tprintf(_T("free...\n"));
  228.                   free(pbPrivateKey);
  229.             }
  230.             if (hPublicKeyFile) {
  231.                   _tprintf(_T("CloseHandle...\n"));
  232.                   CloseHandle(hPublicKeyFile);
  233.             }
  234.             if (hPrivateKeyFile) {
  235.                   _tprintf(_T("CloseHandle...\n"));
  236.                   CloseHandle(hPrivateKeyFile);
  237.             }
  238.             if (hKey) {
  239.                   _tprintf(_T("CryptDestroyKey...\n"));
  240.                   CryptDestroyKey(hKey);
  241.             }
  242.             if (hCryptProv) {
  243.                   _tprintf(_T("CryptReleaseContext...\n"));
  244.                   CryptReleaseContext(hCryptProv, 0);
  245.             }
  246.       }
  247. }
  248. // End of Keys
  249.  
  250. // Encrypt
  251. int Encrypt(_TCHAR* strPublicKeyFile, _TCHAR* strPlainFile, _TCHAR* strEncryptedFile)
  252. {
  253.       // Variables
  254.       HCRYPTPROV hCryptProv = NULL;
  255.       HCRYPTKEY hKey = NULL;
  256.       DWORD dwPublicKeyLen = 0;
  257.       DWORD dwDataLen = 0;
  258.       DWORD dwEncryptedLen = 0;
  259.       BYTE* pbPublicKey = NULL;
  260.       BYTE* pbData = NULL;
  261.       HANDLE hPublicKeyFile = NULL;
  262.       HANDLE hEncryptedFile = NULL;
  263.       HANDLE hPlainFile = NULL;
  264.       DWORD lpNumberOfBytesWritten = 0;
  265.  
  266.       __try
  267.       {
  268.             // Acquire access to key container
  269.             _tprintf(_T("CryptAcquireContext...\n"));
  270.             if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
  271.             {
  272.                   // Error
  273.                   _tprintf(_T("CryptAcquireContext error 0x%x\n"), GetLastError());
  274.                   return 1;
  275.             }
  276.  
  277.             // Open public key file
  278.             _tprintf(_T("CreateFile...\n"));
  279.             if ((hPublicKeyFile = CreateFile(
  280.             strPublicKeyFile,
  281.             GENERIC_READ,
  282.             FILE_SHARE_READ,
  283.             NULL,
  284.             OPEN_EXISTING,
  285.             FILE_FLAG_SEQUENTIAL_SCAN,
  286.             NULL
  287.             )) == INVALID_HANDLE_VALUE)
  288.             {
  289.                   // Error
  290.                   _tprintf(_T("CreateFile error 0x%x\n"), GetLastError());
  291.                   return 1;
  292.             }
  293.  
  294.             // Get file size
  295.             _tprintf(_T("GetFileSize...\n"));
  296.             if ((dwPublicKeyLen = GetFileSize(hPublicKeyFile, NULL)) == INVALID_FILE_SIZE)
  297.             {
  298.                   // Error
  299.                   _tprintf(_T("GetFileSize error 0x%x\n"), GetLastError());
  300.                   return 1;
  301.             }
  302.  
  303.             // Create a buffer for the public key
  304.             _tprintf(_T("malloc...\n"));
  305.             if (!(pbPublicKey = (BYTE *)malloc(dwPublicKeyLen)))
  306.             {
  307.                   // Error
  308.                   _tprintf(_T("malloc error 0x%x\n"), GetLastError());
  309.                   return 1;
  310.             }
  311.  
  312.             // Read public key
  313.             _tprintf(_T("ReadFile...\n"));
  314.             if (!ReadFile(hPublicKeyFile, pbPublicKey, dwPublicKeyLen, &dwPublicKeyLen, NULL))
  315.             {
  316.                   // Error
  317.                   _tprintf(_T("ReadFile error 0x%x\n"), GetLastError());
  318.                   return 1;
  319.             }
  320.  
  321.             // Import public key
  322.             _tprintf(_T("CryptImportKey...\n"));
  323.             if (!CryptImportKey(hCryptProv, pbPublicKey, dwPublicKeyLen, 0, 0, &hKey))
  324.             {
  325.                   // Error
  326.                   _tprintf(_T("CryptImportKey error 0x%x\n"), GetLastError());
  327.                   return 1;
  328.             }
  329.  
  330.             // Open plain text file
  331.             _tprintf(_T("CreateFile...\n"));
  332.             if ((hPlainFile = CreateFile(
  333.             strPlainFile,
  334.             GENERIC_READ,
  335.             FILE_SHARE_READ,
  336.             NULL,
  337.             OPEN_EXISTING,
  338.             FILE_FLAG_SEQUENTIAL_SCAN,
  339.             NULL
  340.             )) == INVALID_HANDLE_VALUE)
  341.             {
  342.                   // Error
  343.                   _tprintf(_T("CreateFile error 0x%x\n"), GetLastError());
  344.                   return 1;
  345.             }
  346.  
  347.             // Get file size
  348.             _tprintf(_T("GetFileSize...\n"));
  349.             if ((dwDataLen = GetFileSize(hPlainFile, NULL)) == INVALID_FILE_SIZE)
  350.             {
  351.                   // Error
  352.                   _tprintf(_T("GetFileSize error 0x%x\n"), GetLastError());
  353.                   return 1;
  354.             }
  355.  
  356.             // Create a buffer for the plain text
  357.             _tprintf(_T("malloc...\n"));
  358.             if (!(pbData = (BYTE *)malloc(dwDataLen)))
  359.             {
  360.                   // Error
  361.                   _tprintf(_T("malloc error 0x%x\n"), GetLastError());
  362.                   return 1;
  363.             }
  364.  
  365.             // Read plain text
  366.             _tprintf(_T("ReadFile...\n"));
  367.             if (!ReadFile(hPlainFile, pbData, dwDataLen, &dwDataLen, NULL))
  368.             {
  369.                   // Error
  370.                   _tprintf(_T("ReadFile error 0x%x\n"), GetLastError());
  371.                   return 1;
  372.             }
  373.  
  374.             // Get lenght for encrypted data
  375.             if (!CryptEncrypt(hKey, NULL, TRUE, 0, NULL, &dwEncryptedLen, 0))
  376.             {
  377.                   // Error
  378.                   _tprintf(_T("CryptEncrypt error 0x%x\n"), GetLastError());
  379.                   return 1;
  380.             }
  381.  
  382.             // Create a buffer for encrypted data
  383.             _tprintf(_T("malloc...\n"));
  384.             if (!(pbData = (BYTE *)realloc(pbData, dwEncryptedLen)))
  385.             {
  386.                   // Error
  387.                   _tprintf(_T("malloc error 0x%x\n"), GetLastError());
  388.                   return 1;
  389.             }
  390.  
  391.             // Encrypt data
  392.             if (!CryptEncrypt(hKey, NULL, TRUE, 0, pbData, &dwDataLen, dwEncryptedLen))
  393.             {
  394.                   // Error
  395.                   _tprintf(_T("CryptEncrypt error 0x%x\n"), GetLastError());
  396.                   return 1;
  397.             }
  398.  
  399.             // Create a file to save the encrypted data
  400.             _tprintf(_T("CreateFile...\n"));
  401.             if ((hEncryptedFile = CreateFile(
  402.             strEncryptedFile,
  403.             GENERIC_WRITE,
  404.             0,
  405.             NULL,
  406.             CREATE_ALWAYS,
  407.             FILE_ATTRIBUTE_NORMAL,
  408.             NULL
  409.             )) == INVALID_HANDLE_VALUE)
  410.             {
  411.                   // Error
  412.                   _tprintf(_T("CreateFile error 0x%x\n"), GetLastError());
  413.                   return 1;
  414.             }
  415.  
  416.             // Write the public key to the file
  417.             _tprintf(_T("WriteFile...\n"));
  418.             if (!WriteFile(
  419.                   hEncryptedFile,
  420.                   (LPCVOID)pbData,
  421.                   dwDataLen,
  422.                   &lpNumberOfBytesWritten,
  423.                   NULL
  424.             ))
  425.             {
  426.                   // Error
  427.                   _tprintf(_T("WriteFile error 0x%x\n"), GetLastError());
  428.                   return 1;
  429.             }
  430.  
  431.             return 0;
  432.       }
  433.       __finally
  434.       {
  435.             // Clean up
  436.             if (!pbPublicKey) {
  437.                   _tprintf(_T("free...\n"));
  438.                   free(pbPublicKey);
  439.             }
  440.             if (!pbData) {
  441.                   _tprintf(_T("free...\n"));
  442.                   free(pbData);
  443.             }
  444.             if (hPublicKeyFile) {
  445.                   _tprintf(_T("CloseHandle...\n"));
  446.                   CloseHandle(hPublicKeyFile);
  447.             }
  448.             if (hPlainFile) {
  449.                   _tprintf(_T("CloseHandle...\n"));
  450.                   CloseHandle(hPlainFile);
  451.             }
  452.             if (hEncryptedFile) {
  453.                   _tprintf(_T("CloseHandle...\n"));
  454.                   CloseHandle(hEncryptedFile);
  455.             }
  456.             if (hKey) {
  457.                   _tprintf(_T("CryptDestroyKey...\n"));
  458.                   CryptDestroyKey(hKey);
  459.             }
  460.             if (hCryptProv) {
  461.                   _tprintf(_T("CryptReleaseContext...\n"));
  462.                   CryptReleaseContext(hCryptProv, 0);
  463.             }
  464.       }
  465. }
  466. // End of Encrypt
  467.  
  468. // Decrypt
  469. int Decrypt(_TCHAR* strPrivateKeyFile, _TCHAR* strEncryptedFile, _TCHAR* strPlainFile)
  470. {
  471.       // Variables
  472.       HCRYPTPROV hCryptProv = NULL;
  473.       HCRYPTKEY hKey = NULL;
  474.       DWORD dwPrivateKeyLen = 0;
  475.       DWORD dwDataLen = 0;
  476.       BYTE* pbPrivateKey = NULL;
  477.       BYTE* pbData = NULL;
  478.       HANDLE hPrivateKeyFile = NULL;
  479.       HANDLE hEncryptedFile = NULL;
  480.       HANDLE hPlainFile = NULL;
  481.       DWORD lpNumberOfBytesWritten = 0;
  482.  
  483.       __try
  484.       {
  485.             // Acquire access to key container
  486.             _tprintf(_T("CryptAcquireContext...\n"));
  487.             if (!CryptAcquireContext(&hCryptProv, _T("AlejaCMa.EncryptDecrypt"), NULL, PROV_RSA_FULL, 0))
  488.             {
  489.                   // Error
  490.                   _tprintf(_T("CryptAcquireContext error 0x%x\n"), GetLastError());
  491.  
  492.                   // Try to create a new key container
  493.                   if (!CryptAcquireContext(&hCryptProv, _T("AlejaCMa.EncryptDecrypt"), NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
  494.                   {
  495.                         // Error
  496.                         _tprintf(_T("CryptAcquireContext error 0x%x\n"), GetLastError());
  497.                         return 1;
  498.                   }
  499.             }
  500.  
  501.             // Open private key file
  502.             _tprintf(_T("CreateFile...\n"));
  503.             if ((hPrivateKeyFile = CreateFile(
  504.             strPrivateKeyFile,
  505.             GENERIC_READ,
  506.             FILE_SHARE_READ,
  507.             NULL,
  508.             OPEN_EXISTING,
  509.             FILE_FLAG_SEQUENTIAL_SCAN,
  510.             NULL
  511.             )) == INVALID_HANDLE_VALUE)
  512.             {
  513.                   // Error
  514.                   _tprintf(_T("CreateFile error 0x%x\n"), GetLastError());
  515.                   return 1;
  516.             }
  517.  
  518.             // Get file size
  519.             _tprintf(_T("GetFileSize...\n"));
  520.             if ((dwPrivateKeyLen = GetFileSize(hPrivateKeyFile, NULL)) == INVALID_FILE_SIZE)
  521.             {
  522.                   // Error
  523.                   _tprintf(_T("GetFileSize error 0x%x\n"), GetLastError());
  524.                   return 1;
  525.             }
  526.  
  527.             // Create a buffer for the private key
  528.             _tprintf(_T("malloc...\n"));
  529.             if (!(pbPrivateKey = (BYTE *)malloc(dwPrivateKeyLen)))
  530.             {
  531.                   // Error
  532.                   _tprintf(_T("malloc error 0x%x\n"), GetLastError());
  533.                   return 1;
  534.             }
  535.  
  536.             // Read private key
  537.             _tprintf(_T("ReadFile...\n"));
  538.             if (!ReadFile(hPrivateKeyFile, pbPrivateKey, dwPrivateKeyLen, &dwPrivateKeyLen, NULL))
  539.             {
  540.                   // Error
  541.                   _tprintf(_T("ReadFile error 0x%x\n"), GetLastError());
  542.                   return 1;
  543.             }
  544.  
  545.             // Import private key
  546.             _tprintf(_T("CryptImportKey...\n"));
  547.             if (!CryptImportKey(hCryptProv, pbPrivateKey, dwPrivateKeyLen, 0, 0, &hKey))
  548.             {
  549.                   // Error
  550.                   _tprintf(_T("CryptImportKey error 0x%x\n"), GetLastError());
  551.                   return 1;
  552.             }
  553.  
  554.             // Open encrypted file
  555.             _tprintf(_T("CreateFile...\n"));
  556.             if ((hEncryptedFile = CreateFile(
  557.             strEncryptedFile,
  558.             GENERIC_READ,
  559.             FILE_SHARE_READ,
  560.             NULL,
  561.             OPEN_EXISTING,
  562.             FILE_FLAG_SEQUENTIAL_SCAN,
  563.             NULL
  564.             )) == INVALID_HANDLE_VALUE)
  565.             {
  566.                   // Error
  567.                   _tprintf(_T("CreateFile error 0x%x\n"), GetLastError());
  568.                   return 1;
  569.             }
  570.  
  571.             // Get file size
  572.             _tprintf(_T("GetFileSize...\n"));
  573.             if ((dwDataLen = GetFileSize(hEncryptedFile, NULL)) == INVALID_FILE_SIZE)
  574.             {
  575.                   // Error
  576.                   _tprintf(_T("GetFileSize error 0x%x\n"), GetLastError());
  577.                   return 1;
  578.             }
  579.  
  580.             // Create a buffer for the encrypted data
  581.             _tprintf(_T("malloc...\n"));
  582.             if (!(pbData = (BYTE *)malloc(dwDataLen)))
  583.             {
  584.                   // Error
  585.                   _tprintf(_T("malloc error 0x%x\n"), GetLastError());
  586.                   return 1;
  587.             }
  588.  
  589.             // Read encrypted data
  590.             _tprintf(_T("ReadFile...\n"));
  591.             if (!ReadFile(hEncryptedFile, pbData, dwDataLen, &dwDataLen, NULL))
  592.             {
  593.                   // Error
  594.                   _tprintf(_T("ReadFile error 0x%x\n"), GetLastError());
  595.                   return 1;
  596.             }
  597.  
  598.             // Get lenght for plain text
  599.             if (!CryptDecrypt(hKey, NULL, TRUE, 0, pbData, &dwDataLen))
  600.             {
  601.                   // Error
  602.                   _tprintf(_T("CryptDecrypt error 0x%x\n"), GetLastError());
  603.                   return 1;
  604.             }
  605.  
  606.             // Create a file to save the plain text
  607.             _tprintf(_T("CreateFile...\n"));
  608.             if ((hPlainFile = CreateFile(
  609.             strPlainFile,
  610.             GENERIC_WRITE,
  611.             0,
  612.             NULL,
  613.             CREATE_ALWAYS,
  614.             FILE_ATTRIBUTE_NORMAL,
  615.             NULL
  616.             )) == INVALID_HANDLE_VALUE)
  617.             {
  618.                   // Error
  619.                   _tprintf(_T("CreateFile error 0x%x\n"), GetLastError());
  620.                   return 1;
  621.             }
  622.  
  623.             // Write the plain text the file
  624.             _tprintf(_T("WriteFile...\n"));
  625.             if (!WriteFile(
  626.                   hPlainFile,
  627.                   (LPCVOID)pbData,
  628.                   dwDataLen,
  629.                   &lpNumberOfBytesWritten,
  630.                   NULL
  631.             ))
  632.             {
  633.                   // Error
  634.                   _tprintf(_T("WriteFile error 0x%x\n"), GetLastError());
  635.                   return 1;
  636.             }
  637.  
  638.             return 0;
  639.       }
  640.       __finally
  641.       {
  642.             // Clean up
  643.             if (!pbPrivateKey) {
  644.                   _tprintf(_T("free...\n"));
  645.                   free(pbPrivateKey);
  646.             }
  647.             if (!pbData) {
  648.                   _tprintf(_T("free...\n"));
  649.                   free(pbData);
  650.             }
  651.             if (hPrivateKeyFile) {
  652.                   _tprintf(_T("CloseHandle...\n"));
  653.                   CloseHandle(hPrivateKeyFile);
  654.             }
  655.             if (hEncryptedFile) {
  656.                   _tprintf(_T("CloseHandle...\n"));
  657.                   CloseHandle(hEncryptedFile);
  658.             }
  659.             if (hPlainFile) {
  660.                   _tprintf(_T("CloseHandle...\n"));
  661.                   CloseHandle(hPlainFile);
  662.             }
  663.             if (hKey) {
  664.                   _tprintf(_T("CryptDestroyKey...\n"));
  665.                   CryptDestroyKey(hKey);
  666.             }
  667.             if (hCryptProv) {
  668.                   _tprintf(_T("CryptReleaseContext...\n"));
  669.                   CryptReleaseContext(hCryptProv, 0);
  670.             }
  671.       }
  672. }
  673. // End of Decrypt
  674.  
  675. //#pragma argsused
  676. //int _tmain(int argc, _TCHAR* argv[])
  677. //{
  678. //  Keys(_T("C:\public.key"), _T("C:\private.key"));
  679. //  while (1)
  680. //      ;
  681. //}
  682.  
  683. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment