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
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
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
Thanks, my solution in elixir
"echo 'hello' | gpg --recipient-file publickey.asc --encrypt --armor" |> String.to_charlist |> :os.cmd
Popular in Questions
Other popular topics
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









