rsa encryption/decryption example code doesn't work

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

rsa encryption/decryption example code doesn't work

Chad Depue
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


________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Reply | Threaded
Open this post in threaded view
|

Re: rsa encryption/decryption example code doesn't work

Richard Andrews-5
On Sun, Jun 28, 2009 at 7:52 AM, Chad DePue<[hidden email]> wrote:

> 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.

For asymmetric encryption, one generally encrypts with one key and
decrypts with the other. In this case you are encrypting with PrivKey
so you will need to decrypt with PubKey.

--
  Rich

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Reply | Threaded
Open this post in threaded view
|

Re: rsa encryption/decryption example code doesn't work

Chad Depue
Thanks, Rich - that did it. Here's the final code.

-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),
   PlainText = "now is the time for all good men to come to the aid of  
their country",
   PrivKey = [crypto:mpint(E), crypto:mpint(N), crypto:mpint(D)] ,
   PubKey = [crypto:mpint(E), crypto:mpint(N)],
   Foo =  
crypto:rsa_private_encrypt(PlainText,PrivKey,rsa_pkcs1_padding),
   Bar = crypto:rsa_public_decrypt(Foo,PubKey,rsa_pkcs1_padding),
   PlainText == binary_to_list(Bar).

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


On Jun 28, 2009, at 7:09 AM, Richard Andrews wrote:

> On Sun, Jun 28, 2009 at 7:52 AM, Chad DePue<[hidden email]>  
> wrote:
>> 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.
>
> For asymmetric encryption, one generally encrypts with one key and
> decrypts with the other. In this case you are encrypting with PrivKey
> so you will need to decrypt with PubKey.
>
> --
>  Rich


________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org