Files
microser/misc/my_encrypt.cpp
2025-01-16 16:17:01 +08:00

68 lines
1.6 KiB
C++

using namespace std;
#include "SM4.h"
#include "../mms/db_interface.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
void GetSM4Code(unsigned char* pSerise,char* pKey,char* output)
{
int i=0;
unsigned char szKey[16+1] = {0};
unsigned char input[16+1] = {0};
time_t now_secs = apr_time_sec(apr_time_now());
#if 0
systime_t tm;
memset(&tm,0,sizeof(systime_t));
tm.year = 2017;
tm.mon = 5;
tm.mday =29;
tm.hour = 15;
tm.min = 16;
tm.sec = 20;
now_secs = apr_time_sec(getTicksOfSystemTime(&tm));
apr_snprintf(pSerise,sizeof(pSerise),"%s","0123456789");
apr_snprintf(pKey,sizeof(pKey),"%s","epri.sgcc.com.cn"); //应生成加密密文 “FACCAB57B27FD9BD1627506EF751EE61”
#endif
int seriseLen = strlen((const char*)pSerise);
int keyLen = strlen(pKey);
time_t t = now_secs ;// -8*60*60; //调整时区
unsigned char* pTime = (unsigned char*)&t;
unsigned char* p_u_key = (unsigned char*)pKey;
input[0] = pTime[3];
input[1] = pTime[2];
input[2] = pTime[1];
input[3] = pTime[0];
for (i = 0; i < 12; i++) {
if(i<seriseLen)
input[4+i] = pSerise[i];
else
input[4+i] = 0;
}
for (i = 0; i < 16; i++) {
if(i<keyLen)
szKey[i] = p_u_key[i];
else
szKey[i] = 0;
}
SM4 sm4Encode;
sm4Encode.sm4_enc((char*)input,seriseLen+4,(char*)output,szKey);
printf("%lld || %s || %x %x %x %x\n", now_secs, output, pTime[3], pTime[2], pTime[1], pTime[0]);
}
void MyGetSM4Code(char* input,unsigned char* szKey,char* output)
{
SM4 sm4Encode;
sm4Encode.sm4_enc(input,strlen(input),output,szKey);
}
//////////////////////////////////////////////////////////////////////////////////////////////////