[erlang-questions] How to encode data with private key?

Martin Koroudjiev <>
Wed Jun 15 10:03:53 CEST 2016


I figured it out. The private key provided by Google contains algorithm
and version information. It was with "-----BEGIN PRIVATE KEY-----"
header. I had to convert it to only RSA key with "openssl rsa -in
server.key -out server_new.key". Then I use the following to sign the data:

[PrivEntry] = public_key:pem_decode(list_to_binary(PrivateKey)),
Priv = public_key:pem_entry_decode(PrivEntry),
public_key:sign(Data, sha256, Priv);

That's how to sign Google's JSON Web Tokens. Hope this will be helpful
to someone else.

Regards,
Martin

On 6/14/2016 3:17 PM, Martin Koroudjiev wrote:
> Hello,
>
> I am trying to implement the OAuth 2 Google web service 2-legged flow.
> When creating the service, I was granted a private key that I have to
> use to sign the request. I try the following:
>
> PrivKey = "-----BEGIN RSA PRIVATE KEY----- ...",
> [PrivEntry] = public_key:pem_decode(list_to_binary(PrivKey)),
> Priv = public_key:pem_entry_decode(PrivEntry),
> public_key:encrypt_private(Data, Priv).
>
> But I get the following error:
>
> ** exception error: no function clause matching
> public_key:encrypt_private(<<"..".>>,
>                                                                           
> <<48,130,4,...>>,
>                                                                           
> []) (public_key.erl, line 364)
>      in function  jwt:encode/4 (../src/jwt.erl, line 25)
>
> Does anyone know what the format of the Key must be for
> public_key:encrypt_private(PlainText, Key)?
>
> Thank you,
> Martin
>



More information about the erlang-questions mailing list