From: snacktime Date: 2006-08-31T07:48:53+09:00 Subject: Re: encryption library Here is an example of one way to use public key (asymmetric) encryption using openssl. Requires an ssl certificate/key pair, but only the certificate is required to encrypt. require 'openssl' keyfile = 'test.key' certfile = 'test.crt' data = "this is a test" cert = OpenSSL::X509::Certificate.new(File.read(certfile)) key = OpenSSL::PKey::RSA.new(File.read(keyfile)) cipher = OpenSSL::Cipher::AES.new("128-CBC") tmp = OpenSSL::PKCS7.encrypt([cert], data, cipher, OpenSSL::PKCS7::BINARY) p7 = OpenSSL::PKCS7::PKCS7.new(tmp.to_der) ## Data will be stored as string so emulate that here p7s = p7.to_s ## Create pkcs7 object out of pkcs7 data p7 = OpenSSL::PKCS7::PKCS7.new(p7s) dec = p7.decrypt(key,cert) print dec