zoukankan      html  css  js  c++  java
  • 写加密文件内容

    这个类写得很烂。 chBuffer 使用前要 memset 初始化一下,否则会报:

     Run-Time Check Failure #2 - Stack around the variable '' was corrupted.

    class EncryFile

    {

    public:

      void CryptString(unsigned char chKey); //加、解密字符串

      bool LoadStringFromFile(const CString &strFileName, unsigned char chKey); //读取解密

      bool SaveStringToFile(const CString &strFileName, unsigned char chKey);//保存加密

    public:

      CString m_strFileName; //文件名

      CString m_strFileBuffer; //文件缓冲区

      DWORD m_dwFileLength; //文件长度

      TCHAR chBuffer[MAX_SIZE]; 文件缓冲区//#define MAX_SIZE 65535

    };

    //.cpp

    const unsigned char DEFAULT = 123;

    void CryptString(unsgined char chKey)

    {

      DWORD dwLen = m_strFileBuffer.Getlength();

      for(DWORD i =0; i<dwLen; i++)

      {

        chBuffer[i] = chBuffer[i]^chKey; //异或,j加密

      }

      m_strFileBuffer = chBuffer;

    }

    bool LoadStringFromFile(const CString &strFileName, unsigned char chKey)

    {

      DWORD dwRead = 0;

      HANDLE hFile = CreateFile(strFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

      if (INVALID_HANDLE_VALUE != hFile)

      {

        //打开成功

        m_dwFileLength = GetFileSize(hFile, NULL);

        if ( !ReadFile(hFile, chBuffer, m_dwFileLength, &dwRead, NULL) )

        {

          //error

          return false;

        }

        CloseHandle(hFile); //关闭文件句柄

      }

      else

      {

        //error : open fail.

        return false;

      }

      m_strFileBuffer = chBuffer; //

      CryptString(chKey); //解密

      //测试读出来的文件内容

      hFile = CreateFile("xxx.ini", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

      BOOL bRet = (::WriteFile(hFile, m_strFileBuffer, m_dwFileLength, &dwRead, NULL));

      if (bRet)

      {

        CloseHandle(hFile);

        return true;

      }

    return false;

    }

    bool SaveStringToFile(const Cstring &strFileName, unsigned char chKey)

    {

      DWORD dwRead = 0;

      HANDLE hFile = CreateFile(strFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

      if (INVALID_HANDLE_VALUE != hFile)

      {

        m_dwFileLength = GetFileSize(hFile, NULL);

        if (!ReadFile(hFile, chBuffer, m_dwFileLength, &dwRead, NULL) )

        {

          //error read

          return false;

        }

        Closehandle(hFile);

      }

      else

      {

        //open error

        return false;

      }

      m_strFileBuffer = chBuffer;

      CryptString(chKey); //加密

      hFile = CreateFile(strFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

      if(INVALID_HANDLE_VALUE != hFile)

      {

        DWROD dwWrite = 0;

        if (!WriteFile(hFile, m_strFileBuffer, m_dwFileLenght, &dwWrite, NULL))

         {//erro write return  false;}

        Closehanle(hFile);

      }

      else

      {//error open return false}

      CryptString(chKey); //对字符串解密是为了不改变原字符串

    return true;

    }

  • 相关阅读:
    Ambient Intelligence in Networked Society
    如何定位问题及如何区分前后端BUG
    接口测试-结合Postman的使用
    Selenium的PageObject设计模式(2)
    Selenium的PageObject设计模式(1)
    Git+Pycharm 分布式版本管理
    php中不用内置函数实现字符串转整形
    mysql中case使用
    快速排序
    魔术方法__sleep(),__wakeup()
  • 原文地址:https://www.cnblogs.com/sylar-liang/p/5490615.html
Copyright © 2011-2022 走看看