JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 27.0w AdaCore Inc
Java


JAVA.CRYPTO.BASE64 : Unsafe Base64 Encoding (Java)

Summary

Base64 encoding is used. This encoding is relatively easy to decode and should not be used with sensitive data.

Properties

Class Name Unsafe Base64 Encoding (Java)
Significance security
Mnemonic JAVA.CRYPTO.BASE64
Categories
CWE CWE:327 Use of a Broken or Risky Cryptographic Algorithm
CERT-Java CERT-Java:DRD18 Do not use the default behavior in a cryptographic library if it does not use recommended practices
  CERT-Java:MSC02-J Generate strong random numbers
OWASP-2017 OWASP-2017:A2 Broken Authentication
OWASP-2021 OWASP-2021:A2 Cryptographic Failures
  OWASP-2021:A6 Vulnerable and Outdated Components
  OWASP-2021:A7 Identification and Authentication Failures
OWASP-2025 OWASP-2025:A04 Cryptographic Failures
  OWASP-2025:A06 Insecure Design
Availability Available for Java and Kotlin.
Enabling Checks for this warning class are disabled by default, and require pedantic Java analysis mode which is also disabled by default. To enable the checks, make the following changes to the project configuration file.
JAVA_ANALYSIS_PEDANTIC_MODE = Yes
WARNING_FILTER += allow class="Unsafe Base64 Encoding (Java)"

Example

In the following program, recoverSecret recovers a secret that was merely Base64-encoded, treating Base64 as if it protected the data. Because Base64 is an encoding rather than encryption, anyone who can read the stored form can decode it back to plaintext. decryptSecret instead uses real encryption (AES in authenticated GCM mode), so recovering the secret requires the secret key.

import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;

public class SecretStore {

    // Base64 is an encoding, not encryption. Treating a Base64-"protected"
    // secret as confidential is unsafe: anyone who can read the stored form
    // can decode it straight back to the plaintext.
    String recoverSecret(String stored) {
        byte[] plain = Base64.getDecoder().decode(stored);   // 'Unsafe Base64 Encoding (Java)' warning issued here
        return new String(plain, StandardCharsets.UTF_8);
    }

    // The safe alternative is real encryption: AES in authenticated GCM mode
    // genuinely protects the secret, so recovering the plaintext requires the
    // secret key.
    String decryptSecret(SecretKey key, byte[] iv, byte[] ciphertext) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(128, iv));
        byte[] plain = cipher.doFinal(ciphertext);           // ok: recovered with a real cipher and key
        return new String(plain, StandardCharsets.UTF_8);
    }
}

To reproduce this example, analyze it with the following configuration:

WARNING_FILTER += allow class="Unsafe Base64 Encoding (Java)"
JAVA_ANALYSIS_PEDANTIC_MODE = Yes
JAVA_ANALYSIS_ENTRY_POINTS_MODE = ALL_METHODS

Resolution

Use a safer cryptography algorithm.

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.adacore.com/csm.