Quick revision
The short answer
Base64 is a reversible encoding that turns any bytes into text using 64 ASCII characters, making the output about 4/3 the size of the input with = padding at the end; anyone can decode it, because there is no key, so it is not encryption. For API keys we only need to verify, we store a hash, such as SHA-256 of a random high-entropy key (or a slow hash like bcrypt or Argon2 if the secret is a human password). For values we must recover, we use real encryption such as AES-GCM with keys held in a KMS or Vault. Base64 is still useful for carrying bytes such as ciphertext or keys in JSON or headers.
What to remember
In this answer, we saw that Base64 is a reversible encoding of bytes into 64 ASCII characters, about 4/3 the size with = padding, that anyone can decode because there is no key, so it is not encryption. For API keys we only verify, we store a hash, SHA-256 for random high-entropy keys or bcrypt and Argon2 for human passwords. For keys we must recover, we use AES-GCM with keys in a KMS or Vault, or keep them in a secret store. Base64 remains useful for carrying bytes such as ciphertext or keys through JSON and headers.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Base64 is a reversible encoding that turns any bytes into text using 64 ASCII characters, making the output about 4/3 the size of the input with = padding at the end; anyone can decode it, because there is no key, so it is not encryption. For API keys we only need to verify, we store a hash, such as SHA-256 of a random high-entropy key (or a slow hash like bcrypt or Argon2 if the secret is a human password). For values we must recover, we use real encryption such as AES-GCM with keys held in a KMS or Vault. Base64 is still useful for carrying bytes such as ciphertext or keys in JSON or headers.
Why this question is asked
"Encrypted with Base64" is a phrase that makes security reviewers wince, but it is an easy mistake: a Base64 string looks scrambled, and the code really does transform the value. The trouble is that the transformation is public and anyone can undo it in one line. Interviewers ask this to see whether we can explain the difference between encoding, hashing and encryption clearly, and choose the right one for API keys. In this answer, we will see what Base64 is and how it works, prove that it hides nothing, then decide between hashing and encryption for stored API keys, and finish with where Base64 really belongs.
1What Base64 Is
Many channels can only carry text: JSON strings, HTTP headers, email bodies, XML. Raw bytes such as an image, a key or ciphertext contain values that are not printable characters. Base64 solves this by mapping every 3 bytes (24 bits) to 4 characters of 6 bits each, chosen from an alphabet of 64 safe characters: A to Z, a to z, 0 to 9, + and /.
Because 3 bytes become 4 characters, the output is about 4/3 the size of the input, roughly 33 percent larger. When the input length is not a multiple of 3, the last group is padded with one or two = characters so the output length is always a multiple of 4.
2Why It Is Not Encryption
Encryption needs a key: without the key, the ciphertext reveals nothing about the plaintext. Base64 has no key at all. The alphabet and the algorithm are public standards (RFC 4648), and every language has a decoder. An attacker who reads the table sees a string ending in = or made of letters, digits, + and /, recognises Base64 at a glance, and decodes it.
| Technique | Reversible? | Needs a key? | Use it for |
|---|---|---|---|
| Base64 encoding | Yes, by anyone | No | carrying bytes as text |
| Hashing (SHA-256) | No | No | verifying high-entropy secrets |
| Slow hashing (bcrypt, Argon2) | No | No (salted) | human passwords |
| Encryption (AES-GCM) | Yes, with the key | Yes | values we must read back |
So the colleague's design stores API keys in plain text with an extra step. Anyone with read access to the database, a backup or a log that prints the row has the real keys. It may even be worse than plain text, because it gives a false sense of security, and nobody adds real protection to something they believe is already encrypted.
3Which Fix Depends on What We Do With the Key
There are two very different cases, and the right answer depends on which one we have. In the first, we issued the API keys to our own clients and only need to check them when a request arrives. We never need to read the key back. In the second, the keys belong to third parties (say our payment provider's secret key) and we must send them in outgoing requests, so we must recover the original value.
4Case 1: Keys We Only Verify, So Hash Them
For keys we issue, we store a hash and never the key itself. When a client calls us, we hash the presented key and look up the hash. If the database leaks, the attacker gets hashes, which cannot be turned back into usable keys.
Notice the detail about speed. Our API keys are generated randomly with 256 bits from SecureRandom, so they cannot be guessed, and a fast hash like SHA-256 is enough; it also lets us look the key up by its hash in an index. Human passwords are different, because people choose guessable values, so they need a slow, salted hash such as bcrypt or Argon2 to make guessing expensive.
5Case 2: Keys We Must Recover, So Encrypt Them
For third-party keys we need the original value, so hashing does not work; we encrypt. We use AES-GCM with a random IV per value, and the encryption key lives in a KMS or Vault, never in the same database or in the application's configuration file. Often the simplest option is not to keep these keys in our database at all, but in a secret store such as Vault or a cloud secret manager, which handles encryption, access policies and audit for us.
Notice the last comment: Base64 appears even in the correct solution, because the API needs to carry bytes in JSON. That is exactly where Base64 belongs.
6Where Base64 Is the Right Tool
Base64 is the right choice whenever bytes must travel through a text channel: ciphertext and IVs stored in a text column or sent in JSON, keys in PEM files, binary data in HTTP headers, JWT segments (which use the URL-safe variant) and email attachments. In all of these, the protection, if any, comes from encryption or signatures applied before encoding; Base64 only makes the result printable.
7Follow-up questions
Is Base64 at least obfuscation?
Barely. It stops someone glancing at a screen, but any developer or tool decodes it instantly, and security scanners search for Base64-encoded secrets. It should never be presented as protection.
Why can a fast hash like SHA-256 be fine for API keys but not for passwords?
Because a random 256-bit API key has far too many possible values to guess, even at billions of hashes per second. Human passwords come from a small, predictable set, so an attacker can try likely passwords quickly unless the hash is deliberately slow and salted.
8Conclusion
In this answer, we saw that Base64 is a reversible encoding of bytes into 64 ASCII characters, about 4/3 the size with = padding, that anyone can decode because there is no key, so it is not encryption. For API keys we only verify, we store a hash, SHA-256 for random high-entropy keys or bcrypt and Argon2 for human passwords. For keys we must recover, we use AES-GCM with keys in a KMS or Vault, or keep them in a secret store. Base64 remains useful for carrying bytes such as ciphertext or keys through JSON and headers.
Open with
I would explain that Base64 is just an encoding. It turns bytes into text by taking three bytes at a time and writing them as four characters from a 64-character alphabet, so output is about a third bigger and ends with one or two equals signs of padding when needed.
Then, if they want more
The algorithm is public and there is no key, so anyone with database access can decode the API keys in one line. It is plain text with an extra step, and arguably worse because it looks protected. What to use instead depends on whether we need the key back.
If these are keys we issue to clients and only verify, we store a hash: generate a random 256-bit key with SecureRandom, show it to the client once, store SHA-256 of it, and on each request hash the presented key and look it up.
A fast hash is fine there because the key is high-entropy; for human passwords I would use bcrypt or Argon2.
If these are third-party keys we must send onwards, we need encryption: AES-GCM with a random IV, with the key in a KMS or Vault transit, not in the same database, or simply store them in Vault or a cloud secret manager.
I would finish by saying Base64 is not useless; it is exactly right for carrying ciphertext or keys in JSON, headers or PEM files, as long as the protection comes from real encryption first.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Security reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A colleague stores API keys in the database 'encrypted with Base64'. Explain what Base64...": https://scrivio-interview-library.netlify.app/spring-boot/secrets-and-crypto/61f4b5c20f/