Encryption in elixir

Hello,

I have a problem with encryption.

I have keys generated by kleopatra software: https://www.openpgp.org/software/kleopatra/

It’s possible use my public key (generated by kleopatra) and encrypt - for example text, with using elixir?
I found erlang module: http://erlang.org/doc/man/crypto.html
but i couldn’t find any examples for my problem.

If you know encryption schema and how to extract public key from the Kleopatra key, then answer is:

Probably yes

Otherwise I would say that I do not know about any tool for that.

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.

2 Likes

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.

2 Likes

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

1 Like