如何将椭圆曲线x和y转换为pem格式公共证书 [英] how to convert elliptic curve x and y to pem format public certificate

查看:534
本文介绍了如何将椭圆曲线x和y转换为pem格式公共证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个256位整数,它们是secp256k1 ec密钥对的公钥.

I have two 256-bit integers which = public key for a secp256k1 ec keypair.

我正在寻找一种以十六进制格式提供给我的这两个值来创建PEM证书的方法.

I'm looking for a way to create a PEM cert from these two values provided to me in Hex format.

有人知道openssl是否有办法做到这一点吗?

Does anyone know if openssl has a way to accomplish this?

推荐答案

OpenSSL公开了一个函数 PEM_write_EC_PUBKEY() ,可用于将公钥写入PEM格式.首先,您必须使用 EC_KEY_xyz()中的函数来构造您的公钥. 家庭.以下代码段将您要查找的内容(我认为)输出到stdout:

OpenSSL exposes a function PEM_write_EC_PUBKEY() that you can use to write the public key to PEM format. You will first have to construct your public key with functions from the EC_KEY_xyz() family. The following code snippet outputs what (I think) you are looking for to stdout:

#include <stdio.h>
#include <openssl/ec.h>
#include <openssl/pem.h>

const char *xHex = "053b5b02d673e6f115b538de3587318821149d3e7bc65903f300b8cfffcacdaa";
const char *yHex = "75e5d460e9d407672ff86683b748b6e882b361fa2fdf78845f8a9a369f6d016e";

int main(
    int argc,
    char **argv)
{
    EC_KEY *eckey = NULL;
    BIGNUM *x = NULL, *y = NULL;

    eckey = EC_KEY_new_by_curve_name(NID_secp256k1);

    BN_hex2bn(&x, xHex);
    BN_hex2bn(&y, yHex);

    EC_KEY_set_public_key_affine_coordinates(eckey, x, y);
    EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
    PEM_write_EC_PUBKEY(stdout, eckey);

    BN_free(x);
    BN_free(y);
    EC_KEY_free(eckey);
}

结果是:

$ ./eckey
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEiSsGkRmvLkGTl7piCk9/j+k+2O5W
e7JoJ5UzJnkdGqvE9agv2adnlftoaZBM497eOsz/tua7uKZr9SwzzJVyVg==
-----END PUBLIC KEY-----

但是,您提供的坐标不在曲线上,如以下命令所示:

However, the coordinates that you provided are not located on the curve, as illustrated by the following command:

$ openssl ec -pubin -in <(./eckey) -pubout -text -noout 
read EC key
unable to load Key
140735626654664:error:1006706B:elliptic curve routines:ec_GFp_simple_oct2point:point is not on curve:ecp_oct.c:417:
140735626654664:error:10098010:elliptic curve routines:o2i_ECPublicKey:EC lib:ec_asn1.c:1286:
140735626654664:error:100D708E:elliptic curve routines:ECKEY_PUB_DECODE:decode error:ec_ameth.c:208:
140735626654664:error:0B07707D:x509 certificate routines:X509_PUBKEY_get:public key decode error:x_pubkey.c:154:
140735626654664:error:0906700D:PEM routines:PEM_ASN1_read_bio:ASN1 lib:pem_oth.c:83:

这篇关于如何将椭圆曲线x和y转换为pem格式公共证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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