首页 > 其他分享 >课程设计验收2

课程设计验收2

时间:2022-12-26 11:44:06浏览次数:38  
标签:char 课程设计 openssl sm2 EC unsigned 验收 include

使用自己研究的算法库中的一种完成下面任务

1.  产生一个sm4加解密密钥k,产生两个sm2公私钥对 A(puba,pria),B(pubb,prib)
2 明文p为你们小组的成员的学号,从你的开始依次排列, 发送方A用sm4计算E k(p) = s, 用sm2 计算   E pub(k) = k1   用sm3计算 H(p)=h1, 提交代码及运行结果截图
3  发送方A把s,k1,h1 链接成一包数据发送给接收方B(可以拷贝给B,也可以用socket编程发送给B,用socket有加分)
4  接受方B, D prib(k1) = k, D k (s) = P, H(p) = h2,   并判断 h1,h2是否相等,提交代码和运行结果截图。

我选择openssl/gmssl

1.产生一个sm4加解密密钥k,产生两个sm2公私钥对 A(puba,pria),B(pubb,prib)

SM4的k

SM2的公私钥对



2.明文p为你们小组的成员的学号,从你的开始依次排列, 发送方A用sm4计算E k(p) = s, 用sm2 计算 E pub(k) = k1 用sm3计算 H(p)=h1

SM4:

SM2:

SM3:

3.发送方A把s,k1,h1 链接成一包数据发送给接收方B


4.接受方B, D prib(k1) = k, D k (s) = P, H(p) = h2, 并判断 h1,h2是否相等,提交代码和运行结果截图

SM4:

#include <stdio.h>

unsigned char plain_text[25] = { 0xed,0xec,0x21,0xbb,0xa7,0xca,0xbd,0xc,0xa3,0xa3,0x4f,0x39,0x8e,0x1b,0xfa,0x28,0xb6,0xf,0x62,0x80,0x16,0xf5,0x63,0xd,0x8e,0xc6,0x65,0x62,0x9c,0x41,0xa4,0xbc};
//unsigned char *key = "01234567891234560123456789123456";
unsigned char key[16] = {0x34,0x2f,0x3d,0xed,0x14,0xa4,0x87,0x49,0xc2,0x4b,0x93,0x27,0xbc,0x41,0xf4,0x8c};
unsigned char iv[16] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};

void main(void) 
{
    sm4_ecb_test();


    sm4_cbc_test();


    sm4_ctr_test();
}


SM2:


#include "openssl/bn.h"
#include "openssl/ec.h"
#include "openssl/evp.h"
#include "openssl/rand.h"
#include "openssl/engine.h"
#include "openssl/sm2.h"
#include "openssl/gmapi.h"
//#include "../crypto/sm2/sm2_lcl.h"

EC_GROUP *group1 = NULL;

EC_KEY *sm2_genKey(void)
{
    EC_KEY *keypair = NULL;
    int ret1,ret2;

    keypair = EC_KEY_new();
    group1 = EC_GROUP_new_by_curve_name(NID_sm2p256v1);

    ret1 = EC_KEY_set_group(keypair, group1);
    ret2 = EC_KEY_generate_key(keypair);

	return keypair;
}

void sm2_enc(const EC_GROUP *group, const EVP_MD *md, EC_KEY *key, unsigned char * in_data, int in_len,unsigned char * out_data, size_t * out_len)
{
 
	int ret = 0;
	SM2CiphertextValue *cv = NULL;
	size_t mlen, clen;
	unsigned char *p;


	if (!(cv = SM2_do_encrypt(md, (unsigned char *)in_data, in_len, key))) {
        printf("SM2_do_encrypt failed\n");
	}

	if ((*out_len = i2o_SM2CiphertextValue(group, cv, &out_data)) <= 0) {
        printf("i2o_SM2CiphertextValue failed\n");
	}

}

#if 1

void sm2_dec(const EC_GROUP *group, const EVP_MD *md,EC_KEY *key, const unsigned char * in_data, int in_len,unsigned char * out_data, size_t  * out_len)
{

	int ret = 0;
	SM2CiphertextValue *cv = NULL;
	size_t clen;
	unsigned char *p;


	if (!(cv=o2i_SM2CiphertextValue(group,md, NULL, &in_data,in_len))) {
        printf("o2i_SM2CiphertextValue failed\n");
	}

	if (!(SM2_do_decrypt(md, cv, out_data, out_len, key))) {
        printf("SM2_do_decrypt failed\n");
	}
}
#endif

void sm2_get_pub(EC_KEY *ec_key,char **pubx, char **puby)
{
	int ret = 0;
	BIGNUM *x = NULL;
	BIGNUM *y = NULL;
	BN_CTX *bn_ctx = NULL;
	const EC_GROUP *group = EC_KEY_get0_group(ec_key);
	const EC_POINT *point = EC_KEY_get0_public_key(ec_key);


	x = BN_new();
	y = BN_new();
	bn_ctx = BN_CTX_new();

	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
		if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, bn_ctx)) {
			GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_EC_LIB);
			goto end;
		}
	} else  {
		if (!EC_POINT_get_affine_coordinates_GF2m(group, point, x, y, bn_ctx)) {
			GMAPIerr(GMAPI_F_EC_KEY_GET_ECCPUBLICKEYBLOB, ERR_R_EC_LIB);
			goto end;
		}
	}


    *pubx=BN_bn2hex(x);
    *puby=BN_bn2hex(y);


end:
	BN_free(x);
	BN_free(y);
}


int main(int argc, const char *argv[])
{
	EC_KEY *sm2_key = NULL;
    const BIGNUM *pri_key;
    const EC_POINT *pub_key;

    char priv_key_text[1000]={0};
    char pub_key_text[1000]={0};

    char * priv=NULL;
    char * pub_x=NULL;
    char * pub_y=NULL;

    char data[32]={2BC48F708C6AA1B72EDDC9A07DA2B905C6CB18FDD73FDF0158C92BF811303A28};
    unsigned char enc_data[1000]={0};
    unsigned char dec_data[1000]={0};
    size_t enc_len=1000;
    size_t dec_len=1000;
    int i=0;

    //1 gen key
    sm2_key = sm2_genKey();


    //2 print priv key
    pri_key = EC_KEY_get0_private_key(sm2_key);
    priv=BN_bn2hex(pri_key);
    printf("priv key:\n");
    for (i = 0; i < 64; i++) 
    {
        printf("%c",priv[i]);
    }
    printf("\n");


    //3 print pub key
    sm2_get_pub(sm2_key,&pub_x,&pub_y);
    printf("pub x :\n");
    for (i = 0; i < 64; i++) 
    {
        printf("%c",pub_x[i]);
    }
    printf("\n");
    printf("pub y:\n");
    for (i = 0; i < 64; i++) 
    {
        printf("%c",pub_y[i]);
    }
    printf("\n");


    //4 print plain data
    printf("plain data:\n");
    for (i = 0; i < 24; i++) 
    {
        printf("%x",data[i]);
    }
    printf("\n");



    //5 sm2 enc 
    sm2_enc(group1, EVP_sm3(),sm2_key,data,4,enc_data,&enc_len);
    printf("==============SM2 ENC===========\n");
    printf("enc len=%d\n",(int)enc_len);
    printf("enc data:\n");
    for (i = 0; i < (int)enc_len; i++) 
    {
        printf("%x ",enc_data[i]);
    }
    printf("\n");
    printf("=========================\n");





    return 0;
}


SM3:

因为gmssl并不支持SM3的解密操作所以无法实现解密SM3生成的HASH值

标签:char,课程设计,openssl,sm2,EC,unsigned,验收,include
From: https://www.cnblogs.com/sanfeng-ooo/p/17005371.html

相关文章