kirega
How to generate RSA public key using crypto provided Exponent and Modulus
Hey, I am trying to achieve generating an RSA public key, any ideas on how to achieve this in elixir?
Marked As Solved
massimo
I wrote a service that encrypts secrets using asymmetric cryptography and this is the function that generates key pairs.
The keys are encoded in PEM format
with private keys like
-----BEGIN RSA PRIVATE KEY-----
$key
-----END RSA PRIVATE KEY-----
and the public keys
ssh-rsa $key
You might not need the text format encoding and skip it.
def key_pair(bits \\ 4096) do
{:RSAPrivateKey, _, modulus, publicExponent, _, _, _, _exponent1, _, _, _otherPrimeInfos} =
rsa_private_key = :public_key.generate_key({:rsa, bits, 65537})
rsa_public_key = {:RSAPublicKey, modulus, publicExponent}
pem_entry = :public_key.pem_entry_encode(:RSAPrivateKey, rsa_private_key)
private_key = :public_key.pem_encode([pem_entry])
public_key = :public_key.ssh_encode([{rsa_public_key, []}], :openssh_public_key)
IO.puts(~s/PUBLIC KEY:\n\n#{public_key}\n\nPRIVATE KEY\n\n#{private_key}/)
end
5
Also Liked
samuelventura
Adjusted for OTP 25
def key_pair(bits \\ 4096) do
{:RSAPrivateKey, _, modulus, publicExponent, _, _, _, _exponent1, _, _, _otherPrimeInfos} =
rsa_private_key = :public_key.generate_key({:rsa, bits, 65537})
rsa_public_key = {:RSAPublicKey, modulus, publicExponent}
pem_entry = :public_key.pem_entry_encode(:RSAPrivateKey, rsa_private_key)
private_key = :public_key.pem_encode([pem_entry])
public_key = :ssh_file.encode([{rsa_public_key, []}], :openssh_key)
IO.puts(~s/PUBLIC KEY:\n\n#{public_key}\n\nPRIVATE KEY\n\n#{private_key}/)
end
6
riccardomanfrin
fwiw: otp 26 (2048 bit key)
defmodule RSAKeyGenerator do
def generate_rsa_key_pair() do
{:RSAPrivateKey, _, modulus, publicExponent, _, _, _, _exponent1, _, _, _otherPrimeInfos} =
rsa_private_key = :public_key.generate_key({:rsa, 2048, 65537})
rsa_public_key = {:RSAPublicKey, modulus, publicExponent}
private_key =
[:public_key.pem_entry_encode(:RSAPrivateKey, rsa_private_key)]
|> :public_key.pem_encode()
public_key =
[:public_key.pem_entry_encode(:RSAPublicKey, rsa_public_key)]
|> :public_key.pem_encode()
{private_key, public_key}
end
end
5
Popular in Questions
Background
Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
can someone please explain to me how Enum.reduce works with maps
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
I would like to know what is the best IDE for elixir development?
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
In AR this is so simple
@articles = current_user.articles
How to do in Ecto?
def index(conn, _params) do
current_user = conn.assig...
New
Other popular topics
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New








