rogy
rogy

Reputation: 450

RSA key pair encryption/decryption between Ruby and JavaScript?

I've been working on something that encrypts a piece of data in Ruby using OpenSSL and decrypts the data in JavaScript using the JavaScript Forge library.

The (unsecure, but this is for research only) method of distributing the keys works fine, i.e., I can generate the keys in Ruby and put them into PEM format, pass the PEM to JavaScript and recover a working key in JavaScript, however encrypting the same string using the same key gives two different results, so encoding/decoding between the two obviously don't work.

Is there a good way of doing this?

The other problem is that passing a binary data string from Ruby to JavaScript without getting an incompatible type error requires some sort of conversion, for example, converting the encrypted data to hex, but working with this kind of information in the forge library is proving difficult.

Upvotes: 2

Views: 1236

Answers (1)

rogy
rogy

Reputation: 450

I actually solved my own problem, in my original question

I just wasn't doing it right

For future reference, if you are using the ruby openssl RSA encryption and want to pass it to javascript, I recommend using the https://github.com/digitalbazaar/forge library for the JS side,

second, convert encrypted strings to hex using .unpack('H*')

Forge PKI library has a .hexToBytes() function, you can then use the forge .decrypt method and get back what you started

Upvotes: 2

Related Questions