title: RSA Decryption - BadPaddingException: Data must start with zero
body: <p>I am writing RSA ENcyrption/Decryption.
  Here's the code.
  But when i decrypt using Private Key, I am getting exception</p>
  
  <pre><code>public class RSACrypto {
  
      private static SecureRandom sr = new SecureRandom();
  
      /**
       * @param rsabits
       * @return keyPair
       * @throws NoSuchAlgorithmException
       */
      public static KeyPair newKeyPair(int rsabits) throws NoSuchAlgorithmException {
          KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
          generator.initialize(rsabits, sr);
          return generator.generateKeyPair();
      }
  
      /**
       * @param key
       * @return key
       */
      public static byte[] pubKeyToBytes(PublicKey key) {
          return key.getEncoded(); // X509 for a public key
      }
  
      /**
       * @param key
       * @return key
       */
      public static byte[] privKeyToBytes(PrivateKey key) {
          return key.getEncoded(); // PKCS8 for a private key
      }
  
      /**
       * @param bytes
       * @return key
       * @throws InvalidKeySpecException
       * @throws NoSuchAlgorithmException
       */
      public static PublicKey bytesToPubKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
          return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));
      }
  
      /**
       * @param bytes
       * @return key
       * @throws InvalidKeySpecException
       * @throws NoSuchAlgorithmException
       */
      public static PrivateKey bytesToPrivKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
          return KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(bytes));
      }
  
      /**
       * @param input
       * @param key
       * @return encryptedText
       * @throws IllegalBlockSizeException
       * @throws BadPaddingException
       * @throws InvalidKeyException
       * @throws NoSuchAlgorithmException
       * @throws NoSuchPaddingException
       */
      public static byte[] encryptWithPubKey(byte[] input, PublicKey key) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
          Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.ENCRYPT_MODE, key);
          return cipher.doFinal(input);
      }
  
      /**
       * @param input
       * @param key
       * @return decryptedText
       * @throws IllegalBlockSizeException
       * @throws BadPaddingException
       * @throws InvalidKeyException
       * @throws NoSuchAlgorithmException
       * @throws NoSuchPaddingException
       */
      public static byte[] decryptWithPrivKey(byte[] input, PrivateKey key) throws IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException {
          Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.DECRYPT_MODE, key);
          return cipher.doFinal(input);
      }
  
      /**
       * @param plainText
       * @return encryptedText
       * @throws InvalidKeyException
       * @throws IllegalBlockSizeException
       * @throws BadPaddingException
       * @throws NoSuchAlgorithmException
       * @throws NoSuchPaddingException
       * @throws UnsupportedEncodingException
       */
      public static String encrypt(String plainText) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException,
              UnsupportedEncodingException {
          KeyPair kp = newKeyPair(1 &lt;&lt; 11); // 2048 bit RSA; might take a second to generate keys
          PublicKey pubKey = kp.getPublic();
          PrivateKey priKey = kp.getPrivate();
          System.out.println("Private Key: " + new BASE64Encoder().encode(privKeyToBytes(priKey)));
          byte[] cipherText = encryptWithPubKey(plainText.getBytes("UTF-8"), pubKey);
          return new BASE64Encoder().encode(cipherText);
      }
  
      /**
       * @param encrypted
       * @param privateKey
       * @return decryptedText
       * @throws NoSuchAlgorithmException
       * @throws InvalidKeySpecException
       * @throws IOException
       * @throws InvalidKeyException
       * @throws IllegalBlockSizeException
       * @throws BadPaddingException
       * @throws NoSuchPaddingException
       */
      public static String decrypt(String encrypted, String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, InvalidKeyException, IllegalBlockSizeException,
              BadPaddingException, NoSuchPaddingException {
          PrivateKey privateKeyValue = bytesToPrivKey(new BASE64Decoder().decodeBuffer(privateKey));
          return new String(decryptWithPrivKey(new BASE64Decoder().decodeBuffer(encrypted), privateKeyValue), "UTF-8");
      }
  }
  </code></pre>
  
  <p>I am getting below error while <strong>decrypting</strong> with generated privateKey and encrypted text. I have gone through many posts, but couldn't figure what's going wrong here.</p>
  
  <pre><code>javax.crypto.BadPaddingException: Data must start with zero
          at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
          at sun.security.rsa.RSAPadding.unpad(Unknown Source)
          at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
          at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
          at javax.crypto.Cipher.doFinal(DashoA13*..)
  </code></pre>
  
  <p>Any idea, please help.</p>
  
  <p><strong>Update</strong></p>
  
  <p>I am calling from different class</p>
  
  <pre><code>String encryptedText = RSACrypto.encrypt("PLAIN TEXT"));
  
  String privateKey = ""; //During encryption, it will print privatekey value, assigning the same here
  String plaintext = RSACrypto.decrypt(encryptedText , privateKey);
  </code></pre>
  
  <p>After little work, I tried without encoding and decoding of Base64. it's working fine.</p>
  
  <p>What I am messing up with base64 here? not getting any clue</p>