2025-01-16 16:17:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"); //Ӧ<><D3A6><EFBFBD>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>FACCAB57B27FD9BD1627506EF751EE61<36><31>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
int seriseLen = strlen((const char*)pSerise);
|
|
|
|
|
|
int keyLen = strlen(pKey);
|
|
|
|
|
|
time_t t = now_secs ;// -8*60*60; //<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
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);
|
2025-03-04 17:29:04 +08:00
|
|
|
|
printf("now_secs:%lld ||series: %s ||ptime: %x %x %x %x\n", now_secs, output, pTime[3], pTime[2], pTime[1], pTime[0]);//lnk20250304
|
2025-01-16 16:17:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MyGetSM4Code(char* input,unsigned char* szKey,char* output)
|
|
|
|
|
|
{
|
|
|
|
|
|
SM4 sm4Encode;
|
|
|
|
|
|
sm4Encode.sm4_enc(input,strlen(input),output,szKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-28 10:40:00 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////lnk20250728
|
2025-01-16 16:17:01 +08:00
|
|
|
|
|
2025-07-28 10:40:00 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* SM4 ECB <EFBFBD><EFBFBD><EFBFBD>ܺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ZeroPadding<EFBFBD><EFBFBD>
|
|
|
|
|
|
* @param encrypted_base64_str <EFBFBD><EFBFBD><EFBFBD>ܺ<EFBFBD><EFBFBD><EFBFBD> Base64 <EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
* @param key_str 16<EFBFBD>ֽ<EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD>
|
|
|
|
|
|
* @param decrypted_output <EFBFBD><EFBFBD><EFBFBD>ܺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD>ȷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
|
|
|
|
|
|
* @return 0 <EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 ʧ<EFBFBD><EFBFBD>
|
|
|
|
|
|
*/
|
|
|
|
|
|
// ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9> ZeroPadding<6E><67><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫȥ<D2AA><C8A5>ĩβ 0
|
|
|
|
|
|
int sm4_ecb_decrypt_zeropad_base64(const char* base64_cipher, const char* key_str, char* plaintext_out) {
|
|
|
|
|
|
if (!base64_cipher || !key_str || !plaintext_out) {
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
2025-01-16 16:17:01 +08:00
|
|
|
|
|
2025-07-28 10:40:00 +08:00
|
|
|
|
// base64 decode
|
|
|
|
|
|
unsigned char cipher_bin[256] = {0};
|
|
|
|
|
|
long cipher_len = 0; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
int ret = base64_decode(base64_cipher, strlen(base64_cipher),
|
|
|
|
|
|
(char*)cipher_bin, &cipher_len);
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ʧ<EFBFBD>ܴ<EFBFBD><DCB4><EFBFBD>
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (cipher_len <= 0 || cipher_len % 16 != 0) {
|
|
|
|
|
|
return -2; // SM4 <20>鳤<EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD>Ϊ 16 <20>ı<EFBFBD><C4B1><EFBFBD>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// <><D7BC> key
|
|
|
|
|
|
if (strlen(key_str) != 16) {
|
|
|
|
|
|
return -3; // SM4 Ҫ<><D2AA><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF> 16 <20>ֽ<EFBFBD>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned char key_bin[16] = {0};
|
|
|
|
|
|
memcpy(key_bin, key_str, 16);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><>ʼ<EFBFBD><CABC> SM4 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
sm4_context ctx;
|
|
|
|
|
|
SM4 sm4;
|
|
|
|
|
|
sm4.sm4_setkey_dec(&ctx, key_bin);
|
2025-01-16 16:17:01 +08:00
|
|
|
|
|
2025-07-28 10:40:00 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
unsigned char plain_buf[256] = {0};
|
|
|
|
|
|
sm4.sm4_crypt_ecb(&ctx, SM4_DECRYPT, cipher_len, cipher_bin, plain_buf);
|
|
|
|
|
|
|
|
|
|
|
|
// ȥ<><C8A5> zero padding<6E><67>ĩβ<C4A9><CEB2> 0x00<30><30>
|
|
|
|
|
|
int plain_len = cipher_len;
|
|
|
|
|
|
while (plain_len > 0 && plain_buf[plain_len - 1] == 0x00) {
|
|
|
|
|
|
plain_len--;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
memcpy(plaintext_out, plain_buf, plain_len);
|
|
|
|
|
|
plaintext_out[plain_len] = '\0';
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2025-01-16 16:17:01 +08:00
|
|
|
|
|