Pycrypto RSA ciphertext to string back to ciphertext issue

MRAB python at mrabarnett.plus.com
Thu Feb 11 20:52:47 EST 2010


Jordan Apgar wrote:
> Hey all,
> I'm trying to convert the encrypted data from RSA to a string for
> sending over xmlrpc and then back to usable data.  Whenever I decrypt
> I just get junk data.  Has anyone else tried doing this?  Here's some
> example code:
> 
> from Crypto.PublicKey import RSA
> from Crypto import Random
> 
> key = RSA.generate(384, Random.new().read)
> l = str(key.encrypt("dog",""))
> l = stringToTuple(l)
> l = key.decrypt(tuple(l))
> print l
> 
> string to tuple:
> def stringToTuple(string):
>     if string[0] + string[-1] == "()":
>         items = string[1:-1]
>         items = items.split(',')
>         return items
>     else:
>         raise ValueError("Badly formatted string (missing brackets).")
> 
> thanks for any help, I've been messing with this for days and have
> come up dry.

I don't know what you're trying to do with converting tuples to
bytestrings and back, but this works:

 >>> key = RSA.generate(384, Random.new().read)
 >>> encrypted = key.encrypt("dog", "")[0]
 >>> encrypted
"NB\xe9\xcf\x88\xf8b.\x81X{\x12\xeaH\x03\xe9\xc4\xd6\xdb\x00lS\r\xc9in|\xa5\xb1'
$\x90_\xc5t$\xd0\xc40-p\x8b\xd0\x95\xdb\xa6\xf7\xc2"
 >>> key.decrypt(encrypted)
'dog'




More information about the Python-list mailing list