Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. bool CNtlFileSerializer::SaveFile(char* pszFullPathFileName, bool bCrypt /* = FALSE */, char* szCryptPassword /* = NULL */)
  2. {
  3.     if(!pszFullPathFileName)
  4.         return false;
  5.  
  6.     DWORD dwFileAttribute = ::GetFileAttributes(pszFullPathFileName);
  7.     if (INVALID_FILE_ATTRIBUTES != dwFileAttribute)
  8.     {
  9.         if (dwFileAttribute & FILE_ATTRIBUTE_READONLY)
  10.         {
  11.             dwFileAttribute &= ~FILE_ATTRIBUTE_READONLY;
  12.  
  13.             ::SetFileAttributes(pszFullPathFileName, dwFileAttribute);
  14.         }
  15.     }
  16.  
  17.     FILE *pFile = NULL;
  18.  
  19.     if (0 != fopen_s(&pFile, pszFullPathFileName, "wb"))
  20.     {
  21.         return false;
  22.     }
  23.  
  24.     if(bCrypt)
  25.     {
  26.         int nBufSize = GetDataSize() + 256;
  27.         char* buf = new char[nBufSize];    
  28.         ZeroMemory(buf, nBufSize);
  29.  
  30.         CNtlCipher cipher;
  31.         cipher.SetKey(DES_CIPHER, szCryptPassword, (int)strlen(szCryptPassword));
  32.         int nEncSize = cipher.Encrypt(m_pBuffer, GetDataSize(), buf, nBufSize);
  33.         if(nEncSize <= 0)
  34.         {
  35.             delete[] buf;
  36.             fclose(pFile);
  37.             return false;
  38.         }
  39.  
  40.         fwrite(buf, nEncSize, 1, pFile);
  41.         delete[] buf;
  42.     }
  43.     else
  44.     {
  45.         fwrite(GetData(), GetDataSize(), 1, pFile);
  46.     }
  47.  
  48.  
  49.  
  50.  
  51.     fclose(pFile);
  52.  
  53.     return true;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement