Standard Library Essentials
Base64 Encoding
Base64 converts ordinary text into an ASCII-safe encoded string and back again.
Base64 Encoding
base64_encoding.rb
require "base64"
text =
encoded = Base64.strict_encode64(text)
decoded = Base64.decode64(encoded)
round_trip = decoded == text
puts "text=#{text}"
puts "encoded=#{encoded}"
puts "round_trip=#{round_trip}"
require "base64"
text =
encoded = Base64.strict_encode64(text)
decoded = Base64.decode64(encoded)
round_trip = decoded == text
puts "text=#{text}"
puts "encoded=#{encoded}"
puts "round_trip=#{round_trip}"
require "base64"
text =
encoded = Base64.strict_encode64(text)
decoded = Base64.decode64(encoded)
round_trip = decoded == text
puts "text=#{text}"
puts "encoded=#{encoded}"
puts "round_trip=#{round_trip}"
base64
Base64 is an encoding format, not encryption; decoding returns the original text.