Javascript - Python RSA encryption interoperability

marek.rocki at wp.pl marek.rocki at wp.pl
Fri Jul 4 11:19:01 EDT 2008


Evren Esat Ozkan napisał(a):
> Hello,
>
> I'm trying to encrypt a string with RSA. But it needs to be compitable
> with Dave's JavaScript RSA implementation*. I'm already read and tried
> lots of different things about RSA and RSA in Python. But could not
> produce the same result with the javascript library.
>
> My experiments could be seen at: http://dpaste.com/hold/60741/
>
> * JavaScript RSA Library: http://www.ohdave.com/rsa/
>
> Python libraries which I tried;
> * PyCrtypo: http://www.amk.ca/python/code/crypto.html
> * rsa library from http://www.stuvel.eu/rsa
>
> How could I create the same results with the JS library in Python.
>
>
> Any help would be appreciated
>
> Evren,

It seems that this Javascript is doing weird things to its input,
namely processing it in reverse. Try encrypting ciphertext[::-1]
instead of just ciphertext.

public_modulus_hex = '9F2E..snip..4BC7'
public_exponent_hex = '10001'
public_modulus = int(public_modulus_hex, 16)
public_exponent = int(public_exponent_hex, 16)

def encrypt(plaintext_text):
	# Beware, plaintext must be short enough to fit in a single block!
	plaintext = int(plaintext_text.encode('hex'), 16)
	ciphertext = pow(plaintext, public_exponent, public_modulus)
	return '%X' % ciphertext # return hex representation

print encrypt('12345')
print encrypt('12345'[::-1]) # Will return the value compatible with
JS output

Regards,
Marek



More information about the Python-list mailing list