Cryptography (JCA/JCE)
The JCA/JCE design: provider-backed getInstance factories and engine classes for hashing, encryption, key generation, signatures and secure randomness, wired together by transformation strings. Interviewers care that you can pick the right engine and parameters, not that you can implement a cipher.
part ofJavaoverview, primer and where to startread it →on this pageshowhide
explore
- MessageDigest API5 questions
- Cipher API: Symmetric (AES)5 questions
- Cipher API: Asymmetric (RSA)5 questions
- Transformation Strings & Cipher Parameters5 questions
- KeyGenerator, KeyPairGenerator & KeyStore5 questions
- Signature API5 questions
- SecureRandom API5 questions
questions
page 2 of 2PBKDF2 ships with the JDK, so why pull in a library for bcrypt, scrypt, or Argon2, and how do you use them on the JVM?
basics
~20 sPBKDF2 only stresses CPU, so it's cheap to crack on GPUs/ASICs. bcrypt, scrypt, and Argon2 are also memory-hard, raising attacker cost much more. The JDK has no built-in for them, so you add a library like Spring Security's PasswordEncoder, jBCrypt, or Bouncy Castle.
What are the common correctness and security pitfalls when implementing PBKDF2 with SecretKeyFactory in Java?
basics
~20 sWatch for: keyLength in bits not bytes, passing the password as char[] and wiping it, a fresh random salt per user, a high iteration count, handling NoSuchAlgorithmException/InvalidKeySpecException, and comparing hashes in constant time. Also store the parameters so you can upgrade later.
What is the difference between new SecureRandom() (the default instance) and SecureRandom.getInstanceStrong(), and when would you use each?
basics
~10 snew SecureRandom() gives a strong, non-blocking generator good for almost everything. getInstanceStrong() returns a platform-configured 'strong' algorithm that may block waiting for entropy; reserve it for rare, very high-value secrets like long-lived keys.
How do KeyPair, KeyPairGenerator, and the Signature API fit together, and how do you persist/transport the keys?
basics
~20 sA KeyPairGenerator makes a KeyPair (a PrivateKey + PublicKey). The private key goes into Signature.initSign to sign; the public key goes into Signature.initVerify to check. To store/send keys you encode them (X.509 for public, PKCS#8 for private) and rebuild them with a KeyFactory.
What are the common security and correctness pitfalls when using the Java Signature API, and how do you avoid them?
basics
~20 sWatch for: ignoring or misreading the verify() boolean (always check it), feeding different bytes/encoding on each side, using weak algorithms (SHA-1/MD5, 1024-bit RSA), leaking the private key, and reusing a Signature object across threads. Verify with the trusted public key, not one supplied by the attacker.
How does Java surface padding errors during decryption, and why is exposing BadPaddingException dangerous (padding-oracle)?
basics
~20 sWhen decryption with CBC/PKCS5Padding finds invalid padding, cipher.doFinal throws BadPaddingException (GCM throws the AEADBadTagException subclass). If your app tells an attacker whether padding was valid, they can decrypt data byte by byte — a padding-oracle attack. Reject all decrypt failures identically.
When designing a system, when is RSA encryption the right asymmetric primitive in Java, and what alternatives or pitfalls should drive that decision?
basics
~20 sUse RSA when you must encrypt a small secret (like a symmetric key) to someone's public key. For most data, use hybrid encryption, and for modern systems consider elliptic-curve options (ECDH/ECIES) which are smaller and faster.
In production, how would you organize keystores vs truststores and protect private key material — and what are the trade-offs of file-based KeyStores versus an HSM/KMS?
basics
~20 sKeep your own private keys in a keystore and the certificates you trust in a separate truststore. File-based stores are simple but the key bytes live in process memory; an HSM or cloud KMS keeps keys in hardware so they never leave, at the cost of latency and complexity.
How do transformation strings interact with security providers, and how do you ensure portable, deterministic Cipher behavior across JDKs and FIPS?
basics
~20 sCipher.getInstance asks the JCA provider list for an implementation of your transformation. Different providers (SunJCE, BouncyCastle, FIPS) may support different transformations or defaults, so always use full transformation strings, don't hardcode a provider unless required, and test on the JDK/provider you deploy on.
What are SecureRandom 'algorithms' like NativePRNG, SHA1PRNG, and DRBG, and how does platform/provider selection affect portability and reproducibility?
basics
~20 sSecureRandom is provided by named algorithms (NativePRNG reads the OS source, SHA1PRNG is a self-contained generator, DRBG is the modern NIST design). Which you get depends on the platform/provider, so behavior can differ across OSes unless you request one explicitly.
showing 31–40 of 40