@cmkarlsson
Thanks alot, your solution worked wonderfully
.
To extract the public key from the certificate record was hard but I have managed to do it using trial and error hacks. (If you know a better way please advice) .
Here is my final working solution incase someone else needs help.
def security do
cert_text = File.read!("./lib/mpesa_elixir/keys/sandbox_cert.cer") |> String.trim()
[pem_entry] = :public_key.pem_decode(cert_text)
plk = :public_key.pem_entry_decode(pem_entry)
list = Tuple.to_list(elem(plk, 1))
der_value = List.keyfind(list, :SubjectPublicKeyInfo, 0) |> elem(2)
plain_text = "Safaricom133!"
public_key = :public_key.der_decode(:RSAPublicKey, der_value)
ciphertext =
:public_key.encrypt_public(plain_text, public_key, [{:rsa_pad, :rsa_pkcs1_padding}])
:base64.encode(ciphertext)
end
Regards.


















