JoaoMorais
JoaoMorais

Reputation: 3

RoR Encryption and Decryption for query string

I've been searching for the past few days about encrypting some data to use on the URL.

I basically have 3 strings and i need to encrypt then on a unique token.

eg: code: '12345678' email: '[email protected]' name: 'nameTest'

This will be join together as code%email%name and i need to encrypt them.

What's the best way to do it as i need to pass the encrypted string on the URL for the other server to decrypt? The algorithms i've used all put some form of '/\=' and i guess that probably may cause problems

Thanks for the help

Upvotes: 0

Views: 1325

Answers (1)

SunnyMagadan
SunnyMagadan

Reputation: 1849

In 2019, URI.encode is obsolete and should not be used

If you want safely put encrypted string into url without problems with special characters then you could use CGI.escapeor ERB::Util.url_encode for this purpose.

require 'erb'

ERB::Util.url_encode("Hello world")
#=> "Hello%20world"

Rails will decode automatically when receiving

If you want something simple that encodes and hide raw data which could be decoded somewhere later and verified then you could use MessageVerifier provided by active support:

@verifier = ActiveSupport::MessageVerifier.new('s3Krit')
cookies[:remember_me] = @verifier.generate([@user.id, 2.weeks.from_now])
id, time = @verifier.verify(cookies[:remember_me])

http://api.rubyonrails.org/classes/ActiveSupport/MessageVerifier.html http://ngauthier.com/2013/01/rails-unsubscribe-with-active-support-message-verifier.html

If you want true encryption then you could look into such project. It uses OpenSSL: http://rocketjob.github.io/symmetric-encryption/

Upvotes: 1

Related Questions