Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Javascript - Python RSA encryption interoperability

Question posted by: Evren Esat Ozkan (Guest) on July 4th, 2008 01:45 PM
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,
marek.rocki@wp.pl's Avatar
marek.rocki@wp.pl
Guest
n/a Posts
July 4th, 2008
03:25 PM
#2

Re: Javascript - Python RSA encryption interoperability
Evren Esat Ozkan napisał(a):
Quote:
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

Evren Esat Ozkan's Avatar
Evren Esat Ozkan
Guest
n/a Posts
July 17th, 2008
12:55 PM
#3

Re: Javascript - Python RSA encryption interoperability
Hi,

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

Thanks again,
Evren


On Jul 4, 6:19*pm, marek.ro...@wp.pl wrote:
Quote:
Evren Esat Ozkan napisał(a):
>
>
>
>
>
Quote:
Hello,

>
Quote:
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.

>
Quote:
My experiments could be seen at:http://dpaste.com/hold/60741/

>
Quote:
* JavaScript RSA Library:http://www.ohdave.com/rsa/

>
Quote:
Python libraries which I tried;
* PyCrtypo:http://www.amk.ca/python/code/crypto.html
* rsa library fromhttp://www.stuvel.eu/rsa

>
Quote:
How could I create the same results with the JS library in Python.

>
Quote:
Any help would be appreciated

>
Quote:
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



Paul Rubin's Avatar
Paul Rubin
Guest
n/a Posts
July 17th, 2008
03:25 PM
#4

Re: Javascript - Python RSA encryption interoperability
Evren Esat Ozkan <sleytr@gmail.comwrites:
Quote:
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.

 
Not the answer you were looking for? Post your question . . .
190,182 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors