简体   繁体   English

如何在Ruby on Rails中使用Base64编码?

[英]How can I use Base64 encoding in Ruby on Rails?

I'm working in Base64 in Ruby on Rails and have a question: 我在Ruby on Rails的Base64中工作并有一个问题:

For example how would the following be Base64 encoded / decoded? 例如,以下将如何进行Base64编码/解码?

<a href="myroute?id=<%=@client.id%>"> MY LINK</a>

To create Base64 try this: 要创建Base64,请尝试以下操作:

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:

b = params[:b]
b[2]=""
b[2]=""
c = base64.decode64(b.to_s)
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? 我提供的方法是如何完成Base64编码,这就是你需要的吗?

Some documentation: http://www.rubydoc.info/docs/rails/3.0.0/ActiveSupport/Base64 一些文档: http//www.rubydoc.info/docs/rails/3.0.0/ActiveSupport/Base64

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: 但要在ruby中解码base64,你会这样做:

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

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

and then do whatever you want with decoded_data 然后用decode_data做你想做的事

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM