TWinCryptF.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //---------------------------------------------------------------------------
  2. #ifndef TWinCryptFH
  3. #define TWinCryptFH
  4. //---------------------------------------------------------------------------
  5. #include <string>
  6. #include <windows.h>
  7. #include <wincrypt.h>
  8. //---------------------------------------------------------------------------
  9. //https://www.experts-exchange.com/articles/1193/Easy-String-Encryption-Using-CryptoAPI-in-C.html
  10. class TWinCrypt
  11. {
  12. public:
  13. TWinCrypt();
  14. virtual ~TWinCrypt();
  15. private:
  16. HCRYPTPROV FProv;
  17. HCRYPTHASH FHash;
  18. HCRYPTKEY FKey;
  19. bool FOk;
  20. DWORD FLastErr;
  21. std::string FErrMsg;
  22. char *FDefaultKeyRaw;
  23. public:
  24. bool SetKey(LPCSTR szKey, LPCSTR szSalt=NULL);
  25. bool EncryptDecrypt(BYTE* pData, DWORD* dwDataLen, LPCSTR pKey, bool fEncrypt);
  26. std::string EncryptStrToHex(LPCSTR szText, LPCSTR pszKey = 0, LPCSTR pszSalt = 0);
  27. std::string EncodeToHex(BYTE* p, int nLen);
  28. std::string DecryptStrFromHex(LPCSTR szHex, LPCSTR pszKey = 0, LPCSTR pszSalt = 0);
  29. int DecodeFromHex(LPCSTR pSrc, BYTE* pDest, int nBufLen);
  30. };
  31. //---------------------------------------------------------------------------
  32. #endif