dev

dev

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.

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

gshaw
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
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
WestKeys
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 Top

New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
minhajuddin
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement