<p>Is <a href="http://bouncycastle.org/java.html" rel="nofollow noreferrer">Bouncy Castle API</a> Thread Safe ? Especially,</p>

<pre><code>org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher
org.bouncycastle.crypto.paddings.PKCS7Padding
org.bouncycastle.crypto.engines.AESFastEngine
org.bouncycastle.crypto.modes.CBCBlockCipher
</code></pre>

<p>I am planning to write a singleton Spring bean for basic level cryptography support in my app. Since it is a web application, there are greater chances of multiple threads accessing this component at a time. So tread safety is essential here.</p>

<p>Please let me know if you have come across such situations using Bouncy Castle.</p>

[{'answer_id': 49498, 'author': 'Tnilsson', 'author_id': 4165, 'author_profile': 'https://Stackoverflow.com/users/4165', 'pm_score': 5, 'selected': True, 'text': '<p>It really does not matter if the API/Code is thread safe. CBC encryption in itself is not thread safe. \nSome terminology -</p>\n\n<pre><code>E(X) = Enctrypt message X\nD(X) = Dectrypt X. (Note that D(E(X)) = X)\nIV = Initialization vector. A random sequence to bootstrap the CBC algorithm\nCBC = Cipher block chaining.\n</code></pre>\n\n<p>A really simple CBC implementation can look like:\nP1, P2, P3 = Plain text messages</p>\n\n<pre><code>1. Generate an IV, just random bits.\n2. Calculate E( P1 xor IV) call this C1\n3. Calculate E( P2 xor C1) call this C2\n4. Calculate E( P3 xor C2) call this C3.\n</code></pre>\n\n<p>As you can see, the result of encrypting P1, P2 and P3 (in that order) is different from encrypting P2, P1 and P3 (in that order). </p>\n\n<p>So, in a CBC implementation, order is important. Any algorithm where order is important can not, by definition, be thread safe. </p>\n\n<p>You can make a Singleton factory that delivers encryption objects, but you cant trust them to be thread safe.  </p>\n'}, {'answer_id': 4345741, 'author': 'Dan Gifford', 'author_id': 294099, 'author_profile': 'https://Stackoverflow.com/users/294099', 'pm_score': 0, 'selected': False, 'text': '<p>The J2ME version is not thread safe.</p>\n'}]
['https://Stackoverflow.com/questions/49473', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/959/']