# RSA Encryption Decryption

**URL:** <https://www.ruby-forum.com/t/rsa-encryption-decryption/58083>\
**Category:** Ruby\
**Created:** [April 18, 2006, 10:43am UTC](https://www.ruby-forum.com/t/rsa-encryption-decryption/58083 "2006-04-18T10:43:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![name\_pipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b5a626/32.png) [@name\_pipe](https://www.ruby-forum.com/u/name_pipe)\
**Post date:** [April 18, 2006, 10:43am UTC](https://www.ruby-forum.com/t/rsa-encryption-decryption/58083/1 "2006-04-18T10:43:40Z")

</div>

Hi,

I would like to know how can we do encryption and decryption using  
public  
and private keys in ruby. Does ruby any RSA libraries for the same. A  
small  
example would be quite helpful.

Thanks

---

<div class="post-metadata">

**Author:** ![name\_pipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b5a626/32.png) [@name\_pipe](https://www.ruby-forum.com/u/name_pipe)\
**Post date:** [April 18, 2006, 11:14am UTC](https://www.ruby-forum.com/t/rsa-encryption-decryption/58083/2 "2006-04-18T11:14:34Z")

</div>

Hi,

name pipe wrote:

> Hi,
> 
> I would like to know how can we do encryption and decryption using public  
> and private keys in ruby. Does ruby any RSA libraries for the same. A small  
> example would be quite helpful.
> 
> Thanks

take a look at the openssl bindings for ruby:

require “openssl”  
include OpenSSL  
include X509  
include PKey

cert = Certificate.new(File.read(“server.cer”))  
private\_key = RSA.new(File.read(“server.cer.key”))  
message = “This is a test!”

puts("— Start private key encryption / public key decryption")  
crypt = private\_key.private\_encrypt(message)  
puts(“Plain text ---------”)  
puts(message)  
puts(“Encrypted text------”)  
puts(crypt)  
puts(“Decryted text-------”)  
puts(cert.public\_key.public\_decrypt(crypt))

puts("— Start public key encryption / private key decryption")  
crypt = cert.public\_key.public\_encrypt(message)  
puts(“Encrypted text------”)  
puts(crypt)  
puts(“Decryted text-------”)  
puts(private\_key.private\_decrypt(crypt))

Regards,  
Roland

---

<div class="post-metadata">

**Author:** ![name\_pipe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/b5a626/32.png) [@name\_pipe](https://www.ruby-forum.com/u/name_pipe)\
**Post date:** [April 18, 2006, 11:36am UTC](https://www.ruby-forum.com/t/rsa-encryption-decryption/58083/3 "2006-04-18T11:36:10Z")

</div>

Thanks, but its bit confusing with new include files and stuff. Any  
simple  
eg. which creates RSA public, private keys and encrypts/decrypts data ?
