从文件中检索私有/公共KEY(OpenSSL)时的随机字符

bal*_*aco 4 openssl elliptic-curve

我正在尝试制作一个程序,在使用OppenSSL EC函数生成公钥/私钥对后,EC_KEY_generate_key将它们存储在单独的文件中并检索它们以生成ECDH KEY.

我的问题是虽然我正确地存储它们(没有任何附加字符),当我读取文件并尝试将十六进制字符转换为BIGNUM时,随机出现一个字符'04'或'00'(甚至有时不出现).因此,当我尝试设置公钥/私钥并检查整个密钥时,它会失败.任何人都可以帮我解决这个问题吗?密钥检查失败是由这些字符引起的还是正常的?

这是我生成/存储私钥的代码(公共密钥是相同的):

    EC_KEY *b = NULL;
const BIGNUM *ppriv_b;
FILE *claveprivb;
const EC_GROUP *group;

b = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1);
group = EC_KEY_get0_group(b);

EC_KEY_generate_key(b);
    claveprivb = fopen("/tmp/mnt/claveprivb", "w+");
    ppriv_b = EC_KEY_get0_private_key(b);
if ((ppriv_b != NULL)) 
    BN_print_fp(claveprivb,ppriv_b);
    fclose(claveprivb);

    //Afterwards do the same with the public key
Run Code Online (Sandbox Code Playgroud)

这是我检索私钥的代码:

    int i, s, blen, bout, ret = 0;
unsigned char *bbuf;
FILE *clavepriv, *clavetotalb;
const char cpriv_string[PRIVATE_KEY_SIZE];
BIGNUM *priv;
EC_KEY *b = NULL;
const EC_GROUP *groupb;

    b = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1);
groupb = EC_KEY_get0_group(b);
    //Open the file with the hexadecimals (PRIVATE KEY)
    clavepriv = fopen("/tmp/mnt/claveprivb", "r");
kk2 = fread(&cpriv_string, sizeof(char), PRIVATE_KEY_SIZE, clavepriv);

priv = BN_new();
    //THIS FUNCTION (HEX2BN) GENERATES THE RANDOM CHARACTER: 
kk2 = BN_hex2bn(&priv, cpriv_string);
ret = EC_KEY_set_private_key(b, priv);

    //HERE I retrieve the public key by the same way and set it into EC_KEY b,
    //the same random character appears in the public key

    if (!EC_KEY_check_key(b)) {
    printf("EC_KEY_check_key failed\n");
} else {
    printf("Key verified OK\n");
}
    //It fails when try to check it.

int k;
clavetotalb = fopen("/tmp/mnt/clavetotalb", "w+");
k = EC_KEY_print_fp(clavetotalb, b, 0);

bout = ECDH_compute_key(bbuf, blen, EC_KEY_get0_public_key(b), b,
        KDF1_SHA1);
Run Code Online (Sandbox Code Playgroud)

任何建议将非常感谢!!!!谢谢!

在我阅读了回答帖后,我尝试使用这些方法对公钥进行解码和编码,但是当我尝试计算ECDH密钥时,我遇到了分段错误.我的程序的目标是生成两个EC密钥,将它们写入多个文件,然后检索它们并用它们计算ECDH密钥.这是我在第一个帖子中从原始程序更改的内容列表,如果出现问题请告诉我:

* Generate EC key (public & private)
* Decode the private key with i2d_ECPrivatekey()
* Decode the public key with i2o_ECPublickey()
* Write them into several files.
* Read the file with the public key.
* Encode it with o2i_ECPublickey()
* Read the file with the private key.
* Encode it with d2i_ECPrivatekey().
* Compute the ECDH key.(Here is where I get the segmentation fault)
Run Code Online (Sandbox Code Playgroud)

我对这个OpenSSL库非常厌倦......对于初次使用的用户来说,它是如此难以接近......

Tho*_*nin 5

EC公钥不是整数; 它是一个曲线点,可以被认为是一整数.这两个整数是点坐标(通常称为XY).

一些符号:曲线在有限域中定义.有限域元素可被映射到整数从0Q-1 ,其中q是字段大小(在使用曲线的情况下,q是素数的整数略低于2 192).让Ñ是在大小字节Q-1 :这是无符号大端表示的大小Q-1 ,即整数,使得2 8(N-1) <= Q-1 <2 8N.对于你的曲线,n = 24.

使用这些表示法,曲线点的标准表示形式恰好包含1 + 2n个字节,按以下顺序排列:

  • 值0x04的字节
  • x超过n个字节的无符号大端表示
  • y超过n个字节的无符号大端表示

所以这解释了你的'0x04'额外字节; 另外,由于xy要在n个字节上进行编码,这可能会强制包含额外的'0x00'字节,以防它们的实际值小于2 8(n-1)(用你的曲线,这个应该是平均约1/128公钥的情况.

这种表示还有其他变体(压缩,其中总大小为1 + n,第一个字节为0x02或0x03,混合,大小为1 + 2n,其中第一个字节为0x06或0x07),但它们是被认为是ECDSA标准(X9.62-2005)中的可选项,并且有传言称压缩格式已获得专利.

底线:如果你想编码和解码EC的公共密钥,你应该看看o2i_ECPublicKey(),并i2o_ECPublicKey()和处理它们的任意字节,而不是编码的整数序列.