Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pycrypto RSA ciphertext to string back to ciphertext issue

3 views
Skip to first unread message

Jordan Apgar

unread,
Feb 11, 2010, 8:27:58 PM2/11/10
to
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.

MRAB

unread,
Feb 11, 2010, 8:52:47 PM2/11/10
to pytho...@python.org

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'

0 new messages