使用Erlang?生成RSA密钥对

2eafrhcq  于 2022-12-08  发布在  Erlang
关注(0)|答案(4)|浏览(205)

Erlang有一个加密函数可以生成公钥私钥(文档复制如下)。但是文档似乎很模糊,我找不到任何描述如何生成共享素数或生成器的示例代码。有人能发布一个生成公钥/私钥对的示例吗?提前感谢您的帮助!

dh_generate_key(DHParams) -> {PublicKey,PrivateKey} 
dh_generate_key(PrivateKey, DHParams) -> {PublicKey,PrivateKey}

类型:DHParameters = [P,G] P,G = Mpint其中P是共享素数,G是共享生成器。公钥,私钥= Mpint()生成Diffie-Hellman公钥和私钥(如果没有给定)。

lvjbypge

lvjbypge1#

You don't generate the shared prime number or the generator for Diffie-Hellman. See:
http://en.wikipedia.org/wiki/Diffie-Hellman_key_exchange
The parameters P and G are agreed to ahead of time by both sides. Using the notation of the Wikipedia article, crypto:dh_generate_key is used for steps 2 & 3 to generate a / A & b / B , and then crypto:dh_compute_key is used for steps 4 & 5 to compute the shared secret s .
For RSA , I don't know of a standard library function that generates a public/private key pair. Generating primes is a fairly involved algorithm from what I remember; I would strongly recommend you not try to code it yourself. Diffie-Hellman key pairs are not suitable for use with RSA; they are different algorithms intended for different purposes.
Generally you don't need to generate these at runtime since you can reuse the key pair. You can use any number of other sources to actually generate it. (Maybe ssh-keygen would work? And there's always OpenSSL .) To use the key pair you've created, you'd use the crypto:rsa_ public/private _ encrypt/decrypt functions.

7ajki6be

7ajki6be2#

See cutkey ( https://github.com/yrashk/cutkey )
"cutkey is an Erlang app for generating RSA keys. It is implemented as a port driver that calls OpenSSL's RSA_generate_key on a thread from the async pool."

zqry0prt

zqry0prt3#

操作系统:命令(“openssl genpkey -算法RSA -输出私钥。pem -pkeyopt rsa密钥生成位:2048”)。

ars1skjm

ars1skjm4#

You can use the crypto:generate_key/2 function to create RSA public and private keys:
For a 2048 length

{Pub, Priv} = crypto:generate_key(rsa, {2048,65537}).

Or for a 1024 length

{Pub, Priv} = crypto:generate_key(rsa, {1024,65537}).

See: https://erlang.org/doc/apps/crypto/crypto.pdf (page 37)

generate_key(Type, Params) -> {PublicKey, PrivKeyOut}

Where

Type = rsa

Params = rsa_params() =
    {ModulusSizeInBits :: integer(),
     PublicExponent :: key_integer()}

相关问题