DevToolsForYou

Base64 Encoder and Decoder in Ruby — Code Examples

Base64 Encoder and Decoder in RubyUse the online tool →

Base64 encoding converts binary or text data into a 64-character ASCII alphabet so it can be safely transmitted over text-only channels. Here is how to encode and decode Base64 in each language using only the standard library.

Ruby's standard library includes the base64 module. Use strict_encode64 and strict_decode64 for standard Base64 without line-break insertion.

Ruby
require 'base64'

# Encode
encoded = Base64.strict_encode64("Hello, world!")
puts encoded # SGVsbG8sIHdvcmxkIQ==

# Decode
decoded = Base64.strict_decode64("SGVsbG8sIHdvcmxkIQ==")
puts decoded # Hello, world!

# URL-safe Base64 (replaces + with - and / with _)
url_safe = Base64.urlsafe_encode64("Hello, world!")
puts url_safe

decoded_url = Base64.urlsafe_decode64(url_safe)
puts decoded_url # Hello, world!

# Without padding (JWT contexts)
no_pad = Base64.urlsafe_encode64("Hello, world!", padding: false)
puts no_pad
Notes & gotchas
  • Use strict_encode64 instead of encode64 — encode64 inserts newlines every 60 characters.
  • urlsafe_encode64 replaces + with - and / with _ to keep output safe in URLs and HTTP headers.
  • Pass padding: false to urlsafe_encode64 to omit = characters, as required by the JWT spec.
Try it in your browser

Need to base64 encode/decode without writing code? The Base64 Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Base64 Encode/Decode
Base64 Encoder and Decoder in other languages