简单的对称加密。。。
#include <iostream>
#include <string.h>
#include <tchar.h>
#include <windows.h>
using namespace std;
/* 简单的对称加密类 */
class symEncryption
{
private:
CHAR *m_pData;
CHAR *m_Key;
CHAR *m_reSult;
public:
/* 设置加密的数据 */
BOOL setEncryData(CHAR *pszData, DWORD dwSizes)
{
if (NULL == pszData || NULL == dwSizes)
{
return(FALSE);
}
if (NULL != m_pData)
{
delete[]m_pData;
}
this->m_pData = new CHAR;
if (NULL == m_pData)
{
return(FALSE);
}
memcpy(m_pData, pszData, dwSizes);
return(TRUE);
}
/* 设置数据钥匙 */
BOOL setKey(CHAR *pszKey, DWORD dwSizes)
{
if (NULL == pszKey || NULL == dwSizes)
{
return(FALSE);
}
if (NULL != m_Key)
{
delete[]m_Key;
}
this->m_Key = new CHAR;
if (NULL == m_Key)
{
return(FALSE);
}
memcpy(m_Key, pszKey, dwSizes);
return(TRUE);
}
/* 加密数据 */
BOOL encryptionData()
{
if (NULL == m_pData || NULL == m_Key)
{
return(FALSE);
}
if (NULL != m_reSult)
{
delete[]m_reSult;
}
/* 生成和密文相同长度的密联流 */
DWORD dwDatalen = strlen(m_pData);
CHAR *pTemp = new CHAR();
/* 生成密联流 */
DWORD dwKeyLne = strlen(m_Key);
for (DWORD sum = 0; sum < dwDatalen; sum++)
{
pTemp = m_Key;
}
pTemp = 0;
/* Xor加密 */
if (NULL != m_reSult)
{
delete[]m_reSult;
}
m_reSult = new CHAR();
for (DWORD sum = 0; sum < dwDatalen; sum++)
{
m_reSult = pTemp ^ m_pData;
}
m_reSult = 0;
delete[]pTemp;
}
/* 获取加密/解密的数据 */
DWORD getResultSize()
{
return(strlen(m_reSult) + 1);
}
/* 获取加密后的数据 */
BOOL getResultData(CHAR *pszData)
{
if (NULL == pszData)
{
return(FALSE);
}
memcpy(pszData, this->m_reSult, strlen(m_reSult) + 1);
}
~symEncryption()
{
if (NULL != this->m_pData)
{
delete[]this->m_pData;
this->m_pData = NULL;
}
if (NULL != this->m_Key)
{
delete[]this->m_Key;
this->m_Key = NULL;
}
if (NULL != this->m_reSult)
{
delete[]this->m_reSult;
this->m_reSult = NULL;
}
}
};
int main(void)
{
/* 【字符串加密测试】 */
/* 加密数据 */
char * str = "hello";
char *key = "fishc";
symEncryption encryData;
/* 设置加密的数据 */
encryData.setEncryData(str, strlen(str) + 1);
/* 设置加密密联 */
encryData.setKey(key, strlen(key) + 1);
/* 设置加密数据 */
encryData.encryptionData();
cout << "数据大小" << encryData.getResultSize() << endl;
cout << "加密前" << str << "密钥"<<key<<endl;
char buffer = { 0 };
/* 得到加密后的数据 */
encryData.getResultData(buffer);
cout << "加密后" << buffer << endl;
/* 解密数据 */
symEncryption decryptData;
/* 设置解密数据密联 */
decryptData.setKey(key, strlen(key) + 1);
/* 设置解密的数据 */
decryptData.setEncryData(buffer, strlen(buffer) + 1);
/* 解密数据 */
decryptData.encryptionData();
/* 得到解密的数据 */
decryptData.getResultData(buffer);
cout << "解密后" << buffer << "密钥" << key <<endl;
system("Pause");
return(EXIT_SUCCESS);
} 学习了,谢谢
页:
[1]