Base64 converts ordinary text into an ASCII-safe encoded string and back again.

base64 Base64 is an encoding format, not encryption; decoding returns the original text.

Base64 Encoding

text
base64_encoding.rb
Replay: real traced execution (multi-file project)
require "base64"

text = "Ruby data"
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 = "Trace data"
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 = "Small note"
encoded = Base64.strict_encode64(text)
decoded = Base64.decode64(encoded)

round_trip = decoded == text

puts "text=#{text}"
puts "encoded=#{encoded}"
puts "round_trip=#{round_trip}"
  1. text ← Ruby data, encoded ← UnVieSBkYXRh, decoded ← "Ruby data"

    1require "base64"23text→ Ruby data = "Ruby data"  #@text="Trace data", "Small note"4encoded→ UnVieSBkYXRh = Base64.strict_encode64(text)UnVieSBkYXRh5decoded→ "Ruby data" = Base64.decode64(encoded)"Ruby data"67round_trip→ true = decoded"Ruby data" == textRuby data89puts "text=#{textRuby data}"10puts "encoded=#{encodedUnVieSBkYXRh}"11puts "round_trip=#{round_triptrue}"
    outputtext=Ruby data
    encoded=UnVieSBkYXRh
    round_trip=true
  1. text ← Trace data, encoded ← VHJhY2UgZGF0YQ==, decoded ← "Trace data"

    1require "base64"23text→ Trace data = "Trace data"4encoded→ VHJhY2UgZGF0YQ== = Base64.strict_encode64(text)VHJhY2UgZGF0YQ==5decoded→ "Trace data" = Base64.decode64(encoded)"Trace data"67round_trip→ true = decoded"Trace data" == textTrace data89puts "text=#{textTrace data}"10puts "encoded=#{encodedVHJhY2UgZGF0YQ==}"11puts "round_trip=#{round_triptrue}"
    outputtext=Trace data
    encoded=VHJhY2UgZGF0YQ==
    round_trip=true
  1. text ← Small note, encoded ← U21hbGwgbm90ZQ==, decoded ← "Small note"

    1require "base64"23text→ Small note = "Small note"4encoded→ U21hbGwgbm90ZQ== = Base64.strict_encode64(text)U21hbGwgbm90ZQ==5decoded→ "Small note" = Base64.decode64(encoded)"Small note"67round_trip→ true = decoded"Small note" == textSmall note89puts "text=#{textSmall note}"10puts "encoded=#{encodedU21hbGwgbm90ZQ==}"11puts "round_trip=#{round_triptrue}"
    outputtext=Small note
    encoded=U21hbGwgbm90ZQ==
    round_trip=true