rsa encryption/decryption example code doesn't work

Chad DePue <>
Sat Jun 27 23:52:33 CEST 2009


I have written this sample rsa encryption code and it doesn't decrypt  
properly.  I generated the test private key using ssh-keygen. any  
ideas? thanks in advance...

chad


-module(crypttest).
-include_lib("public_key/include/public_key.hrl").
-export([testcase/0]).

testcase() ->
   {ok,[Entry]} = public_key:pem_to_der("./test_private_key"),
   {ok,{'RSAPrivateKey', 'two-prime', N , E, D, _P, _Q, _E1, _E2, _C,  
_Other}} =  public_key:decode_private_key(Entry),
   PrivKey = [crypto:mpint(E), crypto:mpint(N), crypto:mpint(D)] ,
   Foo = crypto:rsa_private_encrypt("now is the time for all good men  
to come to the aid of their country",PrivKey,rsa_pkcs1_padding),
   Bar = crypto:rsa_private_decrypt(Foo,PrivKey,rsa_pkcs1_padding),
   Foo == Bar.

8> c(crypttest).
{ok,crypttest}
9> crypttest:testcase().

** exception error: decrypt_failed
      in function  crypto:rsa_private_decrypt/3
         called as  
crypto:rsa_private_decrypt 
(<<141,9,170,241,244,57,128,44,178,223,162,109,80,60,225,
                                                 
19,84,133,118,45,2,19,36,111,58,236,38,98,...>>,
                                              [<<0,0,0,1,35>>,
                                                
<<0,0,1,1,0,162,240,42,103,51,152,51,159,139,165,248,162,
                                                  
95,223,62,6,52,130,119,213,...>>,
                                                
<<0,0,1,1,0,158,72,99,180,181,199,13,147,172,58,212,69,
                                                  
246,187,155,86,124,38,248,...>>],
                                              rsa_pkcs1_padding)
      in call from crypttest:testcase/0



More information about the erlang-questions mailing list