

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("now_secs:%lld ||series: %s ||ptime: %x %x %x %x\n", now_secs, output, pTime[3], pTime[2], pTime[1], pTime[0]);//lnk20250304
}

 void MyGetSM4Code(char* input,unsigned char* szKey,char* output)
{
	SM4 sm4Encode;
	sm4Encode.sm4_enc(input,strlen(input),output,szKey); 
 }

//////////////////////////////////////////////////////////////////////////////////////////////////lnk20250728

/**
 * SM4 ECB 解密函数（ZeroPadding）
 * @param encrypted_base64_str 加密后的 Base64 字符串
 * @param key_str 16字节密钥（明文）
 * @param decrypted_output 解密后输出的字符串，需由调用者预先分配内存
 * @return 0 成功，非 0 失败
 */
// 注意：这里使用 ZeroPadding，因此需要去除末尾 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;
    }

    // base64 decode
    unsigned char cipher_bin[256] = {0};
	long cipher_len = 0;                   // 输出长度
    int ret = base64_decode(base64_cipher, strlen(base64_cipher),
                        (char*)cipher_bin, &cipher_len);
	if (ret != 0) {
    	// 解码失败处理
    	return -1;
	}
	
    if (cipher_len <= 0 || cipher_len % 16 != 0) {
        return -2; // SM4 块长度必须为 16 的倍数
    }

    // 准备 key
    if (strlen(key_str) != 16) {
        return -3; // SM4 要求密钥是 16 字节
    }

    unsigned char key_bin[16] = {0};
    memcpy(key_bin, key_str, 16);

    // 初始化 SM4 解密上下文
    sm4_context ctx;
    SM4 sm4;
    sm4.sm4_setkey_dec(&ctx, key_bin);

    // 解密
    unsigned char plain_buf[256] = {0};
    sm4.sm4_crypt_ecb(&ctx, SM4_DECRYPT, cipher_len, cipher_bin, plain_buf);

    // 去除 zero padding（末尾的 0x00）
    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;
}

