title: RSA example that do not use NoPadding
body: <p>Where can I find a RSA encrypt example that does <strong>not</strong> use "NoPadding"?</p>
  
  <p><strong>--update</strong></p>
  
  <p>Better: how to make this SSCCE run correctly without throw the <em>"too much data for RSA block"</em> exception?</p>
  
  <pre><code>import java.math.BigInteger;
  import java.security.KeyFactory;
  import java.security.interfaces.RSAPrivateKey;
  import java.security.interfaces.RSAPublicKey;
  import java.security.spec.RSAPrivateKeySpec;
  import java.security.spec.RSAPublicKeySpec;
  
  import javax.crypto.Cipher;
  
  /**
   * Basic RSA example.
   */
  public class TestRSA {
  
      public static void main(String[] args) throws Exception {
  
   byte[] input = new byte[100];
  
   Cipher cipher = Cipher.getInstance("RSA/None/NoPadding", "BC");
   KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
  
   // create the keys
  
   RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger("d46f473a2d746537de2056ae3092c451",
    16), new BigInteger("11", 16));
   RSAPrivateKeySpec privKeySpec = new RSAPrivateKeySpec(new BigInteger(
    "d46f473a2d746537de2056ae3092c451", 16), new BigInteger("57791d5430d593164082036ad8b29fb1",
    16));
  
   RSAPublicKey pubKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
   RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privKeySpec);
  
   // encryption step
  
   cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  
   byte[] cipherText = cipher.doFinal(input);
  
   // decryption step
  
   cipher.init(Cipher.DECRYPT_MODE, privKey);
  
   byte[] plainText = cipher.doFinal(cipherText);
  
      }
  }
  </code></pre>
  
  <p><strong>--update: about loop</strong></p>
  
  <p>Using:</p>
  
  <pre><code> byte[] cipherText = new byte[input.length];
   for (int i = 0; i &lt; input.length; i++) {
       byte[] singleByteArray = new byte[] { input[i] };
       cipherText[i] = cipher.doFinal(singleByteArray)[0];
   }
  </code></pre>
  
  <p>does not work fine. For a unknown reason the cipherText became full of zeros - even if the input is an array of 0x03.</p>