int main(int argc, char *argv[]) { ec_param *ecp; sm2_ec_key *key_B; message_st message_data; int type = TYPE_GFp; int point_bit_length = 256; char **sm2_param = sm2_param_recommand; ecp = ec_param_new(); ec_param_init(ecp, sm2_param, type, point_bit_length); key_B = sm2_ec_key_new(ecp); sm2_ec_key_init(key_B, sm2_param_d_B[ecp->type], ecp); memset(&message_data, 0, sizeof(message_data)); sm2_hex2bin((BYTE *)sm2_param_k[ecp->type], message_data.k, ecp->point_byte_length); sm2_bn2bin(key_B->d, message_data.private_key, ecp->point_byte_length); sm2_bn2bin(key_B->P->x, message_data.public_key.x, ecp->point_byte_length); sm2_bn2bin(key_B->P->y, message_data.public_key.y, ecp->point_byte_length); message_data.decrypt = (BYTE *)OPENSSL_malloc(message_data.message_byte_length + 1); memset(message_data.decrypt, 0, message_data.message_byte_length + 1); BIGNUM *P_x; BIGNUM *P_y; //BIGNUM *d; BIGNUM *k; xy_ecpoint *P; xy_ecpoint *xy1; xy_ecpoint *xy2; int pos1; BYTE t; int i; sm2_hash local_C_3; P_x = BN_new(); P_y = BN_new(); k = BN_new(); P = xy_ecpoint_new(ecp); xy1 = xy_ecpoint_new(ecp); xy2 = xy_ecpoint_new(ecp); BN_bin2bn(message_data.public_key.x, ecp->point_byte_length, P_x); BN_bin2bn(message_data.public_key.y, ecp->point_byte_length, P_y); BN_bin2bn(message_data.k, ecp->point_byte_length, k); xy_ecpoint_init_xy(P, P_x, P_y, ecp); xy_ecpoint_mul_bignum(xy1, ecp->G, k, ecp); xy_ecpoint_mul_bignum(xy2, P, k, ecp); char cryptstring[1024]; scanf("%s", cryptstring); / 利用函数sm2_hex2bin将16进制字符串cryptstring转换成二进制流填充到message_data.C里 / / 计算明文长度 message_data.message_byte_length */ message_data.klen_bit = message_data.message_byte_length * 8;(请根据注释补充)
时间: 2023-08-16 19:03:28 浏览: 117
C语言中 int main(int argc,char *argv[])的两个参数详解
以下是补充完整的代码:
```c
int main(int argc, char *argv[]) {
ec_param *ecp;
sm2_ec_key *key_B;
message_st message_data;
int type = TYPE_GFp;
int point_bit_length = 256;
char **sm2_param = sm2_param_recommand;
ecp = ec_param_new();
ec_param_init(ecp, sm2_param, type, point_bit_length);
key_B = sm2_ec_key_new(ecp);
sm2_ec_key_init(key_B, sm2_param_d_B[ecp->type], ecp);
memset(&message_data, 0, sizeof(message_data));
sm2_hex2bin((BYTE *)sm2_param_k[ecp->type], message_data.k, ecp->point_byte_length);
sm2_bn2bin(key_B->d, message_data.private_key, ecp->point_byte_length);
sm2_bn2bin(key_B->P->x, message_data.public_key.x, ecp->point_byte_length);
sm2_bn2bin(key_B->P->y, message_data.public_key.y, ecp->point_byte_length);
message_data.decrypt = (BYTE *)OPENSSL_malloc(message_data.message_byte_length + 1);
memset(message_data.decrypt, 0, message_data.message_byte_length + 1);
BIGNUM *P_x;
BIGNUM *P_y;
BIGNUM *k;
xy_ecpoint *P;
xy_ecpoint *xy1;
xy_ecpoint *xy2;
int pos1;
BYTE t;
int i;
sm2_hash local_C_3;
P_x = BN_new();
P_y = BN_new();
k = BN_new();
P = xy_ecpoint_new(ecp);
xy1 = xy_ecpoint_new(ecp);
xy2 = xy_ecpoint_new(ecp);
BN_bin2bn(message_data.public_key.x, ecp->point_byte_length, P_x);
BN_bin2bn(message_data.public_key.y, ecp->point_byte_length, P_y);
BN_bin2bn(message_data.k, ecp->point_byte_length, k);
xy_ecpoint_init_xy(P, P_x, P_y, ecp);
xy_ecpoint_mul_bignum(xy1, ecp->G, k, ecp);
xy_ecpoint_mul_bignum(xy2, P, k, ecp);
char cryptstring[1024];
scanf("%s", cryptstring);
sm2_hex2bin((BYTE *)cryptstring, message_data.C, sizeof(message_data.C));
// 计算明文长度
message_data.message_byte_length = message_data.klen_bit / 8;
// 进行解密操作
pos1 = 0;
sm2_init_hash(ecp->type, &local_C_3);
for (i = 0; i < message_data.message_byte_length; i++) {
if (pos1 == ecp->point_byte_length * 2) {
xy_ecpoint_mul_bignum(xy1, xy2, ecp->one_time_pad, ecp);
pos1 = 0;
}
message_data.decrypt[i] = (BYTE)(message_data.C[i] ^ xy1->x[pos1]);
sm2_update_hash(&local_C_3, &message_data.decrypt[i], 1);
pos1++;
}
message_data.decrypt[i] = '\0';
// 输出解密后的明文
printf("解密:%s\n", message_data.decrypt);
return 0;
}
```
在上述代码中,我们使用 `scanf` 函数从标准输入中读取需要解密的密文,并使用 `sm2_hex2bin` 函数将其转换为二进制流填充到 `message_data.C` 字段中。然后,我们根据密文长度计算出明文长度,并进行解密操作,最终输出解密后的明文。
阅读全文