Anti-AIClone

Log date · Mon

Can Base64 hide a hardcoded secret in a Java app?

Published on · Updated

No. Our Java test shows Base64 changes static search results while the same shared key is still used at runtime.

The short answer: encoding does not protect a shared key

No. We compiled two Java samples: one embeds a fake key directly, the other stores its Base64 encoding. Both runs printed the same placeholder. In four exact-string checks, the plaintext appeared only in the plain class file and the encoded value only in the encoded class file.

Encoding changed what a simple static search found, but did not make the value used at runtime secret. This experiment covers two Java class files we wrote; it is not a general evaluation of encryption or commercial obfuscators. Android identifies hardcoded cryptographic secrets as a risk. [1]

Results for the two samples
QuestionPlain sampleEncoded sample
Exact search for fake keyFoundNot found
Stored valuePlain textBase64 text
Run the programPrints fake keyPrints same fake key

The sample and environment

We compiled two separate classes on macOS 27.2 with OpenJDK and javac 17.0.19. Neither makes a network request or contains a real credential. The sole value is a placeholder explicitly marked invalid. Plain uses a literal; Encoded decodes Base64 text before use.

To make the behavior visible, both programs deliberately print the placeholder. That is diagnostic output from our own sample, not a runtime extraction from another application. Real applications should not print keys. Oracle describes Base64 as an encoding and decoding scheme; it does not provide a secret key. [4]

Source of the plain sample.
public class Plain {
    private static final String KEY = "DEMO-KEY-not-a-real-credential-0000";
    public static void main(String[] args) {
        System.out.println("fake value: " + KEY);
    }
}
Source of the encoded sample: the decoder and encoded value both ship in the client.
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Encoded {
    private static final String VALUE = "REVNTy1LRVktbm90LWEtcmVhbC1jcmVkZW50aWFsLTAwMDA=";
    public static void main(String[] args) {
        byte[] decoded = Base64.getDecoder().decode(VALUE);
        System.out.println("fake value: " + new String(decoded, StandardCharsets.UTF_8));
    }
}

We do not call Base64 encryption. We chose it to make the distinction between hiding an exact plaintext match and keeping a key secret reproducible in a small experiment. If a shared key actually authorizes a service, the issue is that every installation holds the same authority, not the appearance of search results.

Run the samples and inspect the compiled files

After compiling both classes, we ran each program. The output is identical: on its own execution path, the encoded version obtains and uses the same placeholder again. A real application might not print it. We did not inspect another app’s memory or network traffic, or test runtime instrumentation.

Output from our run, copied verbatim.
== run Plain ==
fake value: DEMO-KEY-not-a-real-credential-0000
== run Encoded ==
fake value: DEMO-KEY-not-a-real-credential-0000

Next we inspected constants and instructions with javap. Oracle documents -constants for static final constants, -c for disassembling code, and -private for including private members. [3] The private modifier is an access control rule; it does not stop these tools displaying a compiled constant.

Excerpt from javap output for Plain.
  private static final java.lang.String KEY = "DEMO-KEY-not-a-real-credential-0000";
Excerpt from javap output for Encoded; it shows the encoded value and decoder calls.
  private static final java.lang.String VALUE = "REVNTy1LRVktbm90LWEtcmVhbC1jcmVkZW50aWFsLTAwMDA=";
       0: invokestatic  #7                  // Method java/util/Base64.getDecoder:()Ljava/util/Base64$Decoder;
       3: ldc           #15                 // String REVNTy1LRVktbm90LWEtcmVhbC1jcmVkZW50aWFsLTAwMDA=
       5: invokevirtual #17                 // Method java/util/Base64$Decoder.decode:(Ljava/lang/String;)[B

The compiled encoded version did not store the plaintext as a complete contiguous string in the class, but javap showed the encoded text and decoder call. Both are in the same program. This observation does not establish that every protection can be inspected in the same way: our sample used neither keyed encryption nor an obfuscator.

What a strings search can and cannot establish

We also used the system strings utility to look for printable text, and exact byte matching to check the two full literals. The results establish only whether those literals occur in these two class files. A missing plaintext match does not establish that a usable secret is absent or that other files are clean.

Output is verbatim; the leading characters are also printed by strings and are not part of the key.
== strings: plain text and encoded text ==
-- Plain.class --
/fake value: DEMO-KEY-not-a-real-credential-0000
#DEMO-KEY-not-a-real-credential-0000
-- Encoded.class --
0REVNTy1LRVktbm90LWEtcmVhbC1jcmVkZW50aWFsLTAwMDA=
Output from our run: the four exact-match results, copied verbatim.
== exact literal occurrences in class files ==
Plain.class | DEMO-KEY-not-a-real-credential-0000 | present
Plain.class | REVNTy1LRVktbm90LWEtcmVhbC1jcmVkZW50aWFsLTAwMDA= | absent
Encoded.class | DEMO-KEY-not-a-real-credential-0000 | absent
Encoded.class | REVNTy1LRVktbm90LWEtcmVhbC1jcmVkZW50aWFsLTAwMDA= | present

On this Mac, strings initially misidentified Encoded.class as an executable container and reported an error when given the file directly. Feeding it through standard input allowed both samples to be inspected; we used that form in our run. A tool error, no match and a passed security review are three different outcomes, and should not be collapsed into “safe”.

Encoding, obfuscation and key storage solve different problems

Splitting or encrypting a string before shipping it may change the result of a static inspection, but the client still needs the information required to use it. Apart from the Base64 encoding above, we did not test those transformations or measure recovery effort. Do not read this example as an evaluation of a particular obfuscator.

Android Keystore addresses key management on a device. Its documentation says key material can remain non-exportable and key use can be restricted; it also notes that a compromised process may still be able to use the app’s key on the device. [2] That is a different design from preinstalling the same server credential on every device.

Classify public keys separately too. A public key used to verify signatures is intended to be distributed; the corresponding signing private key needs protection. A client need not receive the private key to verify an offline licence. You still need rules for expiry, device binding and revocation: an offline device cannot immediately learn about a server-side revocation.

Recommendations by use case

These are design recommendations, not effects measured in the experiment. Map the permission boundary first, then choose where each value lives and how it expires.

Recommendations by asset and privilege
CaseRecommendation
Privileged API key shared by all installationsKeep it on a controlled server; give clients scoped, revocable user tokens.
Private key generated on a deviceUse the platform keystore with usage restrictions; test backup and migration.
Public verification key for an offline licenceShip it with the client; retain the signing private key at the issuer.
A shared key already shippedLimit permissions and rotate it first, inspect logs, then release a version without it.

Tokens have tradeoffs too: a short lifetime reduces the exposure window but requires refresh and failure handling. The service needs to validate audience, scope and expiry and limit sensitive operations. Changing client storage alone does not remove the consequences of a shared credential.

Rotation must account for older releases still running. Inventory dependants, prepare replacement credentials and a migration window, monitor usage, then revoke the old value according to incident severity. Do not paste the old value into tickets or logs to demonstrate the issue; record an identifier, scope and response time instead.

Check your own release build

Reproduce our sample in an isolated directory, then inspect a Java release build you are authorized to check. For a jar, use the JDK jar command to list entries and select a known class for javap; substitute your own path and class name. The output may contain secrets, so do not paste it into public logs.

The first five commands reproduce this example; replace the jar and class in the last two.
javac Plain.java Encoded.java
java Plain
java Encoded
javap -c -constants -private Plain
javap -c -constants -private Encoded

# Replace the path and class with your own release artifact.
jar tf path/to/your-release.jar
javap -classpath path/to/your-release.jar -c -constants -private com.example.YourClass

For a known test placeholder, also check whether the build output contains the full literal. Do not put real credentials in command lines, repositories or shared reports. The command below checks only our sample. Exact matching misses split or transformed values and other formats, so review resources, dependencies and build configuration as well.

Run against our plain sample; this checks one complete literal only.
LC_ALL=C grep -aqF 'DEMO-KEY-not-a-real-credential-0000' Plain.class && echo present

Classify findings as public identifiers, user tokens, device keys or server secrets and verify each one’s privileges. Removing a value from source does not establish that old artifacts, caches and published releases are clean. Repeat checks on a new release build, and actually verify that old credentials stop working.

Your checklist

  • Inventory credentials in release artifacts and verify their privileges.
  • Move shared privileged keys to a controlled server.
  • Use a platform keystore and restrict device key use.
  • Rotate exposed credentials and verify that they stop working.
  • Repeat checks on resources and dependencies in the new release.

Primary sources

All sources and reproduction status ↗