12345678910111213141516171819202122232425262728293031323334353637383940 |
- //---------------------------------------------------------------------------
- #ifndef TWinCryptFH
- #define TWinCryptFH
- //---------------------------------------------------------------------------
- #include <string>
- #include <windows.h>
- #include <wincrypt.h>
- //---------------------------------------------------------------------------
- //https://www.experts-exchange.com/articles/1193/Easy-String-Encryption-Using-CryptoAPI-in-C.html
- class TWinCrypt
- {
- public:
- TWinCrypt();
- virtual ~TWinCrypt();
- private:
- HCRYPTPROV FProv;
- HCRYPTHASH FHash;
- HCRYPTKEY FKey;
- bool FOk;
- DWORD FLastErr;
- std::string FErrMsg;
- char *FDefaultKeyRaw;
- public:
- bool SetKey(LPCSTR szKey, LPCSTR szSalt=NULL);
- bool EncryptDecrypt(BYTE* pData, DWORD* dwDataLen, LPCSTR pKey, bool fEncrypt);
- std::string EncryptStrToHex(LPCSTR szText, LPCSTR pszKey = 0, LPCSTR pszSalt = 0);
- std::string EncodeToHex(BYTE* p, int nLen);
- std::string DecryptStrFromHex(LPCSTR szHex, LPCSTR pszKey = 0, LPCSTR pszSalt = 0);
- int DecodeFromHex(LPCSTR pSrc, BYTE* pDest, int nBufLen);
- };
- //---------------------------------------------------------------------------
- #endif
|