解析 EC 公钥 [英] Parse EC Public key

查看:163
本文介绍了解析 EC 公钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ECIES 上工作,需要加载对等公钥.加载EC公钥

I an working on ECIES and need to load peer public key. Load EC Public key

我正在使用 ECDH 并且需要加载对等公钥.当我尝试从 PEM 文件加载公钥时,似乎没有问题

I an using ECDH and need to load peer public key. When I try to load public key from PEM file , seems no issue

问题在这里:

EVP_PKEY * get_peer_key()  
{
     // base64 certificate data of alice_pub_key.pem
     char *buffer= "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEjWrT7F97QrSqGrlIgPK8dphNBicNO6gDLfOIMjhF2MiLuuzd7L7BP+bLCuNtKKe/2dOkgPqgXv4BFWqgp6PZXQ=="`
     // calculate buffer length
     int l = strlen(buffer)
     //create bio from buffer
     BIO *in = BIO_new_mem_buf(buffer,l)
     //gnerate ec key   
     EC_KEY *eckey = PEM_read_bio_EC_PUBKEY(in,NULL,NULL,NULL)` // ==> FAIL
     //need to convert to EVP format
     EVP_PKEY *peerKey = EVP_PKEY_new()
     //assign ec key evp
     if(EVP_PKEY_assign_EC_KEY(peerKey,eckey) != 1 )
         printf("\n error hapened");
     return peerKey;
}

工作正常:

EVP_PKEY * get_peer_key()
{
     //Load PEM format file
     char * infile = "alice_pub_key.pem";
     //create bio
     BIO *in = BIO_new(BIO_s_file());
     //read bio file
     BIO_read_filename(in , infile);
     //create eckey
     EC_KEY *eckey = PEM_read_bio_EC_PUBKEY(in,NULL,NULL,NULL); // ==> success
     // create peer key   
     EVP_PKEY *peerKey = EVP_PKEY_new();
     //assign public key
     if(EVP_PKEY_assign_EC_KEY(peerKey,eckey) != 1 )
         printf("\n error hapened");
     return peerKey;
}

有人可以建议读取pem文件的base64数据时出了什么问题

Can some one suggest whats going wrong while reading base64 data of pem file

推荐答案

有两种解决方法:

  1. 使用页眉和页脚换行符以及换行符(在第 64 个字符处;
  2. )创建 PEM
  3. Base 64 解码文本,然后通过解析生成的 ASN.1/DER 二进制文件来处理它;
  1. Creating a PEM using a header and footer line and line breaks (at the 64th character;
  2. Base 64 decoding the text and then handling it by parsing the resulting ASN.1 / DER binary;

我更喜欢后者,因为我讨厌添加行等,它充其量是容易出错的,并且应尽可能避免字符串操作.

I'd prefer the latter, as I abhor adding lines and such, it is error prone at best, and string manipulations should be avoided where possible.

请注意,这里假设 base 64 包含我之前向您展示的 SubjectPublicKeyInfo 结构.否则,您可能需要找出如何解析 X9.62 结构或仅解析一个点.

Note that this assumes that the base 64 contains a SubjectPublicKeyInfo structure which I've shown you earlier. Otherwise you may have to find out how to parse a X9.62 structure or just a point.

这篇关于解析 EC 公钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆