Is Cipher thread-safe?

No, it isn’t. The instance is stateful. So you need to store it threadlocal, or to obtain a new instance on every encrypt/decrypt call, or to wrap it in a synchronized(cipher) block.

Threadsafety is usually mentioned in javadocs as “is thread safe” or “is not thread safe“. This is not the case for Cipher, so you should not assume it to be threadsafe.

Leave a Comment