title: How to get private key from jks keystore for signing the message with privatekey using shal256rsa in java
body: <p>I want to encrypt the message with signed private key.and verify the signature but getting error in verify signature</p>
  <blockquote>
  <p>//importing the crt and key into p12 file  and importing into jks file</p>
  </blockquote>
  <blockquote>
  <p>openssl pkcs12 -export -in D:\cedge_uat\STAR_cedgenetbanking_in.crt -inkey D:\cedge_uat\newcedgenetbanking251920.key -name cedge1 -out D:\cedge_uat\convertedfile1.p12</p>
  </blockquote>
  <blockquote>
  <p>keytool -importkeystore -deststorepass cedge1 -destkeystore newkeystore.jks -srckeystore D:\cedge_uat\convertedfile1.p12 -srcstoretype PKCS12</p>
  </blockquote>
  <pre><code> public static PrivateKey generatePrivateKey() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, UnrecoverableKeyException{
              KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
                  //Jks file path 
              FileInputStream fis;
              PrivateKey privateKey = null;
              try {
                  fis = new FileInputStream(&quot;C:/Program Files/Java/jre1.8.0_171/bin/keystore12.jks&quot;);
                  keyStore.load(fis, &quot;changeit&quot;.toCharArray());
                  //jks file password 
                   privateKey = (PrivateKey) keyStore.getKey(&quot;changeit&quot;, &quot;changeit&quot;.toCharArray());
                  System.out.println(&quot;privateKey--&quot;+privateKey);
              } catch (FileNotFoundException e) {
                  System.out.println(&quot;e--&quot;+e);
                      e.printStackTrace();
              }
              return privateKey;
      }
          
      public static String signature(String sessionkey, PrivateKey privatekey) throws Exception 
      {
              Signature sign = Signature.getInstance(&quot;SHA256withRSA&quot;);
              sign.initSign(privatekey);
              sign.update(sessionkey.getBytes());
              return new String(Base64.getEncoder().encodeToString(sign.sign()));
      }
  
  
   public static PublicKey generatePublicKey() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, UnrecoverableKeyException{
              KeyStore keyStore = KeyStore.getInstance(&quot;JKS&quot;);
              FileInputStream fis;
              PublicKey publicKey=null;
              try {
                  fis = new FileInputStream(&quot;C:/Program Files/Java/jre1.8.0_171/bin/newkeystore.jks&quot;);
                  
                  keyStore.load(fis, &quot;cedge1&quot;.toCharArray());
                  Certificate cert = keyStore.getCertificate(&quot;cedge1&quot;);
                   publicKey = cert.getPublicKey(); 
              } catch (FileNotFoundException e) {
                  System.out.println(&quot;e--&quot;+e);
                      e.printStackTrace();
              }
              return publicKey;
      }
      //Signature verification using their public key
      public static boolean verifySignature(String input, String signature, PublicKey publicKey) throws Exception
      {
              Signature verifySig = Signature.getInstance(&quot;SHA256withRSA&quot;);
              verifySig.initVerify(publicKey);
              byte[] singedData = Base64.getDecoder().decode(signature);
              verifySig.update(Base64.getDecoder().decode(input));
              boolean isVerified = verifySig.verify(singedData);
              System.out.println(&quot;isVerified &quot;+ isVerified);
              return isVerified;
      }
  </code></pre>
  <p>Getting below response:</p>
  <blockquote>
  <p>privateKey--sun.security.rsa.RSAPrivateCrtKeyImpl@ffe594cb
  privateKey=sun.security.rsa.RSAPrivateCrtKeyImpl@ffe594cb
  Signature=QtFcvROXmFb+SIqi/sFG5BXtMviidqWYP0ae/Z0PQNKbxYg9LiJMAqjU+XB+V7awkpVpeV8/TmrxO2AFi1hDLOOOdL4rVY1xxPTGw77Q==</p>
  </blockquote>
  <blockquote>
  <p>publicKey=Sun RSA public key, 2048 bits
  modulus: 2170304779081185713374867545321744099657549785541087943424133659953554520622568213352873219823464920874049569111847413669517192082390131
  public exponent: 65537
  Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: Last unit does not have enough valid bits
  at java.util.Base64$Decoder.decode0(Unknown Source)</p>
  </blockquote>
  <pre><code>  public static void main(String[] args) throws Exception 
      {
          //generatePrivateKey();
          PrivateKey privateKey=generatePrivateKey();
          System.out.println(&quot;privateKey=&quot;+privateKey);
          String signature = signature(&quot;hello&quot;,privateKey);
          System.out.println(&quot;Signature=&quot;+signature);
          
          //generatePublicKey();
          PublicKey publicKey=generatePublicKey();
          System.out.println(&quot;publicKey=&quot;+publicKey);
          System.out.println(&quot;verify=&quot;+ verifySignature(&quot;hello&quot;,signature,publicKey));
              
      }
  </code></pre>