title: Extract private key from keystore file in java
body: <p>I have a keystore file and i have some encrypted text. I have to decrypt it using my private key stored within keystore file. I am not able to figure out how to extract that private key within my java method.</p>
  
  <pre><code>public void decrypt(String encryptedText) {
      byte[] decryptedData = null;  
      try { 
        FileInputStream is = new FileInputStream("/Users/rajat/workspace/dev/abc.jks");    
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(is, "abc".toCharArray());
        String alias = "xyz";
        Key key = keystore.getKey(alias, "abc".toCharArray());
  
        Cipher cipher = Cipher.getInstance("RSA");  
        cipher.init(Cipher.DECRYPT_MODE, key);  
        decryptedData = cipher.doFinal(encryptedText.getBytes());  
        System.out.println("Decrypted Data: " + new String(decryptedData));  
      } catch (Exception e) {  
       e.printStackTrace();  
      }   
    }
  </code></pre>
  
  <p>and when i run I get error as :</p>
  
  <pre><code>javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes
      at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:337)
      at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
      at javax.crypto.Cipher.doFinal(Cipher.java:2087)
  </code></pre>