13

I'm working in Base64 in Ruby on Rails and have a question:

For example how would the following be Base64 encoded / decoded?

<a href="myroute?id=<%[email protected]%>"> MY LINK</a>

3 Answers 3

18

To create Base64 try this:

def create_base_64 
  begin 
    #for create 
    a = 1
    b = Base64.encode64(a.to_s)
    # for decoding
    c = Base64.decode64(b)
    puts c
  rescue Exception => e 
    puts e 
  end 
end

For more security try to broke it. For example:

b[1] = b[1] << SecureRandom.hex(1)

And other action, you recived the param b:

b = params[:b]
b[2]=""
b[2]=""
c = base64.decode64(b.to_s)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, i work with example... but your broke the base64 and reassembled again the base64
do not rely on security through obscurity
8
ActiveSupport::Base64.encode64(`whateveryouwanttoencode`)

You don't really provide enough detail about what exactly you want to do. . . The method I provided is how you accomplish Base64 encoding though, is that all you need?

Some documentation: http://www.rubydoc.info/docs/rails/3.0.0/ActiveSupport/Base64

3 Comments

thanks, but first answer for @luis is interesting...per security
@pedroooo No problem, his post wasn't here when I was writing this. Just trying to get you going in the right direction.
For me works just Base64.encode64, without ActiveSupport.
0

I don't understand what you are trying the achieve with your anchor tag example. But to decode a base64 in ruby you would do this:

base64_string = "67382hfuisab3y289321787123890......"

decoded_data = StringIO.new(Base64.decode64(base64_string))

and then do whatever you want with decoded_data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.