0

How to encrypt and decrypt the url using blowfish in ruby?

ex: url= http://localhost:3000?username=vam&paswd=1234&street=hyd&contact=999999999&company=raymarine&city=hyd&state=UP&country=ZP&zip_code=543211

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vam
  • 123
  • 1
  • 2
  • 9
  • What are you trying to accomplish? Encrypting an url by itself does solve no problem, since you need to decrypt before using, and need to store the key somewhere. – CodesInChaos Mar 07 '11 at 10:07
  • And why blowfish and not some newer cypher such as AES(Rijndael) or TwoFish? – CodesInChaos Mar 07 '11 at 10:08

2 Answers2

3

Shamelessly stolen and adapted, this appears to be what you want.

require 'rubygems'
require 'crypt/blowfish'
require 'base64'

plain = "http://localhost:3000?username=vam&paswd=1234&street=hyd&contact=999999999&company=raymarine&city=hyd&state=UP&country=ZP&zip_code=543211"
puts plain

blowfish = Crypt::Blowfish.new("A key up to 56 bytes long")
enc = blowfish.encrypt_string(plain)

mimed = Base64.encode64(enc)

puts mimed


$ ruby blowfish.rb 
http://localhost:3000?username=vam&paswd=1234&street=hyd&contact=999999999&company=raymarine&city=hyd&state=UP&country=ZP&zip_code=543211
K9XLp7LmidHZnhQi1i93Lfi1qV4pWFzksnOkNDt/VqyWdZ0OA+K+0soWl7OZ
bNOi17OLIkjhMzHx4Av+h1SL7GP9aletclQGO6XoW2Cge0JweChlj3HXjZT1
fQ6WIqw0zVRaWmqvk1sTqKgvNhy7XPS99RPuX8JdVP87rreklam2LJC97sPh
pu5W9U/lhW7VeRm1HgbI+M0=

Of course, if you need the encrypted contents to serve as an URL, then prepend http://localhost:3000/foo?q= to the encrypted contents, and provide a /foo/q GET handler that can decrypt the string and do whatever it is you need to do with it.

Community
  • 1
  • 1
sarnold
  • 102,305
  • 22
  • 181
  • 238
0

The Crypt library does not work for Ruby 1.9 and later. You can use this gist instead. It requires no gems: https://gist.github.com/kajic/5686064

url = 'http://localhost:3000?username=vam&paswd=1234&street=hyd&contact=999999999&company=raymarine&city=hyd&state=UP&country=ZP&zip_code=543211'
encrypted_url = Cipher.encrypt_base64('your secret key', url)
Robert Kajic
  • 8,689
  • 4
  • 44
  • 43