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
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-----
Popular in Questions
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
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”:
14:57:30.512 [warn] ...
New
Hey guys.
I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
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
Hi,
I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Hey,
I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
Hi all
I want to have a unix time, from the current time plus 1 hour.
DateTime.now + 1 hour
How to get it in elixir?
Thanks
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
can someone please explain to me how Enum.reduce works with maps
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
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
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New








