首页 > 学院 > 开发设计 > 正文

windows API CryptBinaryToString

2019-11-10 18:16:57
字体:
来源:转载
供稿:网友
// windowsapi 直接调用的base64编码解码函数// #include <windows.h>  #include <Wincrypt.h>  #PRagma comment(lib,"crypt32")  #include "base64.h"  using namespace gloox;  static unsigned long get_file_size(const char *path)  {      unsigned long filesize = -1;      FILE *fp;      fp = fopen(path, "r");      if(fp == NULL)          return filesize;      fseek(fp, 0L, SEEK_END);      filesize = ftell(fp);      fclose(fp);      return filesize;  }  int _tmain(int argc, _TCHAR* argv[])  {      const char path[] = "silence.png";      ULONG uSize = get_file_size(path);      FILE* fp = fopen(path, "rb");      char* inBuffer = new char[uSize];      ULONG uReaded = fread(inBuffer, 1, uSize, fp);      const BYTE* pbBinary = (const BYTE*)inBuffer;      DWord dwLen;      CryptBinaryToStringA(pbBinary, uSize, CRYPT_STRING_BASE64, NULL, &dwLen);   //把二进制序列转换成字符串    char* pCrypt1 = new char[dwLen];      CryptBinaryToStringA(pbBinary, uSize, CRYPT_STRING_BASE64, pCrypt1, &dwLen);//把二进制序列转换成字符串    fclose(fp);      std::string strIn(pCrypt1);      int nIn = strIn.length();      std::string strOut = Base64::encode64(strIn);      int nOut = strOut.length();      //////////////////////////////////////////////////////////////      std::string strFile = Base64::decode64(strOut);      int nFile = strFile.length();      int nFileSize = strFile.size();      DWORD cbBinary;      DWORD dwSkip;      DWORD dwFlags;      CryptStringToBinaryA(strFile.data(), nFile+1, CRYPT_STRING_BASE64, NULL, &cbBinary, &dwSkip, &dwFlags);      BYTE* outBuffer = new BYTE[cbBinary];      CryptStringToBinaryA(strFile.data(), nFile+1, CRYPT_STRING_BASE64, outBuffer, &cbBinary, &dwSkip, &dwFlags);      FILE* fp2 = fopen("silence2.png", "wb");      ULONG uWrite = fwrite(outBuffer, 1, uSize, fp2);      fclose(fp2);      return 0;  }  
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表