首页 > 解决方案 > How to get private key from jks keystore for signing the message with privatekey using shal256rsa in java

问题描述

I want to encrypt the message with signed private key.and verify the signature but getting error in verify signature

//importing the crt and key into p12 file and importing into jks file

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

keytool -importkeystore -deststorepass cedge1 -destkeystore newkeystore.jks -srckeystore D:\cedge_uat\convertedfile1.p12 -srcstoretype PKCS12

 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("C:/Program Files/Java/jre1.8.0_171/bin/keystore12.jks");
                keyStore.load(fis, "changeit".toCharArray());
                //jks file password 
                 privateKey = (PrivateKey) keyStore.getKey("changeit", "changeit".toCharArray());
                System.out.println("privateKey--"+privateKey);
            } catch (FileNotFoundException e) {
                System.out.println("e--"+e);
                    e.printStackTrace();
            }
            return privateKey;
    }
        
    public static String signature(String sessionkey, PrivateKey privatekey) throws Exception 
    {
            Signature sign = Signature.getInstance("SHA256withRSA");
            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("JKS");
            FileInputStream fis;
            PublicKey publicKey=null;
            try {
                fis = new FileInputStream("C:/Program Files/Java/jre1.8.0_171/bin/newkeystore.jks");
                
                keyStore.load(fis, "cedge1".toCharArray());
                Certificate cert = keyStore.getCertificate("cedge1");
                 publicKey = cert.getPublicKey(); 
            } catch (FileNotFoundException e) {
                System.out.println("e--"+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("SHA256withRSA");
            verifySig.initVerify(publicKey);
            byte[] singedData = Base64.getDecoder().decode(signature);
            verifySig.update(Base64.getDecoder().decode(input));
            boolean isVerified = verifySig.verify(singedData);
            System.out.println("isVerified "+ isVerified);
            return isVerified;
    }

Getting below response:

privateKey--sun.security.rsa.RSAPrivateCrtKeyImpl@ffe594cb privateKey=sun.security.rsa.RSAPrivateCrtKeyImpl@ffe594cb Signature=QtFcvROXmFb+SIqi/sFG5BXtMviidqWYP0ae/Z0PQNKbxYg9LiJMAqjU+XB+V7awkpVpeV8/TmrxO2AFi1hDLOOOdL4rVY1xxPTGw77Q==

publicKey=Sun RSA public key, 2048 bits modulus: 2170304779081185713374867545321744099657549785541087943424133659953554520622568213352873219823464920874049569111847413669517192082390131 public exponent: 65537 Exception in thread "main" java.lang.IllegalArgumentException: Last unit does not have enough valid bits at java.util.Base64$Decoder.decode0(Unknown Source)

  public static void main(String[] args) throws Exception 
    {
        //generatePrivateKey();
        PrivateKey privateKey=generatePrivateKey();
        System.out.println("privateKey="+privateKey);
        String signature = signature("hello",privateKey);
        System.out.println("Signature="+signature);
        
        //generatePublicKey();
        PublicKey publicKey=generatePublicKey();
        System.out.println("publicKey="+publicKey);
        System.out.println("verify="+ verifySignature("hello",signature,publicKey));
            
    }

标签: javasignaturesha256private-keyrsa-sha256

解决方案


推荐阅读