使用ECC生成私有/公共对密钥:椭圆曲线

问题描述 投票:1回答:2

我正在研究小ECC加密问题。

目标是用C或bash编写一个程序,它将输入一个由十六进制的128个字符组成的散列 (Example: 8A9A35145C4EA5260DF9972C804FE2D3F9F3D7A2AC01A6BEB21C82BB30957B3952273AC9166B90C1207347A925780F84A1D2359E7AA05201C674D2B9746FCA07) ,它将从input hash生成私钥和Elliptic Curve类型的公钥,并显示生成的密钥对。

有人可以为我澄清这个问题。我无法理解为什么我们需要一个哈希(或任何字符串)来生成一对密钥,正如我在许多在线解决方案中所发现的那样this one不需要给出哈希。也许它是一个贱民?也许这是曲线键或类似的东西。

我认为我们需要的是为私钥做这样的事情: openssl ecparam -genkey -noout -out myprivatekey.pem 和公钥生成: openssl -ec -in myprivatekey.pem -pubout -out mypublickey.pem

问题是:为什么我们需要输入一个由128组成的哈希来生成我们的对键?这是安全原因的密码吗?如何用openssl制作技巧?

cryptography elliptic-curve
2个回答
1
投票

如果您有某种输入二进制值需要转换为键,则可以使用哈希。

您可以使用哈希作为私钥的输入。要进行转换,首先应将其转换为数字,然后对其执行n模计算,其中n是ECC域参数的顺序。结果值可以称为s然后,您可以通过执行s * G计算公钥,即与基点进行点乘。

OpenSSL不是低级加密库,因此您必须对其进行编程,可能使用OpenSSL API和随附的BN(大数字)库。这不是那么棘手,但如果你还在谈论128个字符而不是64个字节,那么你可能有很多学习要做。


0
投票

实际上这是我自己的代码,你可以改进它并编辑下面的解决方案:

// gcc -Wall ecdsapubkey.c -o ecdsapubkey -lcrypto
#include <stdio.h>
#include <stdlib.h>
#include <openssl/ec.h>
#include <openssl/obj_mac.h>
#include <openssl/bn.h>

int main()
{
     EC_KEY *eckey = NULL;
     EC_POINT *pub_key = NULL;
     const EC_GROUP *group = NULL;
     BIGNUM *start;
     BIGNUM *res;
     BN_CTX *ctx;

     start = BN_new();
     ctx = BN_CTX_new(); // ctx is an optional buffer to save time from allocating and deallocating memory whenever required

     res = start;
     BN_hex2bn(&res,"8A9A35145C4EA5260DF9972C804FE2D3F9F3D7A2AC01A6BEB21C82BB30957B3952273AC9166B90C1207347A925780F84A1D2359E7AA05201C674D2B9746FCA07");
     eckey = EC_KEY_new_by_curve_name(NID_secp256k1);

     group = EC_KEY_get0_group(eckey);
     pub_key = EC_POINT_new(group);


    printf("private key : "); BN_print_fp(stdout, res); printf("\n");
    EC_KEY_set_private_key(eckey, res);

     /* pub_key is a new uninitialized `EC_POINT*`.  priv_key res is a `BIGNUM*`. */
     if (!EC_POINT_mul(group, pub_key, res, NULL, NULL, ctx))
       printf("Error at EC_POINT_mul.\n");

     EC_KEY_set_public_key(eckey, pub_key);

     char *cc = EC_POINT_point2hex(group, pub_key, 4, ctx);

     char *c=cc;

     int i;

     printf("public key : ");
     for (i=0; i<130; i++) // 1 byte 0x42, 32 bytes for X coordinate, 32 bytes for Y coordinate
     {
       printf("%c", *c++);
     }

     printf("\n");

     BN_CTX_free(ctx);

     free(cc);

     return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.