Skip to content
Advertisement

rsa encryption with javascrypt decrypt in python

I’m trying to encrypt using Wix-Velo in javascript using hybrid-crypto-js, and decrypt using python using PKCS1_v1_5 and I’m getting incorrect length error.

This is my code in Javascript:

JavaScript

In python:

JavaScript

Advertisement

Answer

The easiest way to explain this is with an example. The following key pair is used for the example:

JavaScript

For reasons of space, a 1024 bits key is used. Note, however, that for security reasons RSA keys with a size of at least 2048 bits must be used in practice today.

One possible output of the posted JavaScript code using this key pair is:

JavaScript

The first element is the version, the second the IV, the third the AES key encrypted with RSA/OAEP and the fourth the ciphertext encrypted with AES-256/CBC, s. Hybrid Crypto JS.

To decrypt the key, the posted Python/PyCryptodome code can be used, changing the padding from PKCS#1 v1.5 to OAEP and using the above RSA key and ciphertext:

JavaScript

Now the actual ciphertext can be decrypted:

JavaScript

Note that the IV has a length of 32 bytes, of which only the first 16 bytes (corresponding to the AES block size) are used for AES.

For completeness: The fingerprint e7:de...17:f8 used in the ciphertext of the JavaScript code is the SHA1 hash of the public key in PKCS#1 format, DER encoded.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement