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
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir?
...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
How to handle excepions in elixir?
Suppose i have A, B, C ,D, E modules. and each module has get() function.
A.get() method will call t...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
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 records...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Other popular topics
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
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
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
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
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









