13 Security Lab

Base64(b64) 암호화 복호화 함수 본문

Computer Science/Windows Externals

Base64(b64) 암호화 복호화 함수

Maj0r Tom 2014. 10. 28. 14:26

REF. http://www.experts-exchange.com/Programming/Microsoft_Development/A_3216-Fast-Base64-Encode-and-Decode.html


Windows CryptoAPI: CryptBinaryToString and CryptStringToBinary

The Windows CryptoAPI provides a set of general-purpose functions (CryptBinaryToString and CryptStringToBinary) that support base64 encoding and decoding. The following is a pair of functions that wrap that API:

#include 
#pragma comment (lib, "Crypt32.lib")

int ToBase64Crypto( const BYTE* pSrc, int nLenSrc, char* pDst, int nLenDst )
{
   DWORD nLenOut= nLenDst;
   BOOL fRet= CryptBinaryToString(
      (const BYTE*)pSrc, nLenSrc,
      CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF,
      pDst, &nLenOut
   );
   if (!fRet) nLenOut=0;  // failed
   return( nLenOut );
}
int FromBase64Crypto( const BYTE* pSrc, int nLenSrc, char* pDst, int nLenDst )
{
   DWORD nLenOut= nLenDst;
   BOOL fRet= CryptStringToBinary(
      (LPCSTR)pSrc, nLenSrc,
      CRYPT_STRING_BASE64,
      (BYTE*)pDst, &nLenOut,
      NULL,        // pdwSkip (not needed)
      NULL         // pdwFlags (not needed)
   );
   if (!fRet) nLenOut=0;  // failed
   return( nLenOut );
}

Comments