111
This commit is contained in:
parent
be21e720ec
commit
4a5d575687
|
|
@ -0,0 +1,234 @@
|
|||
|
||||
#include "pch.h"
|
||||
#include "BASE64.h"
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
|
||||
using namespace LenheartBase;
|
||||
|
||||
// 编解码转换表
|
||||
unsigned char CBASE64::s_encTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
unsigned char CBASE64::s_decTable[] = {
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3E,0xFF,0xFF,0xFF,0x3F,
|
||||
0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
|
||||
0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
|
||||
0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
|
||||
0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF
|
||||
};
|
||||
|
||||
// 执行BASE64编码操作
|
||||
std::string CBASE64::encode(const std::string& str)
|
||||
{
|
||||
std::string strEncode;
|
||||
strEncode.resize((str.size() / 3 + 1) * 4);
|
||||
strEncode.resize(__encode((unsigned char*)strEncode.data(), (const unsigned char*)str.data(), str.size()));
|
||||
return strEncode;
|
||||
}
|
||||
|
||||
// 执行BASE64解码操作
|
||||
std::string CBASE64::decode(const std::string& str)
|
||||
{
|
||||
std::string strDecode;
|
||||
strDecode.resize(str.size());
|
||||
strDecode.resize(__decode((unsigned char*)strDecode.data(), (const unsigned char*)str.data(), str.size()));
|
||||
return strDecode;
|
||||
}
|
||||
|
||||
// 分组编码
|
||||
int CBASE64::__encode(unsigned char* pDest, const unsigned char* pSrc, size_t nSrcLen)
|
||||
{
|
||||
unsigned char* dst = pDest;
|
||||
const unsigned char* src = pSrc;
|
||||
size_t len = nSrcLen;
|
||||
|
||||
unsigned char* odst = dst;
|
||||
unsigned long l = 0, m = 0, n = 0;
|
||||
|
||||
// 循环处理分组
|
||||
size_t off = 0;
|
||||
for (; off + 3 <= len; off += 3)
|
||||
{
|
||||
l = *src++;
|
||||
m = *src++;
|
||||
n = *src++;
|
||||
|
||||
*dst++ = s_encTable[(l >> 2) & 0x3F];
|
||||
*dst++ = s_encTable[((l << 4) & 0x30) | ((m >> 4) & 0x0F)];
|
||||
*dst++ = s_encTable[((m << 2) & 0x3C) | ((n >> 6) & 0x03)];
|
||||
*dst++ = s_encTable[n & 0x3F];
|
||||
}
|
||||
|
||||
// 处理余下的2个字节
|
||||
if (off + 2 <= len)
|
||||
{
|
||||
l = *src++;
|
||||
m = *src++;
|
||||
|
||||
*dst++ = s_encTable[(l >> 2) & 0x3F];
|
||||
*dst++ = s_encTable[((l << 4) & 0x30) | ((m >> 4) & 0x0F)];
|
||||
*dst++ = s_encTable[((m << 2) & 0x3C)];
|
||||
*dst++ = '=';
|
||||
}
|
||||
|
||||
// 处理余下的1个字节
|
||||
else if (off + 1 <= len)
|
||||
{
|
||||
l = *src++;
|
||||
|
||||
*dst++ = s_encTable[(l >> 2) & 0x3F];
|
||||
*dst++ = s_encTable[((l << 4) & 0x30)];
|
||||
*dst++ = '=';
|
||||
*dst++ = '=';
|
||||
}
|
||||
|
||||
return (int)(dst - odst);
|
||||
}
|
||||
|
||||
// 分组解码
|
||||
int CBASE64::__decode(unsigned char* pDest, const unsigned char* pSrc, size_t nSrcLen)
|
||||
{
|
||||
unsigned char* dst = pDest;
|
||||
const unsigned char* src = pSrc;
|
||||
size_t len = nSrcLen;
|
||||
|
||||
unsigned char* odst = dst;
|
||||
unsigned long l = 0, m = 0, n = 0, o = 0;
|
||||
|
||||
// 循环处理分组
|
||||
size_t off = 0;
|
||||
for (; off + 4 <= len; off += 4)
|
||||
{
|
||||
if ((src[0] > 0x7F) || (src[1] > 0x7F) || (src[2] > 0x7F) || (src[3] > 0x7F)) return (int)(dst - odst);
|
||||
if ((src[0] == '=') || (src[1] == '=') || (src[2] == '=') || (src[3] == '=')) break;
|
||||
|
||||
l = s_decTable[*src++];
|
||||
m = s_decTable[*src++];
|
||||
n = s_decTable[*src++];
|
||||
o = s_decTable[*src++];
|
||||
|
||||
*dst++ = (unsigned char)(((l << 2) & 0xFC) | ((m >> 4) & 0x03));
|
||||
*dst++ = (unsigned char)(((m << 4) & 0xF0) | ((n >> 2) & 0x0F));
|
||||
*dst++ = (unsigned char)(((n << 6) & 0xC0) | (o & 0x3F));
|
||||
}
|
||||
|
||||
// 处理余下的3个字节
|
||||
if (off + 3 <= len)
|
||||
{
|
||||
if ((src[0] != '=') && (src[1] != '='))
|
||||
{
|
||||
l = s_decTable[*src++];
|
||||
m = s_decTable[*src++];
|
||||
|
||||
*dst++ = (unsigned char)(((l << 2) & 0xFC) | ((m >> 4) & 0x03));
|
||||
}
|
||||
|
||||
if ((src[2] != '='))
|
||||
{
|
||||
n = s_decTable[*src++];
|
||||
|
||||
*dst++ = (unsigned char)(((m << 4) & 0xF0) | ((n >> 2) & 0x0F));
|
||||
}
|
||||
}
|
||||
|
||||
// 处理余下的两个字节
|
||||
else if (off + 2 <= len)
|
||||
{
|
||||
if ((src[0] != '=') && (src[1] != '='))
|
||||
{
|
||||
l = s_decTable[*src++];
|
||||
m = s_decTable[*src++];
|
||||
|
||||
*dst++ = (unsigned char)(((l << 2) & 0xFC) | ((m >> 4) & 0x03));
|
||||
}
|
||||
}
|
||||
|
||||
return (int)(dst - odst);
|
||||
}
|
||||
|
||||
|
||||
std::string CBASE64::RsaPriDecrypt(const std::string& cipher_text, const std::string& pri_key)
|
||||
{
|
||||
std::string decrypt_text;
|
||||
RSA* rsa = RSA_new();
|
||||
BIO* keybio;
|
||||
keybio = BIO_new_mem_buf((unsigned char*)pri_key.c_str(), -1);
|
||||
|
||||
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
|
||||
if (rsa == nullptr) {
|
||||
unsigned long err = ERR_get_error(); //获取错误号
|
||||
char err_msg[1024] = { 0 };
|
||||
ERR_error_string(err, err_msg); // 格式:error:errId:库:函数:原因
|
||||
return std::string();
|
||||
}
|
||||
|
||||
// 获取RSA单次处理的最大长度
|
||||
int key_len = RSA_size(rsa);
|
||||
char* sub_text = new char[key_len + 1];
|
||||
memset(sub_text, 0, key_len + 1);
|
||||
int ret = 0;
|
||||
std::string sub_str;
|
||||
unsigned int pos = 0;
|
||||
// 对密文进行分段解密
|
||||
while (pos < cipher_text.length() - 1) {
|
||||
sub_str = cipher_text.substr(pos, key_len);
|
||||
memset(sub_text, 0, key_len + 1);
|
||||
ret = RSA_private_decrypt(sub_str.length(), (const unsigned char*)sub_str.c_str(), (unsigned char*)sub_text, rsa, 1);
|
||||
if (ret >= 0) {
|
||||
decrypt_text.append(std::string(sub_text, ret));
|
||||
//printf("pos:%d, Length: %d ,sub: %s\n", pos, cipher_text.length(),sub_text);
|
||||
pos += key_len;
|
||||
}
|
||||
}
|
||||
// 释放内存
|
||||
delete[] sub_text;
|
||||
BIO_free_all(keybio);
|
||||
RSA_free(rsa);
|
||||
|
||||
return decrypt_text;
|
||||
}
|
||||
|
||||
std::string CBASE64::RsaPriEncrypt(const std::string& clear_text, const std::string& pri_key)
|
||||
{
|
||||
std::string encrypt_text;
|
||||
BIO* keybio = BIO_new_mem_buf((unsigned char*)pri_key.c_str(), -1);
|
||||
RSA* rsa = RSA_new();
|
||||
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
|
||||
if (!rsa)
|
||||
{
|
||||
BIO_free_all(keybio);
|
||||
return std::string("");
|
||||
}
|
||||
|
||||
// 获取RSA单次可以处理的数据块的最大长度
|
||||
int key_len = RSA_size(rsa);
|
||||
int block_len = key_len - 11; // 因为填充方式为RSA_PKCS1_PADDING, 所以要在key_len基础上减去11
|
||||
|
||||
// 申请内存:存贮加密后的密文数据
|
||||
char* sub_text = new char[key_len + 1];
|
||||
memset(sub_text, 0, key_len + 1);
|
||||
int ret = 0;
|
||||
unsigned int pos = 0;
|
||||
std::string sub_str;
|
||||
// 对数据进行分段加密(返回值是加密后数据的长度)
|
||||
while (pos < clear_text.length()) {
|
||||
sub_str = clear_text.substr(pos, block_len);
|
||||
memset(sub_text, 0, key_len + 1);
|
||||
ret = RSA_private_encrypt(sub_str.length(), (const unsigned char*)sub_str.c_str(), (unsigned char*)sub_text, rsa, RSA_PKCS1_PADDING);
|
||||
if (ret >= 0) {
|
||||
encrypt_text.append(std::string(sub_text, ret));
|
||||
}
|
||||
pos += block_len;
|
||||
}
|
||||
|
||||
// 释放内存
|
||||
delete sub_text;
|
||||
BIO_free_all(keybio);
|
||||
RSA_free(rsa);
|
||||
|
||||
return encrypt_text;
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
#ifndef __DAKUANG_BASE64_H__
|
||||
#define __DAKUANG_BASE64_H__
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace LenheartBase
|
||||
{
|
||||
|
||||
class CBASE64
|
||||
{
|
||||
public:
|
||||
|
||||
// 执行BASE64编码操作
|
||||
static std::string encode(const std::string& str);
|
||||
|
||||
// 执行BASE64解码操作
|
||||
static std::string decode(const std::string& str);
|
||||
|
||||
// 执行RSA私匙解密操作
|
||||
static std::string RsaPriDecrypt(const std::string& cipher_text, const std::string& pri_key);
|
||||
// 执行RSA私匙加密操作
|
||||
static std::string RsaPriEncrypt(const std::string& clear_text, const std::string& pri_key);
|
||||
private:
|
||||
|
||||
// 分组编码
|
||||
static int __encode(unsigned char* pDest, const unsigned char* pSrc, size_t nSrcLen);
|
||||
|
||||
// 分组解码
|
||||
static int __decode(unsigned char* pDest, const unsigned char* pSrc, size_t nSrcLen);
|
||||
|
||||
private:
|
||||
|
||||
// 编解码转换表
|
||||
static unsigned char s_encTable[];
|
||||
static unsigned char s_decTable[];
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -55,11 +55,11 @@
|
|||
#if defined LOCALHOSTS_SWITCH
|
||||
|
||||
#else
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#include "framework.h"
|
||||
#include "RSAC.h"
|
||||
#include "BASE64.h"
|
||||
#include "httplib.h"
|
||||
#define CPPHTTPLIB_OPENSSL_SUPPORT
|
||||
#endif
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -172,6 +172,7 @@
|
|||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="BASE64.h" />
|
||||
<ClInclude Include="Dargon.h" />
|
||||
<ClInclude Include="DNFTOOL.h" />
|
||||
<ClInclude Include="framework.h" />
|
||||
|
|
@ -184,6 +185,7 @@
|
|||
<ClInclude Include="STL.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="BASE64.cpp" />
|
||||
<ClCompile Include="dllmain.cpp" />
|
||||
<ClCompile Include="DNFTOOL.cpp" />
|
||||
<ClCompile Include="hook.cpp" />
|
||||
|
|
|
|||
|
|
@ -45,6 +45,9 @@
|
|||
<ClInclude Include="Dargon.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="BASE64.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="dllmain.cpp">
|
||||
|
|
@ -74,5 +77,8 @@
|
|||
<ClCompile Include="STL.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="BASE64.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Loading…
Reference in New Issue