GrpcPrint/PrintS/utils/CryptHelper.cpp
2024-03-19 17:45:12 +08:00

119 lines
3.1 KiB
C++

#include "CryptHelper.h"
#include <openssl/aes.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/des.h>
#include <queue>
#include <sstream>
CryptHelper::CryptHelper()
{
}
CryptHelper::~CryptHelper()
{
}
int CryptHelper::Base64DecodeWrap(string str,unsigned char** out)
{
unsigned char buffer[1024];
BIO* b64 = NULL;
BIO* bio = NULL;
b64 = BIO_new(BIO_f_base64());
BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
bio = BIO_new_mem_buf(str.c_str(), str.length());
bio = BIO_push(b64, bio);
int outlength = BIO_read(bio, buffer, str.length());
*out = new unsigned char[outlength];
memcpy(*out,buffer,outlength);
BIO_free_all(bio);
return outlength;
}
int CryptHelper::AesDecode(unsigned char* ct, int ctLength, unsigned char* key, int keyLength, unsigned char* iv, int ivLength, unsigned char* tag, int tagLength,unsigned char** out) {
unsigned char buffer[1024];
unsigned char tagBuffer[1024];
int outLength,rv,tagOutLength;
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN,ivLength, NULL);
EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv);
EVP_DecryptUpdate(ctx, buffer, &outLength, ct,ctLength);
EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tagLength, tag);
rv = EVP_DecryptFinal_ex(ctx, tagBuffer, &tagOutLength);
if (rv > 0 && tagOutLength>0) {
*out = new unsigned char[outLength+tagOutLength];
memcpy(*out,buffer,outLength);
memcpy((*out) + outLength, tagBuffer, tagOutLength);
}
else {
*out = new unsigned char[outLength];
memcpy(*out, buffer, outLength);
}
EVP_CIPHER_CTX_free(ctx);
return outLength+tagOutLength;
}
string CryptHelper::DesEncode(string str, string strkey) {
if (str.empty())return 0;
const_DES_cblock key = { 0 };
for (int i = 0; i < strkey.length() && i < 8; i++) {
key[i] = strkey[i];
}
int strLength = str.length();
queue<unsigned char> srcdata;
for (int i = 0; i < strLength; i++) {
srcdata.push(str[i]);
}
DES_key_schedule key_sch;
DES_set_key(&key, &key_sch);
vector<unsigned char> outdata;
while (!srcdata.empty()) {
unsigned char datatemp[8];
unsigned char outtemp[8];
int ids = 0;
for (int i = 0; i < 8; i++) {
if (!srcdata.empty()) {
datatemp[i] = srcdata.front();
srcdata.pop();
ids++;
}
}
if (ids < 8) {
for (int s = ids; s < 8; s++) {
datatemp[s] = 8 - ids;
}
}
DES_ecb_encrypt((const_DES_cblock*)&datatemp, (DES_cblock*)&outtemp, &key_sch, DES_ENCRYPT);
for (int j = 0; j < 8; j++) {
outdata.push_back(outtemp[j]);
}
}
if (srcdata.size() % 8 == 0) {
unsigned char datatemp[8];
unsigned char outtemp[8];
memset(datatemp, 8, 8);
DES_ecb_encrypt((const_DES_cblock*)&datatemp, (DES_cblock*)&outtemp, &key_sch, DES_ENCRYPT);
for (int j = 0; j < 8; j++) {
outdata.push_back(outtemp[j]);
}
}
char temp[10];
stringstream ss;
for (size_t i = 0; i < outdata.size(); i++) {
sprintf_s(temp, sizeof(temp), "%02x", outdata[i]);
ss << temp;
}
return ss.str().c_str();
}