寫了一個異或加密工具類
- 呼叫加密字串
- 直接加密檔案
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Encrypt
{
public:
// 建構式
Encrypt();
Encrypt(char *pContent, unsigned char ucKey = 'x');
// 拷貝建構式
Encrypt(const Encrypt& e);
// 多載賦值運算子
Encrypt& operator=(const Encrypt& e);
// 解構式
~Encrypt();
public:
// 更新密匙
void ChangeKey(unsigned char ucNewKey)
{
m_ucKey = ucNewKey;
}
// 獲取密匙
unsigned char GetKey() const
{
return(m_ucKey);
}
public:
// 加密檔案
bool CryptFile(const char* pszFileName);
// 加密字串(在原字串上修改)
bool CryptString(char *pszStr);
// 加密字串, 給定字串長度(在原字串上修改)
bool CryptString(char *pszFile, size_t nSize);
// 分配新的空間并在此基礎上加密 (不變動原字串)
bool CryptString(const char *pszStr, char **ppszNewStr);
private:
// 以wb+方式打開檔案
FILE *OpenTheFile(const char *pszFileName);
// 寫入檔案的封裝
size_t WriteToFile(const char *pszFileName, const char *pszStr);
size_t WriteToFile(const char* pszFileName, const char* pszStr, size_t nSize);
// 讀取檔案
size_t ReadFromFile(const char *pszFileName, char **ppszBuf);
// 獲取檔案大小
size_t GetFileSize(const char* pszFileName);
private:
// 只有加密字串時才會使用該成員, 加密檔案該成員不使用
char* m_pContent;
unsigned char m_ucKey;
};
Encrypt::Encrypt() : m_pContent(nullptr), m_ucKey('x')
{
}
Encrypt::Encrypt(char* pContent, unsigned char ucKey /*= 'x'*/)
{
size_t nSize = 0;
if (nullptr != pContent)
{
nSize = strlen(pContent);
// 獲取密匙
if (ucKey != 'x')
{
m_ucKey = ucKey;
}
// 分配內容并拷貝
m_pContent = new char[nSize + 1];
memset(m_pContent, 0, nSize + 1);
strcpy(m_pContent, pContent);
}
else
{
// 如果傳入字串為nullptr直接賦空
m_pContent = nullptr;
m_ucKey = ucKey;
}
}
Encrypt::Encrypt(const Encrypt& e)
{
size_t nSize = 0;
if (nullptr != e.m_pContent)
{
nSize = strlen(e.m_pContent);
// 獲取秘鑰
m_ucKey = e.m_ucKey;
// 內容復制
m_pContent = new char[nSize + 1];
memset(m_pContent, 0, nSize + 1);
strcpy(m_pContent, e.m_pContent);
}
else
{
m_pContent = nullptr;
m_ucKey = e.m_ucKey;
}
}
Encrypt& Encrypt::operator=(const Encrypt& e)
{
// 原空間是否有內容,有就釋放掉
if (nullptr != m_pContent)
{
delete[] m_pContent;
m_pContent = nullptr;
}
// 檢查目標物件是否為Null, 是就直接復制密匙就回傳, 不是則拷貝內容
if (nullptr != e.m_pContent)
{
// 如果目標物件由內容則拷貝內容
size_t nSize = strlen(e.m_pContent);
m_pContent = new char[nSize + 1];
strcpy(m_pContent, e.m_pContent);
}
m_ucKey = e.m_ucKey;
return(*this);
}
Encrypt::~Encrypt()
{
if (nullptr != m_pContent)
{
delete[] m_pContent;
m_pContent = nullptr;
}
}
bool Encrypt::CryptString(char* pszStr)
{
size_t nSize = 0;
if (nullptr == pszStr)
{
cout << "Encrypt::CryptString引數不能為nullptr" << endl;
return(false);
}
nSize = strlen(pszStr);
// 執行xor加密程序
for (size_t nIdx = 0; nIdx < nSize; ++nIdx)
{
*(pszStr + nIdx) ^= m_ucKey;
}
return(true);
}
bool Encrypt::CryptString(char* pszFile, size_t nSize)
{
if ((nullptr == pszFile) || (nSize == 0))
{
cout << "Encrypt::CryptFile的引數錯誤" << endl;
return(false);
}
for (size_t nIdx = 0; nIdx < nSize; ++nIdx)
{
*(pszFile + nIdx) ^= m_ucKey;
}
return(true);
}
bool Encrypt::CryptString(const char* pszStr, char** ppszNewStr)
{
char* pBuf = nullptr;
size_t nSize = 0;
bool fOk = false;
if ((nullptr == pszStr) || (nullptr == ppszNewStr))
{
cout << "Encrypt::CryptString引數不能為nullptr" << endl;
return(false);
}
nSize = strlen(pszStr);
pBuf = new char[nSize + 1];
memset(pBuf, 0, nSize + 1);
memcpy(pBuf, pszStr, nSize);
// 加密字串
fOk = CryptString(pBuf);
if (false == fOk)
{
cout << "Encrypt::CryptString的多載中CryptString失敗" << endl;
delete[] pBuf;
return(fOk);
}
*ppszNewStr = pBuf;
return(fOk);
}
FILE* Encrypt::OpenTheFile(const char* pszFileName)
{
FILE* fp = nullptr;
if (nullptr == pszFileName)
{
cout << "Encrypt::OpenTheFile 引數不能為nullptr" << endl;
return(nullptr);
}
fp = fopen(pszFileName, "rb+");
if (nullptr == fp)
{
cout << "Encrypt::OpenTheFile中fopen失敗" << endl;
return(nullptr);
}
return(fp);
}
size_t Encrypt::WriteToFile(const char* pszFileName, const char* pszStr)
{
FILE* fp = nullptr;
size_t nSize = 0;
size_t nWriteIn = 0;
if ((nullptr == pszFileName) || (nullptr == pszStr))
{
cout << "Encrypt::WriteToFile 引數不能為nullptr";
return(0);
}
fp = fopen(pszFileName, "wb");
if (nullptr == fp)
{
cout << "Encrypt::WriteToFile中OpenTheFile失敗" << endl;
return(0);
}
nSize = strlen(pszStr);
if (nSize != (nWriteIn = fwrite(pszStr, 1, nSize, fp)))
{
cout << "Encrypt::WriteToFile中fwrite失敗" << endl;
nWriteIn = 0;
}
fclose(fp);
return(nWriteIn);
}
size_t Encrypt::WriteToFile(const char* pszFileName, const char* pszStr, size_t nSize)
{
FILE* fp = nullptr;
size_t nWriteIn = 0;
if ((nullptr == pszFileName) || (nullptr == pszStr) || (nSize == 0))
{
cout << "Encrypt::WriteToFile 引數不能為nullptr";
return(0);
}
fp = fopen(pszFileName, "wb");
if (nullptr == fp)
{
cout << "Encrypt::WriteToFile中OpenTheFile失敗" << endl;
return(0);
}
if (nSize != (nWriteIn = fwrite(pszStr, 1, nSize, fp)))
{
cout << "Encrypt::WriteToFile中fwrite失敗" << endl;
nWriteIn = 0;
}
fclose(fp);
return(nWriteIn);
}
size_t Encrypt::ReadFromFile(const char* pszFileName, char** ppszBuf)
{
size_t nSize = 0;
size_t nSizeRead = 0;
FILE* fp = nullptr;
char* pBuf = nullptr;
if ((nullptr == pszFileName) || (nullptr == ppszBuf))
{
cout << "Encrypt::ReadFromFile引數不能是nullptr" << endl;
return(0);
}
// 獲取檔案大小
nSize = GetFileSize(pszFileName);
// 打開檔案
fp = OpenTheFile(pszFileName);
if (nullptr == fp)
{
cout << "Encrypt::ReadFromFile的OpenTheFile()失敗!" << endl;
return(0);
}
// 分配空間
pBuf = new char[nSize + 1];
memset(pBuf, 0, nSize + 1);
// 讀取檔案內容到分配的堆空間中
if (nSize != (nSizeRead = fread(pBuf, 1, nSize, fp)))
{
delete[] pBuf;
pBuf = nullptr;
cout << "Encrypt::ReadFromFile中fread失敗" << endl;
fclose(fp);
return(0);
}
*ppszBuf = pBuf;
fclose(fp);
return(nSizeRead);
}
size_t Encrypt::GetFileSize(const char* pszFileName)
{
FILE* fp = nullptr;
long lSizeOfFile = 0;
if (nullptr == pszFileName)
{
cout << "Encrypt::GetFileSize引數不能是nullptr" << endl;
return(0);
}
// 打開檔案
fp = OpenTheFile(pszFileName);
if (nullptr == fp)
{
cout << "Encrypt::GetFileSize中OpenTheFile失敗" << endl;
return(0);
}
// 走到末尾
fseek(fp, 0L, SEEK_END);
// 獲取大小
lSizeOfFile = ftell(fp);
fclose(fp);
return((size_t)lSizeOfFile);
}
bool Encrypt::CryptFile(const char* pszFileName)
{
size_t nSize = 0;
char* pBuf = nullptr;
bool fOk = true;
if (nullptr == pszFileName)
{
cout << "Encrypt::CryptFile中引數是nullptr" << endl;
return(false);
}
// 讀取檔案內容
nSize = ReadFromFile(pszFileName, &pBuf);
if (0 == nSize)
{
cout << "Encrypt::CryptFile中ReadFromFile失敗!" << endl;
return(false);
}
// 加密檔案內容
fOk = CryptString(pBuf, nSize);
if (true == fOk)
{
// 保存檔案內容到原檔案
nSize = WriteToFile(pszFileName, pBuf, nSize);
if (0 != nSize)
{
// 成功, 釋放空間
delete[] pBuf;
pBuf = nullptr;
return(true);
}
else
{
cout << "Encrypt::CryptFile中WriteToFile失敗!" << endl;
}
}
else
{
cout << "Encrypt::CryptFile中CryptString失敗!" << endl;
}
delete[] pBuf;
pBuf = nullptr;
return(false);
}
int main()
{
Encrypt e;
system("pause");
return(0);
}
(完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/259527.html
標籤:區塊鏈
上一篇:Clion配置C語言環境
下一篇:Abaqus單位制簡述
