如何在Java中使用shal256rsa从jks密钥库获取私钥以使用私钥对消息进行签名

问题描述

我想用签名的私钥加密邮件,并验证签名,但在验证签名时出错

///将crt和密钥导入p12文件并导入jks文件

openssl pkcs12-导出-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,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;
    }

得到以下回应:

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

publicKey = Sun RSA公钥,2048位 模量:2170304779081185713374867545321744099657549785541087943424133659953554520622568213352873219823464920874049569111847413669517192082390131 公开指数:65537 线程“主”中的异常java.lang.IllegalArgumentException:最后一个单元没有足够的有效位 在java.util.Base64 $ Decoder.decode0(未知来源)

  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));
            
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...