Javascript - Python RSA encryption interoperability

  • Thread starter Evren Esat Ozkan
  • Start date
E

Evren Esat Ozkan

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,
 
M

marek.rocki

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
 
E

Evren Esat Ozkan

Hi,

Thank you very much. Your code is worked like a charm and saved my
honeymoon :)

Thanks again,
Evren


Evren Esat Ozkan napisa³(a):




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/
Python libraries which I tried;
* PyCrtypo:http://www.amk.ca/python/code/crypto.html
* rsa library fromhttp://www.stuvel.eu/rsa
How could I create the same results with the JS library in Python.
Any help would be appreciated

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
 
P

Paul Rubin

Evren Esat Ozkan said:
I'm trying to encrypt a string with RSA. But it needs to be compitable
with Dave's JavaScript RSA implementation*.

What exactly are you trying to do? That Javascript implementation
looks like bad news. If you're trying to secure a web page, use SSL,
don't mess around with trying to encrypt it with Javascript.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top