#include "WXBizDataCrypt.h"
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include "openssl/aes.h"
#include "openssl/sha.h"
#include "openssl/evp.h"
#include "rapidjson/document.h"
#define FREE_PTR(ptr) \
if (NULL != (ptr)) {\
free (ptr);\
(ptr) = NULL;\
}
#define DELETE_PTR(ptr) \
if (NULL != (ptr)) {\
delete (ptr);\
(ptr) = NULL;\
}
namespace WxBizDataSecure{
int WXBizDataCrypt :: DecryptData(
const std::string &sEncryptedData,
const std::string &sIv,
std::string &sData)
{
std::string result;
//1.decode base64 for sessionkey , iv and encryptedData
std::string sAesData;
if(0 != DecodeBase64( sEncryptedData, sAesData ))
{
return WXBizDataCrypt_DecodeBase64_Error;
}
std::string sAesKey;
if(0 != DecodeBase64(m_sSessionkey,sAesKey))
{
return WXBizDataCrypt_IllegalAesKey;
}
std::string sAesIv;
if(0 != DecodeBase64(sIv, sAesIv))
{
return WXBizDataCrypt_IllegalIv;
}
//2.decode aes
if(0 != AES_CBCDecrypt(sAesData, sAesKey, sAesIv, &result))
{
return WXBizDataCrypt_IllegalBuffer;
}
// 3. check appid
rapidjson::Document tJsonDoc;
if( tJsonDoc.Parse < 0 > ( result.c_str() ).HasParseError() || tJsonDoc.IsObject() == false )
{
return WXBizDataCrypt_IllegalBuffer;
}
if( tJsonDoc.HasMember( "watermark" ) == false || tJsonDoc["watermark"].IsObject() == false )
{
return WXBizDataCrypt_IllegalBuffer;
}
rapidjson::Value & tWaterMarkDoc = tJsonDoc["watermark"];
if( tWaterMarkDoc.HasMember( "appid" ) == false || tWaterMarkDoc["appid"].IsString() == false )
{
return WXBizDataCrypt_IllegalBuffer;
}
if( tWaterMarkDoc["appid"].GetString() != m_sAppid )
{
return WXBizDataCrypt_IllegalBuffer;
}
sData = result;
return WXBizDataCrypt_OK;
}
int WXBizDataCrypt::AES_CBCDecrypt( const std::string & objSource,
const std::string & objKey, const std::string & sIv,
std::string * poResult )
{
return AES_CBCDecrypt( objSource.data(), objSource.size(),
objKey.data(), objKey.size(),
sIv.c_str(), sIv.size(),
poResult );
}
int WXBizDataCrypt::AES_CBCDecrypt( const char * sSource, const uint32_t iSize,
const char * sKey, uint32_t iKeySize,
const char * sIv, uint32_t iIvSize,
std::string * poResult )
{
if ( !sSource || !sKey || iSize < kAesKeySize || iSize % kAesKeySize != 0 || !poResult)
{
return -1;
}
poResult->clear();
unsigned char * out = (unsigned char*)malloc( iSize );
if(NULL == out)
{
return -1;
}
unsigned char key[ kAesKeySize ] = { 0 };
unsigned char iv[ kAesIVSize ] = {0} ;
memcpy( key, sKey, iKeySize > kAesKeySize ? kAesKeySize : iKeySize );
memcpy(iv, sIv, iIvSize > kAesIVSize ? kAesIVSize : iIvSize );
int iReturnValue = 0;
AES_KEY aesKey;
AES_set_decrypt_key( key, 8 * kAesKeySize, &aesKey );
AES_cbc_encrypt( (unsigned char *)sSource, out, iSize, &aesKey, iv ,AES_DECRYPT);
if( out[iSize-1] > 0 && out[iSize-1] <= kAesKeySize && (iSize - out[iSize-1]) > 0 )
{
poResult->append( (char *)out , iSize - out[iSize-1] );
} else {
iReturnValue = -1;
}
FREE_PTR(out);
return iReturnValue;
}
int WXBizDataCrypt::DecodeBase64(const std::string sSrc, std::string & sTarget)
{
if(0 == sSrc.size() || kMaxBase64Size < sSrc.size())
{
return -1;
}
//计算末尾=号个数
int iEqualNum = 0;
for(int n= sSrc.size() - 1; n>=0; --n)
{
if(sSrc.c_str()[n] == '=')
{
iEqualNum++;
}
else
{
break;
}
}
int iOutBufSize = sSrc.size();
char * pcOutBuf = (char*)malloc( iOutBufSize);
if(NULL == pcOutBuf)
{
return -1;
}
int iRet = 0;
int iTargetSize = 0;
iTargetSize = EVP_DecodeBlock((unsigned char*)pcOutBuf, (const unsigned char*)sSrc.c_str(), sSrc.size());
if(iTargetSize > iEqualNum && iTargetSize < iOutBufSize)
{
sTarget.assign(pcOutBuf, iTargetSize - iEqualNum);
}
else
{
iRet = -1;
}
FREE_PTR(pcOutBuf);
return iRet;
}
}
DTcode7
- 粉丝: 3w+
- 资源: 4986
最新资源
- 前端分析-2023071100789
- 基于纯手工HTML与CSS构建的田东家乡介绍网站设计源码
- 基于鸿蒙操作系统的芯片评估板自检系统源码
- manatee电磁噪声振动计算softwareManatee 1.09 电机电磁振动噪声NVH终结者 带教程,带教程,带教程重要的话说3遍 史上最强后处理软件,甩jmag、Maxwell、flux几
- 基于Python的Django框架开发的subaoApi设计源码
- 基于Owin+融云的LayIM3.0 .NET版本设计源码
- 单向光伏并网逆变器 图一单向光伏并网逆变器整体结构图 图二并网电流与电压曲线图 图三mppt控制最大功率追踪图 图四直流母线电压曲线图
- 基于Python Django框架的旅游网站后端设计源码
- 基于Kotlin语言的Gradle统一依赖管理设计源码
- 基于PyQt5框架的ExcelDiffer设计源码
- COMSOL裂缝地层的THM耦合,离散裂缝模型,随机复杂裂缝,适合地热能研究 增强地热系统,热流固耦合的开采过程
- 基于2020.02.06快照的xadmin JavaScript/Python/HTML/CSS/Shell五语言混合设计源码
- 基于Vue框架的计算机协会招新系统前端微服务设计源码
- BUCK多种控制策略对比 图一BUCK主电路图与控制策略方法 图二采用开环控制波形 图三开环调节过程 图四单电压闭环控制波形 图五单电压调节过程
- 永磁同步电机(PMSM)MATLAB仿真 直接转矩控制 转速外环 转矩跟磁链内环控制 转矩脉动去下图 能够明显减小电机转矩脉动
- 基于JavaScript的UscIoV跨链交互区块链平台设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈