Base64 Encoding & Decoding in Ruby
Ruby's standard library includes a Base64 module with three encoding methods. The most important thing to know: encode64 adds newlines, but strict_encode64 does not — always use the strict variant for general use.
Basic Usage
require 'base64'
# Encode
encoded = Base64.strict_encode64("Hello, world!")
puts encoded # "SGVsbG8sIHdvcmxkIQ=="
# Decode
decoded = Base64.strict_decode64("SGVsbG8sIHdvcmxkIQ==")
puts decoded # "Hello, world!"
The Three Encoding Methods
Ruby's Base64 module provides three encoding methods — and they produce different output:
require 'base64' data = "Hello, world!" # encode64: adds a newline (\n) every 60 characters Base64.encode64(data) # => "SGVsbG8sIHdvcmxkIQ==\n" ← has trailing newline! # strict_encode64: RFC 4648 compliant, NO newlines Base64.strict_encode64(data) # => "SGVsbG8sIHdvcmxkIQ==" ← clean # urlsafe_encode64: URL-safe (-/_), NO padding by default Base64.urlsafe_encode64(data) # => "SGVsbG8sIHdvcmxkIQ==" (padding optional)
Common bug: Using encode64 instead of strict_encode64 embeds newlines in the output. This causes issues when the Base64 string is used in JSON, HTTP headers, or URLs. Always prefer strict_encode64.
URL-Safe Base64
require 'base64' data = "data for a URL: a+b/c=d" # Encode URL-safe (padding=true by default in Ruby 3.x) encoded = Base64.urlsafe_encode64(data, padding: false) puts encoded # No +, /, or = characters # Decode URL-safe decoded = Base64.urlsafe_decode64(encoded) puts decoded # "data for a URL: a+b/c=d"
Encoding Files
require 'base64'
# Encode file to Base64 string
encoded = Base64.strict_encode64(File.binread("image.png"))
File.write("image.b64", encoded)
# Decode Base64 back to file
decoded = Base64.strict_decode64(File.read("image.b64"))
File.binwrite("output.png", decoded)
Creating Data URIs
require 'base64'
require 'mime/types'
def file_to_data_uri(path)
mime = MIME::Types.type_for(path).first&.content_type || 'application/octet-stream'
encoded = Base64.strict_encode64(File.binread(path))
"data:#{mime};base64,#{encoded}"
end
puts file_to_data_uri("logo.png")
# "data:image/png;base64,iVBORw0KGgo..."
Decoding Methods
require 'base64'
encoded = "SGVsbG8sIHdvcmxkIQ=="
# decode64: handles newlines in input (lenient)
Base64.decode64(encoded)
# strict_decode64: strict RFC 4648, raises on invalid chars
begin
Base64.strict_decode64(encoded)
rescue ArgumentError => e
puts "Invalid Base64: #{e.message}"
end
# urlsafe_decode64: handles - and _ instead of + and /
Base64.urlsafe_decode64("SGVsbG8sIHdvcmxkIQ")
Rails / ActiveSupport
If you're in a Rails project, ActiveSupport extends Base64 with convenience methods:
require 'active_support/core_ext/string/encoding'
# Available via ActiveSupport::Base64
encoded = ::Base64.strict_encode64("Hello")
# Or for URL-safe tokens in Rails:
token = SecureRandom.base64(32) # URL-safe random token
Verify your Ruby Base64 output
Paste the output of your Ruby Base64 encoding into base64.dev to decode and verify it.
Open base64.dev →