Public/Private Key Encryption, Sign and Verification in Erlang
You want to encrypt/decrypt some content?
You want to generate a signature and let others verify it?
At least that's what I wanted to do, so here it is.
First generate keys if you don't have some available:
openssl genrsa -out private.pem 2048 openssl rsa -in private.pem -out public.pem -outform PEM -pubout
Load the raw keys:
{ok, RawSKey} = file:read_file("private.pem"). {ok, RawPKey} = file:read_file("public.pem"). [EncSKey] = public_key:pem_decode(RawSKey). SKey = public_key:pem_entry_decode(EncSKey). [EncPKey] = public_key:pem_decode(RawPKey). PKey = public_key:pem_entry_decode(EncPKey).
Let's encrypt a message with the private key and decrypt with the public key:
Msg = <<"hello crypto world">>. CMsg = public_key:encrypt_private(Msg, SKey). Msg = public_key:decrypt_public(CMsg, PKey).
We can do it the other way, encrypt with the public key and decrypt with the private key:
Let's generate a signature for the message that others can verify with our public key: