On Fri, Aug 03, 2007 at 02:03:33PM +0100, Brian Candler wrote:
> $ cat enc.rb
> require 'openssl'
> key = OpenSSL::PKey::RSA.new(File.read('key.pub'))
> raise "Not public key" unless key.public?
> $stdout.write key.public_encrypt($stdin.read)
> 
> $ echo "Hello, world" | ruby enc.rb >data.bin
> 
> $ ls -l data.bin
> -rw-r--r-- 1 candlerb candlerb 256 2007-08-03 14:00 data.bin
> 
> $ openssl rsautl -decrypt -in data.bin -inkey key.priv -passin pass:abcd
> Hello, world

Of course, using native RSA is extremely slow for large amounts of data. In
that case you should be generating a random session key, encrypting the data
with a symmetric cipher, and then encrypting the session key with RSA.

All this is exactly what PGP/GPG does for you (or S/MIME)

Regards,

Brian.