# Openssl encrypt / decrypt question

**URL:** <https://www.ruby-forum.com/t/openssl-encrypt-decrypt-question/53478>\
**Category:** Ruby\
**Created:** [February 7, 2006, 4:06am UTC](https://www.ruby-forum.com/t/openssl-encrypt-decrypt-question/53478 "2006-02-07T04:06:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aynon\_Ehmouse](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c5a1d2/32.png) [@Aynon\_Ehmouse](https://www.ruby-forum.com/u/Aynon_Ehmouse)\
**Post date:** [February 7, 2006, 4:06am UTC](https://www.ruby-forum.com/t/openssl-encrypt-decrypt-question/53478/1 "2006-02-07T04:06:12Z")

</div>

Hi all,

I’m attempting to encrypt and decrypt a string, and am having some  
issues with the decrypt side. I’m using ruby 1.8.4 on gentoo.

My code looks something like this:

-----code starts-----  
require ‘openssl’

# Load up sender key pair

from\_cert = X509::Certificate.new(File::read(“send\_cert.pem”))  
from\_key = PKey::RSA.new(File::read(“send\_key.pem”))

# Load up recipient key pair (

to\_cert = X509::Certificate.new(File::read(“rcpt\_cert.pem”))  
to\_key = PKey::RSA.new(File::read(“rcpt\_key.pem”))

# Basic data

data = “test this out”

# Sign message

from\_signed = PKCS7::sign(from\_cert, from\_key, data, [], PKCS7::BINARY)

# The next line emits the data correctly

print from\_signed.data

# Encrypt signed message

from\_encrypted = PKCS7::encrypt([to\_cert], from\_signed.to\_der,  
Cipher::Cipher::new(“DES3”), PKCS7::BINARY)

# This is the data format I send to the recipient.

from\_message = from\_encrypted.to\_pem

# Pretend that I’m receiving this on the ‘to’ side.

to\_encrypted = PKCS7::PKCS7.new(from\_message)  
to\_decrypted = PKCS7::PKCS7.new(to\_encrypted.decrypt(to\_key, to\_cert,  
PKCS7::BINARY))

# See who signed the message:

to\_decrypted.signers.each { |signer|  
print “#{signer.name}\n”  
}

# This _should_ emit the data, but instead prints nothing.

print “#{to\_decrypted.data}\n”  
-----code ends-----

So… I know that the encryption works, because a third party can  
decrypt messages I send them. However, in this testing setup, I can see  
who the signer is, but there’s no data (output is ‘nil’).

Anyone know what I’m doing wrong?

Thanks,

Aynon
