4 Star 13 Fork 23

老张/TdxTradeServer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tts_encrypt.cpp 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
RainX(Jing Xu) 提交于 2017-08-23 11:25 . fix issuef for encrypt and decrypt
#include "tts_encrypt.h"
#include "aes.h"
#include <QDebug>
#include <iostream>
using namespace std;
TTS_Encrypt::TTS_Encrypt(TTS_SettingObject so)
{
// init key and iv
memcpy(key, so.transport_enc_key.toLocal8Bit().constData(), 16);
memcpy(iv, so.transport_enc_iv.toLocal8Bit().constData(), 16);
moo.set_key(key);
moo.set_mode(MODE_CBC);
moo.set_iv(iv);
}
TTS_Encrypt::~TTS_Encrypt()
{
}
string TTS_Encrypt::encryptString(const string plaintext)
{
int plaintextSize = plaintext.size();
int encryptedDataSize = plaintextSize + 32; // alloc more space
string paddedtext;
int needToPadding = 16 - plaintextSize % 16;
int paddedSize = plaintextSize + needToPadding;
// 添加zeropadding
if (needToPadding != 0) {
string padding(needToPadding, '\x00');
paddedtext = plaintext + padding;
} else {
paddedtext = plaintext;
}
unsigned char* encryptedData = new unsigned char[encryptedDataSize];
memset(encryptedData, 0, encryptedDataSize);
int len = moo.Encrypt((unsigned char*) paddedtext.data(), paddedSize, encryptedData);
string estr;
estr.assign((char*)encryptedData, len);
delete[] encryptedData;
return estr;
}
string TTS_Encrypt::decryptString(const string enctext)
{
int encsize = enctext.size();
unsigned char* decdata = new unsigned char[encsize];
int len = moo.Decrypt( (unsigned char*) enctext.data(), encsize, decdata );
string dstr;
dstr.assign((char*)decdata, len);
delete[] decdata;
return dstr;
}
string TTS_Encrypt::toBase64(const string input)
{
QByteArray qb = QByteArray::fromStdString(input);
QByteArray qb64 = qb.toBase64();
string out(qb64.data(), qb64.size());
return out;
}
string TTS_Encrypt::fromBase64(const string input)
{
QByteArray qb64 = QByteArray::fromStdString(input);
QByteArray qb = qb64.fromBase64(qb64);
string out(qb.data(), qb.size());
return out;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/better319/TdxTradeServer.git
git@gitee.com:better319/TdxTradeServer.git
better319
TdxTradeServer
TdxTradeServer
master

搜索帮助