开发者

blowfish encrypted url in ruby

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=开发者_StackOverflow中文版raymarine&city=hyd&state=UP&country=ZP&zip_code=543211


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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜