C++实现简单异或加密算法
2014-12-19 13:05
309 查看
#pragma once #include <iostream> class NXCORE_API XorCrypt { public: static std::string Xor(__in const std::string& input, __in const std::string& key); static bool Encrypt(__in const std::string& input, __in const std::string& output, __in const std::string& key); static bool Decrypt(__in const std::string& input, __out std::string& output, __in const std::string& key); static bool EncryptFile(__in const std::string& input, __in const std::string& output, __in const std::string& key); static bool DecryptFile(__in const std::string& input, __in const std::string& output, __in const std::string& key); };
#include "stdafx.h"
#include "XorCrypt.h"
std::string XorCrypt::Xor(__in const std::string& input,
__in const std::string& key)
{
std::string output;
output.resize(input.size(), '\0');
for (unsigned int i = 0, j = 0; i < input.size(); i++)
output[i] = input[i] ^ key[j == key.size() ? 0 : j++];
return output;
}
bool XorCrypt::Encrypt(__in const std::string& input,
__in const std::string& output, __in const std::string& key)
{
std::string encrypt = Xor(input, key);
HANDLE hFile = ::CreateFileA(output.c_str(), GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return false;
unsigned long written = 0;
::WriteFile(hFile, encrypt.c_str(), encrypt.size(), &written, NULL);
::FlushFileBuffers(hFile);
::CloseHandle(hFile);
return true;
}
bool XorCrypt::Decrypt(__in const std::string& input,
__out std::string& output, __in const std::string& key)
{
HANDLE hFile = ::CreateFileA(input.c_str(), GENERIC_READ,
0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return false;
unsigned long fileSize = ::GetFileSize(hFile, NULL);
char* buff = new char[fileSize + 1];
memset(buff, 0, fileSize + 1);
unsigned long readSize = 0;
::ReadFile(hFile, buff, fileSize, &readSize, NULL);
output.resize(fileSize);
memcpy(const_cast<char*>(output.c_str()), buff, fileSize);
output = Xor(output, key);
::CloseHandle(hFile);
delete buff;
return true;
}
bool XorCrypt::EncryptFile(__in const std::string& input,
__in const std::string& output, __in const std::string& key)
{
HANDLE hFile = ::CreateFileA(input.c_str(), GENERIC_READ,
0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return false;
unsigned long fileSize = ::GetFileSize(hFile, NULL);
char* buff = new char[fileSize + 1];
memset(buff, 0, fileSize + 1);
unsigned long readSize = 0;
::ReadFile(hFile, buff, fileSize, &readSize, NULL);
::CloseHandle(hFile);
std::string data;
data.resize(fileSize);
memcpy(const_cast<char*>(data.c_str()), buff, fileSize);
delete buff;
return Encrypt(data, output, key);
}
bool XorCrypt::DecryptFile(__in const std::string& input,
__in const std::string& output, __in const std::string& key)
{
std::string data;
if (!Decrypt(input, data, key))
return false;
HANDLE hFile = ::CreateFileA(output.c_str(), GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return false;
unsigned long written = 0;
::WriteFile(hFile, data.c_str(), data.size(), &written, NULL);
::FlushFileBuffers(hFile);
::CloseHandle(hFile);
return true;
}
相关文章推荐
- JAVA 可逆加密算法的简单实现 - XOR异或运算
- c++ 简单的实现椭圆曲线加密算法
- 用C++实现简单的文件I/O操作
- 用C++实现简单的文件I/O操作
- 用异或的性质实现简单加密解密
- c++ stl list 迭代器简单实现
- 使用VBA实现简单的EXCEL异或操作
- C++ : 用proxy Class实现简单的二维数组。
- Ping的简单实现 CODE: C /C++
- 用C++实现简单的文件I/O操作
- 初学者看过来:简单谈谈 C/C++ 递归的思想,实现,以及和循环的关系。
- 用C++实现简单的文件I/O操作(ifstream,ofstream)
- 标准纯C++实现简单的词法分析器(三)
- 标准纯C++实现简单的词法分析器(二)
- RC4加密算法在C++中的实现
- 使用异或进行简单的密码加密(JAVA实现)
- 标准纯C++实现简单的词法分析器(二)
- c++ 写的linux下简单的实现 get post 请求
- 实例学MFC(3)--超简单C++实现网络下载器
- 堆栈的简单C++实现