# Long string in crypting

**URL:** <https://www.ruby-forum.com/t/long-string-in-crypting/166378>\
**Category:** Rails\
**Created:** [April 24, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/long-string-in-crypting/166378 "2009-04-24T14:46:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 24, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/long-string-in-crypting/166378/1 "2009-04-24T14:46:29Z")

</div>

I use a solution to crypt a string that I found using OpenSSL. But the  
crypted string becomes very long, too long for a varchar 255 to hold it.  
What can I do to make it shorter? Or should I just use text as column in  
the mysql db?

public\_key\_file = ‘lib/public.pem’  
public\_key = OpenSSL::PKey::RSA.new(File.read(public\_key\_file))  
@encrypted\_string = Base64.encode64(public\_key.public\_encrypt(string))

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 24, 2009, 3:06pm UTC](https://www.ruby-forum.com/t/long-string-in-crypting/166378/2 "2009-04-24T15:06:22Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> I use a solution to crypt a string that I found using OpenSSL. But the  
> crypted string becomes very long, too long for a varchar 255 to hold it.  
> What can I do to make it shorter? Or should I just use text as column in  
> the mysql db?
> 
> public\_key\_file = ‘lib/public.pem’  
> public\_key = OpenSSL::PKey::RSA.new(File.read(public\_key\_file))  
> @encrypted\_string = Base64.encode64(public\_key.public\_encrypt(string))

It appears that you are using public/private key encryption which uses  
much longer keys than symmetric encryption by necessity. Asymmetric  
encryption is also very slow and more processor intensive than that of  
symmetric encryption. The key advantage of asymmetric is that it  
separates the public and private keys to solve the key exchange problem.

Symmetric encryption has advantage in almost every other way over  
asymmetric. It’s able to use much shorter keys for equal or better  
quality encryption, it’s very much faster and more efficient.

Take for example SSL, which uses asymmetric (public/private keys) to  
encrypt only one small bit of data. This small bit is the shared  
symmetric key that gets exchanged between the client and server. Once  
both sides have this shared key then all remaining data for the session  
gets encrypted with a symmetric cypher algorithm.

That’s a long winded way to say, “Do you need the secure key exchange,  
or will a prearranged shared key work for your case?” If not then  
switching to a symmetric algorithm will be smaller, faster and way more  
efficient.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 24, 2009, 3:24pm UTC](https://www.ruby-forum.com/t/long-string-in-crypting/166378/3 "2009-04-24T15:24:40Z")

</div>

Robert W. wrote:

> It appears that you are using public/private key encryption which uses  
> much longer keys than symmetric encryption by necessity. Asymmetric  
> encryption is also very slow and more processor intensive than that of  
> symmetric encryption. The key advantage of asymmetric is that it  
> separates the public and private keys to solve the key exchange problem.
> 
> Symmetric encryption has advantage in almost every other way over  
> asymmetric. It’s able to use much shorter keys for equal or better  
> quality encryption, it’s very much faster and more efficient.
> 
> Take for example SSL, which uses asymmetric (public/private keys) to  
> encrypt only one small bit of data. This small bit is the shared  
> symmetric key that gets exchanged between the client and server. Once  
> both sides have this shared key then all remaining data for the session  
> gets encrypted with a symmetric cypher algorithm.
> 
> That’s a long winded way to say, “Do you need the secure key exchange,  
> or will a prearranged shared key work for your case?” If not then  
> switching to a symmetric algorithm will be smaller, faster and way more  
> efficient.

I need a somewhat simple encrypting method of storing data that can be  
decrypted. This was the only thing I could get to work. When I try  
EzCrypto I run into an error “uninitialized constant” or something.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 24, 2009, 4:06pm UTC](https://www.ruby-forum.com/t/long-string-in-crypting/166378/4 "2009-04-24T16:06:45Z")

</div>

PÃ¥l BergstrÃ¶m wrote:

> I need a somewhat simple encrypting method of storing data that can be  
> decrypted. This was the only thing I could get to work. When I try  
> EzCrypto I run into an error “uninitialized constant” or something.

What I would recommend is switching from RSA (public-key encryption) to  
AES (symmetric cypher).

Try this code:

[http://snippets.dzone.com/posts/show/576](http://snippets.dzone.com/posts/show/576)

I have no idea if it work I just found by Googling.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [April 24, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/long-string-in-crypting/166378/5 "2009-04-24T16:14:52Z")

</div>

Robert W. wrote:

> What I would recommend is switching from RSA (public-key encryption) to  
> AES (symmetric cypher).
> 
> Try this code:
> 
> [http://snippets.dzone.com/posts/show/576](http://snippets.dzone.com/posts/show/576)
> 
> I have no idea if it work I just found by Googling.

Great. I’ll try this.
