栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java使用SHA256withRSA/PSS进行验签

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java使用SHA256withRSA/PSS进行验签

文章目录

前言一、SHA256withRSA/PSS是什么?二、使用步骤

1.引入库2.验签方法 总结


前言

最近业务提出了要做签名验签的一些功能,大体背景如下:
加密算法:RSA
密钥长度:2048
签名算法:SHA256withRSA/PSS 摘要算法为SHA256 填充算法PSS
需要在c#平台生成签名和摘要拿到java平台来进行验签,java端的入参为摘要和签名


一、SHA256withRSA/PSS是什么?

SHA256withRSA/PSS是一种安全的签名算法,它使用了SHA256的摘要算法、PSS的填充算法

二、使用步骤 1.引入库

引入maven依赖:

        
            org.bouncycastle
            bcprov-jdk15on
            1.62
        
  由于java自带的security包下的签名算法不包括SHA256withRSA/PSS,所以我们需要引入算法提供
者BC(BouncyCastleProvider) 。
2.验签方法

由于我们的入参没有原文,只有原文经过sha256之后的摘要,而java中Signature类提供的verify方法中入参为原文,所以这里无法使用Signature直接进行验签名(是否可以我也不知道知道,之间在这里卡了很久,一直希望可以直接使用java完成,但是一直无法实现。如果大家可以,要@我~~):

    
    public static boolean verifyWithDigest(String hash,String sign,String publicKey) {
        long thisTime = System.currentTimeMillis();
        OutputStream outputStream=null,outputStream1=null;
        File file = new File(HASHVERIFY_PREFIX);
        if (!file.exists()) {
            file.mkdir();
        }
        File hashFile = new File(HASHVERIFY_PREFIX+"/"+thisTime+"-hash");
        File signFile = new File(HASHVERIFY_PREFIX+"/"+thisTime+"-sign");
        String publicPemPath = "";
        try {
            outputStream = new FileOutputStream(hashFile);
            outputStream.write(HexUtil.decodeHex(hash));
            outputStream1 = new FileOutputStream(signFile);
            outputStream1.write(HexUtil.decodeHex(sign));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec pkcs8KeySpec = new X509EncodedKeySpec(cn.hutool.core.codec.base64.decode(publicKey));
            PublicKey publicK = keyFactory.generatePublic(pkcs8KeySpec);
            publicPemPath = getPublicPem(publicK);
            // TODO: 这里pem存储位置要修改
            String command = "openssl pkeyutl -verify -pubin -inkey %s -sigfile %s -in %s -pkeyopt rsa_padding_mode:pss -pkeyopt rsa_pss_saltlen:32 -pkeyopt digest:sha256";
            String endCommand = String.format(command,publicPemPath,signFile.getAbsolutePath(), hashFile.getAbsolutePath());
            LOG.info(endCommand);
            String result = RSAUtil.execShellCommand(endCommand).toString();
            if (result.toLowerCase().contains("ok") || result.toLowerCase().contains("success")){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            LOG.error("验签执行失败,{}",e);
            return false;
        }finally {
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream1 != null){
                try {
                    outputStream1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //文件删除
            hashFile.delete();
            signFile.delete();
            new File(publicPemPath).delete();
        }
    }
    

     
    public static Object execShellCommand(String cmd) {
        LineNumberReader br = null;
        try {
            String[] cmdA = { "/bin/sh", "-c", cmd };
            Process process = Runtime.getRuntime().exec(cmdA);
            br = new LineNumberReader(new InputStreamReader(
                    process.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("n");
            }
            return sb.toString();
        } catch (Exception e) {
            LOG.error("{}",e);
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

       private static String getPublicPem(PublicKey publicKey)
            throws IOException {
        StringWriter stringWriter = new StringWriter();
        PemWriter pemWriter = new PemWriter(stringWriter);
        PemObject pemObject = new PemObject("PUBLIC KEY",publicKey.getEncoded());
        pemWriter.writeObject(pemObject);
        pemWriter.flush();
        pemWriter.close();
        File files = new File(PEMFILE_PREFIX);
        if (!files.exists()) {
            files.mkdir();
        }
        File file = new File(PEMFILE_PREFIX+"/"+System.currentTimeMillis()+"publicKey.pem");
        OutputStream outputStream = new FileOutputStream(file);
        outputStream.write(stringWriter.toString().getBytes(StandardCharsets.UTF_8));
        return file.getAbsolutePath();
    }

代码如上


整体思路为使用Openssl命令代替Java验证签名,需要将publicKey(公钥)以pem格式保存到服务器内,然后将摘要、签名和公钥传入shell命令中,在openssl中执行命令,获取结果,判断command返回的值就可以了。


总结

由于参数没有原值,所以我在验签的地方卡了很久,最终采用了java+openssl的方式实现,虽然实现了,但是作为一名Java开发来说,这样完成并不爽,但是又暂时没有其他替代方式,如果大家有其他解决方案可以@我~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709404.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号