I am trying to encrypt text locally in Ruby and decrypt it on the server with Python.
I post the encrypted phrase like so:
require "base64"
require "openssl"
public_key = OpenSSL::PKey::RSA.new File.read './publickey.pem'
encrypted = public_key.public_encrypt "Hello everyone. This is a secret phrase."
uri = URI('#...')
params = { "passage" => Base64.encode64(encrypted) }
response = HTTParty.post(
uri,
:body => params.to_json,
:headers => {'Content-Type' => 'application/json'}
)
My Python looks like this:
f = open('./privatekey.pem', 'rb') # generated with ruby (OpenSSL::PKey::RSA.new 2048)
private_key = RSA.importKey(f.read())
f.close()
return private_key.decrypt(encrypted)
This results in the error:
raise NotImplementedError("Use module Crypto.Cipher.PKCS1_OAEP instead")
NotImplementedError: Use module Crypto.Cipher.PKCS1_OAEP instead
So, I use PKCS1_OAEP
:
#...
cipher_priv = PKCS1_OAEP.new(private_key)
decrypted = cipher_priv.decrypt(encrypted)
This results in the error:
raise ValueError("Incorrect decryption.")
ValueError: Incorrect decryption.
…and I'm sent round in circles. If not PKCS1_OAEP
what is the correct decryption?
How do I decrypt the text sent from Ruby to the Python server?