fschuindt

fschuindt

How to generate GPG-compatible and PEM-encoded keys using Elixir?

I’m trying to create an Elixir code that will generate GnuPG-compatible key pairs and also to be able to encrypt, decrypt and sign messages. But it’s also important that it can export PEM-formatted keys and messages, so users can use it with the GnuPG implementation.

Here’s how I began my attempt:

defmodule GPG do
  def test do
    private_key = generate_private_key()
    public_pem = pem_encoded_public_key(private_key)

    IO.puts("Public Key PEM:")
    IO.puts(public_pem)
  end

  defp pem_encoded_public_key(private_key) do
    private_key
    |> public_key_from_private_key()
    |> pem_entry_encode(:RSAPublicKey)
  end

  defp generate_private_key do
    :public_key.generate_key({:rsa, 2048, 65537})
  end

  defp public_key_from_private_key(private_key) do
    {:RSAPublicKey, elem(private_key, 2), elem(private_key, 2)}
  end

  defp pem_entry_encode(key, type) do
    :public_key.pem_encode([:public_key.pem_entry_encode(type, key)])
  end
end

If I run GNU.test() on iEx, it will output:

iex(3)> GPG.test()
Public Key PEM:
-----BEGIN RSA PUBLIC KEY-----
MIICCgKCAQEAxEKcdLQmc8ik+srOf/hVTUzADGOVOrtOAigyqCMmtxAmNW4fTj5+
3MTSBTHQLSO0gWeuq2gR2Qrb/vd0d3OOielAudonvgo3enDL2jDp49zokB333LgK
64QK+l1erkjmy1Atj2fbXJAE0O2VzZmfMjCUGBJJQ/cklWXZH0Qczjojith2VHJU
jx4OoGHzIOOJ9qDOPPrY43vvymWs4CEM7OPUKq37MDfU0yxtSGoBERW2a/4jUOlk
JNQSBzjUSfd7VZaIziyF8NRy47j1ErrES3DS8m5zPoR/d+qyrQs0pg1S7Wgr9Aws
tpwM6yFEKhK507S4gdBuVWqyIVuiI/HcmwKCAQEAxEKcdLQmc8ik+srOf/hVTUzA
DGOVOrtOAigyqCMmtxAmNW4fTj5+3MTSBTHQLSO0gWeuq2gR2Qrb/vd0d3OOielA
udonvgo3enDL2jDp49zokB333LgK64QK+l1erkjmy1Atj2fbXJAE0O2VzZmfMjCU
GBJJQ/cklWXZH0Qczjojith2VHJUjx4OoGHzIOOJ9qDOPPrY43vvymWs4CEM7OPU
Kq37MDfU0yxtSGoBERW2a/4jUOlkJNQSBzjUSfd7VZaIziyF8NRy47j1ErrES3DS
8m5zPoR/d+qyrQs0pg1S7Wgr9AwstpwM6yFEKhK507S4gdBuVWqyIVuiI/Hcmw==
-----END RSA PUBLIC KEY-----

But if I create a test_public_key.txt file with this given key as content and run GnuPG to evaluate it, this happens:

$ gpg --show-keys test_public_key.txt 
gpg: no valid OpenPGP data found.

I’m wondering what I’m missing.

First Post!

massimo

massimo

I think the problem is that GPG keys have different headers than RSA keys and they contain a checksum

something like

-----BEGIN PGP PUBLIC KEY BLOCK-----
MIICCgKCAQEAxEKcdLQmc8ik+srOf/hVTUzADGOVOrtOAigyqCMmtxAmNW4fTj5+
3MTSBTHQLSO0gWeuq2gR2Qrb/vd0d3OOielAudonvgo3enDL2jDp49zokB333LgK
64QK+l1erkjmy1Atj2fbXJAE0O2VzZmfMjCUGBJJQ/cklWXZH0Qczjojith2VHJU
jx4OoGHzIOOJ9qDOPPrY43vvymWs4CEM7OPUKq37MDfU0yxtSGoBERW2a/4jUOlk
JNQSBzjUSfd7VZaIziyF8NRy47j1ErrES3DS8m5zPoR/d+qyrQs0pg1S7Wgr9Aws
tpwM6yFEKhK507S4gdBuVWqyIVuiI/HcmwKCAQEAxEKcdLQmc8ik+srOf/hVTUzA
DGOVOrtOAigyqCMmtxAmNW4fTj5+3MTSBTHQLSO0gWeuq2gR2Qrb/vd0d3OOielA
udonvgo3enDL2jDp49zokB333LgK64QK+l1erkjmy1Atj2fbXJAE0O2VzZmfMjCU
GBJJQ/cklWXZH0Qczjojith2VHJUjx4OoGHzIOOJ9qDOPPrY43vvymWs4CEM7OPU
Kq37MDfU0yxtSGoBERW2a/4jUOlkJNQSBzjUSfd7VZaIziyF8NRy47j1ErrES3DS
8m5zPoR/d+qyrQs0pg1S7Wgr9AwstpwM6yFEKhK507S4gdBuVWqyIVuiI/Hcmw==
=ax4T <== CHECKSUM, ARBITRARLY INVENTED BY ME
-----END PGP PUBLIC KEY BLOCK-----

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
ashish173
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement