help for using Python rsa

Dave Angel davea at davea.name
Sat Feb 7 01:47:38 EST 2015


On 02/07/2015 12:14 AM, mhr1224 at gmail.com wrote:
> I use this code to create public and private keys:
>          import rsa
>          (pubkey, privkey) = rsa.newkeys(512)
>
> And then convert it to PEM format:
>          exppub =pubkey.save_pkcs1(format='PEM')
>          exppriv = privkey.save_pkcs1(format='PEM')
>
> When i encrypt massage with this keys:
>          message = 'hello Bob!'
>          crypto = rsa.encrypt(message, pubkey)
>
> encrypted massage is like this:
> "@\xc4\xb2\x14r\xf1x\xb8\xb2\t;\x9a:\x1dl\x11\xe2\x10\xa9E\xee\x8b\xac\xd0\xd3Y\xfb}\xd9@\xdd\x0c\xa5\xd2\xfc1\xd6\x06\xf0\xb8\x944\xe1\xc2r\xe5anyq\xac\xdfh\xeb\x10\x80\x98\xa1\xee'\xe6hpi"
>
> and i know it should be like this:
> SEcPB1mYNrfeE4zP4RI3z2K4Rh9HDNfPhuF28IyxHFjEOJ9Z+1zdIwPF0jsJGQDJyKpAju7dcYueHHXXeH8d+w==
>
> How can i change the format of this?
>

I can't help with the crypto, but I can help make it more likely that 
someone will be able to help.

1) Always specify the Python version and OS when you start a new thread.

2) When you show data, show how you're displaying that data.  You have 
two strings, but the one is apparently a repr() of a string, while the 
other is not.  And you don't specify how you "know it should be like 
this."  Are you running some other encryption utility that gives you 
that message?  Be specific.  Something like:

     crypto = rsa.enc....
     print( repr(crypto) )

displays as
"@\xc4\xb2\x...

but running    mycrypto.exe  key="xyzyzzyzyzy"  data="hello Bob!"
displays as
SEcPB1mYNrf...

Have you tried decrypting the encrypted message, using the privkey?  Did 
it in fact produce the original?  Could it be that the error is in how 
you're transporting the public key to your reference program?

It might also be useful to show the types of each of the variables.  For 
example, you probably cannot encrypt text, but only binary.  So you 
might need to explicitly encode the text "hello Bob!" before encrypting it.

-- 
DaveA



More information about the Python-list mailing list