GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Standard Base64 encoding and decoding | java.evolved

Home / Security / Standard Base64 encoding and decoding
Security Beginner

Standard Base64 encoding and decoding

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Use java.util.Base64 instead of internal JDK classes or third-party codecs.

Security Serialization
Code Comparison
✕ Internal JDK API
String encoded = new sun.misc.BASE64Encoder()
    .encode(data);
✓ Java 8+
String encoded = Base64.getEncoder()
    .encodeToString(data);
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
🛡️

Supported API

Avoids inaccessible sun.misc implementation classes.

🧩

Complete variants

Includes basic, URL-safe, and MIME encoders and decoders.

🚀

No dependency

Encoding and decoding are built into the JDK.

Old Approach
sun.misc encoder
Modern Approach
Standard Base64 API
Since JDK
8
Difficulty
Beginner
JDK Support
Standard Base64 encoding and decoding
Available

Widely available since JDK 8 (March 2014).

How it works

The standard Base64 API provides basic, URL-safe, and MIME encoders and decoders without relying on unsupported internal packages. Use getEncoder() and getDecoder() for standard Base64, or select the URL and MIME variants when the target format requires them.

Related Documentation
Base64 ↗
Proof
View proof source ↗
Related patterns
Security Advanced

PEM encoding/decoding

Java 8
String pem = "-----BEGIN CERTIFICATE-----\n"
    + Base64.getMimeEncoder()
        .encodeToString(
            cert.getEncoded())
    + "\n-----END CERTIFICATE-----";
Java 25 (Preview)
// Encode to PEM
String pem = PEMEncoder.of()
    .encodeToString(cert);
// Decode from PEM
var cert = PEMDecoder.of()
    .decode(pemString);
Hover to see modern ➜
JDK 25+ →
Date/Time Intermediate

HexFormat

Java 8
// Pad to 2 digits, uppercase
String hex = String.format(
    "%02X", byteValue);
// Parse hex string
int val = Integer.parseInt(
    "FF", 16);
Java 17+
var hex = HexFormat.of()
    .withUpperCase();
String s = hex.toHexDigits(
    byteValue);
byte[] bytes =
    hex.parseHex("48656C6C6F");
Hover to see modern ➜
JDK 17+ →
Strings Beginner

String chars as stream

Java 8
for (int i = 0; i < str.length(); i++) {
    char c = str.charAt(i);
    if (Character.isDigit(c)) {
        process(c);
    }
}
Java 9+
str.chars()
    .filter(Character::isDigit)
    .forEach(c -> process((char) c));
Hover to see modern ➜
JDK 9+ →
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.