Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool CNtlFileSerializer::SaveFile(char* pszFullPathFileName, bool bCrypt /* = FALSE */, char* szCryptPassword /* = NULL */)
- {
- if(!pszFullPathFileName)
- return false;
- DWORD dwFileAttribute = ::GetFileAttributes(pszFullPathFileName);
- if (INVALID_FILE_ATTRIBUTES != dwFileAttribute)
- {
- if (dwFileAttribute & FILE_ATTRIBUTE_READONLY)
- {
- dwFileAttribute &= ~FILE_ATTRIBUTE_READONLY;
- ::SetFileAttributes(pszFullPathFileName, dwFileAttribute);
- }
- }
- FILE *pFile = NULL;
- if (0 != fopen_s(&pFile, pszFullPathFileName, "wb"))
- {
- return false;
- }
- if(bCrypt)
- {
- int nBufSize = GetDataSize() + 256;
- char* buf = new char[nBufSize];
- ZeroMemory(buf, nBufSize);
- CNtlCipher cipher;
- cipher.SetKey(DES_CIPHER, szCryptPassword, (int)strlen(szCryptPassword));
- int nEncSize = cipher.Encrypt(m_pBuffer, GetDataSize(), buf, nBufSize);
- if(nEncSize <= 0)
- {
- delete[] buf;
- fclose(pFile);
- return false;
- }
- fwrite(buf, nEncSize, 1, pFile);
- delete[] buf;
- }
- else
- {
- fwrite(GetData(), GetDataSize(), 1, pFile);
- }
- fclose(pFile);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement