dev

dev

Encryption in elixir

Hello,

I have a problem with encryption.

I have keys generated by kleopatra software: Kleopatra - OpenPGP

It’s possible use my public key (generated by kleopatra) and encrypt - for example text, with using elixir?
I found erlang module: crypto — OTP 29.0.2 (crypto 5.9)
but i couldn’t find any examples for my problem.

Most Liked

cmkarlsson

cmkarlsson

If you are working with RSA keys.

Convert the pgp key to PEM format and import and encrypt using the :public_key module.

For the pgp → pem conversion I see people are using gpgsm

Once you have the pem file you can do:

{:ok, pem} = File.read(yourfile)
[pem_entry | _ ] = :public_key.pem_decode(pem)
# Assuming first entry is the key you want (or only one key)
key = :public_key.pem_entry_decode(pem_entry)
:public_key.encrypt_public(plaintext, key, [])

You need to read the documentation for public_key and likely crypto.

cmkarlsson

cmkarlsson

I haven’t actually used gpgsm so can’t help you there. I think the problem is that the keys are in different format and the pgp key uses a format that the pem_decode can’t understand. Reading up a bit on this I don’t know if this actually does what you want it to.

Who is supposed to decrypt the encrypted payload? The private key is still in pgp format so you can’t use that one directly either.

An important thing to consider is that it is considered bad practice and potential security risk to re-use a key for different purposes.

If you want to use pgp for crypto, perhaps use the pgp (or gpg) application or library directly through a command or port.

dev

dev

Thanks, my solution in elixir
"echo 'hello' | gpg --recipient-file publickey.asc --encrypt --armor" |> String.to_charlist |> :os.cmd

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
bsollish-terakeet
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
albydarned
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement