title: decrypting in RSA throws BadPaddingException when decrypting a string from file
body: <p>I used this program to output the encrypted string and copied and pasted the string in a file which I read and tried to decrypt. The program works fine if I try to decrypt strings generated in the program. It only fails if I copy the encrypted string to a file and then try to decrypt it. This is the error I got -</p>
  
  <pre><code>Nov 21, 2013 11:40:01 AM rsademo.RSADemo main
  SEVERE: null
  javax.crypto.BadPaddingException: Data must start with zero
      at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:325)
      at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:272)
      at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)
      at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)
      at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
      at javax.crypto.Cipher.doFinal(Cipher.java:2087)
      at rsademo.RSAUtil.decrypt(RSAUtil.java:112)
      at rsademo.RSADemo.main(RSADemo.java:65)
  </code></pre>
  
  <p>This is the code that I used -</p>
  
  <pre><code>package rsademo;
  
  import java.io.BufferedReader;
  import java.io.FileReader;
  import java.io.IOException;
  import java.security.InvalidKeyException;
  import java.security.KeyPair;
  import java.security.KeyPairGenerator;
  import java.security.NoSuchAlgorithmException;
  import java.security.PrivateKey;
  import java.security.PublicKey;
  import java.security.spec.InvalidKeySpecException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.crypto.BadPaddingException;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.NoSuchPaddingException;
  
  /**
   *
   * @author Test01
   */
  public class RSADemo {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) throws InvalidKeySpecException {
          RSAUtil util = new RSAUtil();
          String path = "c:";
          try {
              KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
              keyGen.initialize(1024);
              KeyPair generatedKeyPair = keyGen.genKeyPair();
              util.dumpKeyPair(generatedKeyPair);
              //util.SaveKeyPair(path, generatedKeyPair);
  
              KeyPair loadedKeyPair = util.LoadKeyPair(path, "RSA");
              System.out.println("Loaded Key Pair");
              util.dumpKeyPair(loadedKeyPair);
  
              PublicKey pub=generatedKeyPair.getPublic();
              PrivateKey priv=generatedKeyPair.getPrivate();
              byte[] data="in153".getBytes();
              System.out.println("Original: "+new String(data));
              byte[] encrypted=util.encrypt(data, pub);
  
              FileReader fr=new FileReader("D:\\Pankaj\\beta\\signageplus.conf");
              BufferedReader br=new BufferedReader(fr);
              String s;
              String[] splits=null;
              while((s=br.readLine())!=null){
                  if(s.startsWith("PASS")){
                      splits=s.split(";");
                      encrypted=splits[1].getBytes();
                      break;
                  }
              }
              br.close();
              System.out.println("Encrypted: "+new String(encrypted));
              byte[] decrypted=util.decrypt(encrypted);
              System.out.println("Decrypted: "+new String(decrypted));
          } catch (NoSuchAlgorithmException ex) {
              Logger.getLogger(RSADemo.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IOException ex) {
              Logger.getLogger(RSADemo.class.getName()).log(Level.SEVERE, null, ex);
          } catch (NoSuchPaddingException ex) {
              Logger.getLogger(RSADemo.class.getName()).log(Level.SEVERE, null, ex);
          } catch (InvalidKeyException ex) {
              Logger.getLogger(RSADemo.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IllegalBlockSizeException ex) {
              Logger.getLogger(RSADemo.class.getName()).log(Level.SEVERE, null, ex);
          } catch (BadPaddingException ex) {
              Logger.getLogger(RSADemo.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
  }
  </code></pre>
  
  <p>The util class is as follows -</p>
  
  <pre><code>package rsademo;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.security.InvalidKeyException;
  import java.security.KeyFactory;
  import java.security.KeyPair;
  import java.security.NoSuchAlgorithmException;
  import java.security.PrivateKey;
  import java.security.PublicKey;
  import java.security.spec.InvalidKeySpecException;
  import java.security.spec.PKCS8EncodedKeySpec;
  import java.security.spec.X509EncodedKeySpec;
  import javax.crypto.BadPaddingException;
  import javax.crypto.Cipher;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.NoSuchPaddingException;
  
  /**
   *
   * @author Test01
   */
  public class RSAUtil {
  
      public void dumpKeyPair(KeyPair keyPair) {
          PublicKey pub = keyPair.getPublic();
          System.out.println("Public Key: " + getHexString(pub.getEncoded()));
  
          PrivateKey priv = keyPair.getPrivate();
          System.out.println("Private Key: " + getHexString(priv.getEncoded()));
      }
  
      public String getHexString(byte[] b) {
          String result = "";
          for (int i = 0; i &lt; b.length; i++) {
              result += Integer.toString((b[i] &amp; 0xff) + 0x100, 16).substring(1);
          }
          return result;
      }
  
      public void SaveKeyPair(String path, KeyPair keyPair) throws IOException {
          PrivateKey privateKey = keyPair.getPrivate();
          PublicKey publicKey = keyPair.getPublic();
  
  // Store Public Key.
          X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
                  publicKey.getEncoded());
          FileOutputStream fos = new FileOutputStream(path + "/public.key");
          fos.write(x509EncodedKeySpec.getEncoded());
          fos.close();
  
  // Store Private Key.
          PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
                  privateKey.getEncoded());
          fos = new FileOutputStream(path + "/private.key");
          fos.write(pkcs8EncodedKeySpec.getEncoded());
          fos.close();
      }
  
      public KeyPair LoadKeyPair(String path, String algorithm)
              throws IOException, NoSuchAlgorithmException,
              InvalidKeySpecException {
  // Read Public Key.
          File filePublicKey = new File(path + "/public.key");
          FileInputStream fis = new FileInputStream(path + "/public.key");
          byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
          fis.read(encodedPublicKey);
          fis.close();
  
  // Read Private Key.
          File filePrivateKey = new File(path + "/private.key");
          fis = new FileInputStream(path + "/private.key");
          byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
          fis.read(encodedPrivateKey);
          fis.close();
  
  // Generate KeyPair.
          KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
          X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
                  encodedPublicKey);
          PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
  
          PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
                  encodedPrivateKey);
          PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
  
          return new KeyPair(publicKey, privateKey);
      }
  
      public byte[] encrypt(byte[] data, PublicKey key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
          Cipher cipher = Cipher.getInstance("RSA");
          cipher.init(Cipher.ENCRYPT_MODE, key);
  
          byte[] cipherData = cipher.doFinal(data);
          return cipherData;
  
      }
  
      public byte[] decrypt(byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException, InvalidKeySpecException, InvalidKeyException {
          Cipher cipher = Cipher.getInstance("RSA");
          KeyPair pair=LoadKeyPair("c:", "RSA");
          PrivateKey key=pair.getPrivate();
          cipher.init(Cipher.DECRYPT_MODE, key);
  
          byte[] cipherData = cipher.doFinal(data);
          return cipherData;
      }
  }
  </code></pre>
  
  <p>Any help is appreciated. Thanks in advance.</p>