title: RSA Decrypt takes longer if preceded by a thread sleep
body: <p>My main concern comes from: I have a spring boot app,A, that calls another spring boot app,B, using rest template. The response from B contains an encrypted fields that needs to be decrypted.</p>
  
  <p>In order to decrypt it a RSA private key is loaded in app A a single time.</p>
  
  <p>The decryption code looks like this:</p>
  
  <pre><code>Cipher rsaCipher2 = 
  Cipher.getInstance("RSA/ECB/PKCS1Padding");
  rsaCipher2.init(Cipher.DECRYPT_MODE, Test.privateKey);
  rsaCipher2.update(encryptMessage);
  byte[] decryptedMessage = rsaCipher2.doFinal();
  </code></pre>
  
  <p>If i submit multiple requests, one after another , I see that the responses are proceeses by the app A in different times and the decryption (only those 4 lines above) takes different times each run, from 5ms to 20ms even if the same test, with the same data, is executed. Is this something normal? I would have expected to have more similar duration times.</p>
  
  <p>Any idea on how I can keep the decryption time at a minimum? I tried a pool of Ciphers but didn't went to well.</p>
  
  <p>In order to see if i could replicate the behaviour outside the app I did the following:</p>
  
  <p>I generated a pair of keys using RSA algorithm, I encrypted a string.
     I run the decryption multiple times, one after another, in a main method, on a single thread, and a decryption is made on average in <strong>5 ms</strong></p>
  
  <p>If i do the same thing, but add a timeout of 100ms before the decryption, the decryption is made on average in <strong>20 ms</strong>. (in case you ask i did not take the timeout into consideration when calculating the average).</p>
  
  <p>Can someone please shine some light on why is this happening, and how can I avoid it. I tried to initialize the cipher before the for statement, but I saw no improvement, I tried to run the decrypt on a separate thread and a slightly improvement was seen, but yet far from the original results.</p>
  
  <p>generated a pair of keys using RSA</p>
  
  <pre><code>KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
  keyGen.initialize(2048);the same thing, but add a timeout of 100ms before the decryption, the decryption is made on average in 20 ms. (in case you ask i did not take the timeou
  KeyPair keyPair = keyGen.genKeyPair();
  PrivateKey privateKey = keyPair.getPrivate();
  PublicKey publicKey = keyPair.getPublic();
  </code></pre>
  
  <p>encoded a random string</p>
  
  <pre><code>Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
  rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
  rsaCipher.update("randomString".getBytes());
  encryptMessage = rsaCipher.doFinal();
  </code></pre>
  
  <p>run the decript 1000 times</p>
  
  <pre><code>for (int i  =  0 ; i &lt; 1000 ; i++ ) {
    //Thread.sleep(100);
      Long time =  System.currentTimeMillis();    
      Cipher rsaCipher2 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
      rsaCipher2.init(Cipher.DECRYPT_MODE, privateKey);
      rsaCipher2.update(encryptMessage);
      byte[] decryptedMessage = rsaCipher2.doFinal();
      System.out.println((System.currentTimeMillis() - time)   + " ms ");
  }
  </code></pre>
  
  <p>Just in case of someone asking about my entropy: </p>
  
  <pre><code>cat /proc/sys/kernel/random/entropy_avail
  3795
  </code></pre>