Unit1.cpp
上传用户:zhihui0713
上传日期:2022-08-07
资源大小:1k
文件大小:2k
源码类别:

加密解密

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Unit1.h"
  5. #include "tripledes.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma resource "*.dfm"
  9. TForm1 *Form1;
  10. //---------------------------------------------------------------------------
  11. __fastcall TForm1::TForm1(TComponent* Owner)
  12.     : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. void __fastcall TForm1::Button1Click(TObject *Sender)
  17. {
  18.     PFILEENCRYPT FileEncrypt;
  19.     PFILEDECRYPT FileDecrypt;
  20.     PENCRYPTBUFFER EncryptBuffer;
  21.     PDECRYPTBUFFER DecryptBuffer;
  22.     PFREEBUFFER FreeBuffer;
  23.     HANDLE hLib = ::LoadLibrary("tripledes.dll");
  24.     if(!hLib)
  25.      return;
  26.      FileEncrypt = (PFILEENCRYPT) ::GetProcAddress(hLib,"FileEncrypt");
  27.      FileDecrypt = (PFILEDECRYPT) ::GetProcAddress(hLib,"FileDecrypt");
  28.      EncryptBuffer = (PENCRYPTBUFFER) ::GetProcAddress(hLib,"EncryptBuffer");
  29.      DecryptBuffer = (PDECRYPTBUFFER) ::GetProcAddress(hLib,"DecryptBuffer");
  30.      FreeBuffer = (PFREEBUFFER) ::GetProcAddress(hLib,"FreeBuffer");
  31.      if(!FileEncrypt || !FileDecrypt ||
  32.         !EncryptBuffer || !DecryptBuffer ||
  33.         !FreeBuffer)
  34.         {
  35.          ::FreeLibrary(hLib);
  36.          return;
  37.         }
  38.       // deal with files
  39.       FileEncrypt("C:\test.txt","123456");
  40.       FileDecrypt("C:\test.txt","123456");
  41.       //deal with buffers
  42. DWORD dwEncSize = Edit1->Text.Length();
  43. BYTE* encripted = EncryptBuffer(Edit1->Text.c_str(),dwEncSize,"123456",&dwEncSize);
  44. if(encripted)
  45.       {
  46.       BYTE* decripted = DecryptBuffer(encripted,dwEncSize,"123456");
  47.           if(decripted)
  48.           {
  49.              //::MessageBox(NULL,(char*)decripted,NULL,MB_OK);
  50.                     ///Edit2->Text = (char*)decripted;
  51.                     Edit2->Text = (char*)encripted;
  52.             FreeBuffer(decripted);
  53.            } // if(decripted)
  54.         FreeBuffer(encripted);
  55.        } // if(encripted)
  56.     ::FreeLibrary(hLib);
  57. }
  58. //---------------------------------------------------------------------------
  59.