I can successfully RSA sign a message using lower-level functions such as crypto:mpint, crypto:rsa_private_encrypt (e.g. as in the samples at http://lj.rossia.org/users/eacher/2013/01/07/).<br><br>However I'd prefer to use what looks like higher-level functions in the public_key module but I'm not sure what I'm doing wrong. The following sample program fails with "badkey" - any ideas what is wrong?<div><br></div><div>(Erlang R16B)<br><br><div>-module(test_crypto).</div><div><br></div><div>-compile(export_all).</div><div><br></div><div>-include_lib("public_key/include/public_key.hrl").</div><div>-include_lib("eunit/include/eunit.hrl").</div><div><br></div><div>-define(TEST_DATA, <<"Test">>).</div><div><br></div><div>sign_test() -></div><div>    application:start(crypto),</div><div>    [Entry] = public_key:pem_decode(test_priv_key()),</div><div>    PrivateKey = public_key:pem_entry_decode(Entry),</div><div>    ?assert(is_record(PrivateKey, 'RSAPrivateKey')),</div><div><br></div><div>    #'RSAPrivateKey'{ modulus = N, publicExponent = E, privateExponent = D} = PrivateKey,</div><div><br></div><div>    io:format("Decoded: ~p~n  N=~p~n  E=~p~n  D=~p~n", [PrivateKey, N, E, D]),</div><div><br></div><div>    Signed = public_key:sign(?TEST_DATA, md5, PrivateKey),</div><div><br></div><div>    io:format("signed: ~p~n", [Signed]),</div><div><br></div><div>    error(force_failure).</div><div><br></div><div><br></div><div>test_priv_key() -></div><div>    iolist_to_binary([</div><div>        "-----BEGIN RSA PRIVATE KEY-----", $\n,</div><div>        "MIGqAgEAAiEAsfQgn015/7zGm2Hy0wh28voJTcDM5vXbQIb47IRvPkECAwEAAQIg", $\n,</div><div>        "Ibi0Udu5qaeZLyqxfCIXgATS+x7mDA0tMJXtheJCkUECEQDWLuQfQZJEJOZq05eM", $\n,</div><div>        "EFypAhEA1LJxrD0/ZVA8u7FSlV/72QIRALr5BkXO3RvFxqv8+K/Z3LkCEBQpJeUx", $\n,</div><div>        "mQS9akRQNoe6JWECEDR94PUjkOWSkZrCwpsXbbg=", $\n,</div><div>        "-----END RSA PRIVATE KEY-----", $\n]).</div><div><br></div><div>test_pub_key() -></div><div>    iolist_to_binary([</div><div>        "-----BEGIN PUBLIC KEY-----", $\n,</div><div>        "MDwwDQYJKoZIhvcNAQEBBQADKwAwKAIhALH0IJ9Nef+8xpth8tMIdvL6CU3AzOb1", $\n,</div><div>        "20CG+OyEbz5BAgMBAAE=", $\n,</div><div>        "-----END PUBLIC KEY-----", $\n]).</div><div><br></div></div>