2

我正在尝试在 Java 中为一些政府服务创建带有签名(俄罗斯 GOST34.10-2001 算法)的 SOAP 消息。问题是服务告诉我签名无效,我看不到我在哪里犯了错误。我从中读取私钥和证书的密钥容器是有效的。哪里出了问题?我知道我的问题非常具体,但我仍然希望有人能告诉我方法。

更新1:一点更新:消息正文有一些西里尔文(utf)-8节点和值 更新2:所以我通读了这个问题并尝试以相同的方式获取PrivateKey并得到了这个异常:java.lang.IllegalArgumentException:private密钥算法与终端实体证书中的公钥算法不匹配

这是一些代码:

在这里,我将类编组到 SOAP 主体中:

try {
            MessageFactory mf = MessageFactory.newInstance();
            SOAPMessage sm = mf.createMessage();
            SOAPPart sp = sm.getSOAPPart();
            SOAPEnvelope envelope = sp.getEnvelope();

            QName bodyId = new QName("http://ws.unisoft/", "SendFullULRequest");

            SOAPBody sb = (SOAPBody) envelope.getBody();
            SOAPHeader sh = (SOAPHeader) envelope.getHeader();

            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(FullULRq.class);
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();

            marshaller.marshal(new JAXBElement<FullULRq>(bodyId, FullULRq.class, fulr),
                    sm.getSOAPBody());

            sm.saveChanges();

然后我计算摘要值:

ReferenceType rt2 = new ReferenceType();
            rt2.setURI("#body");
            ////Пункт 7
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            Source xmlSource = new DOMSource(sm.getSOAPBody());
            Result outputTarget = new StreamResult(outputStream);
            try {
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                transformer.setOutputProperty(OutputKeys.INDENT, "no");
                transformer.transform(xmlSource, outputTarget);
                Init.init();

                Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
                final ByteArrayInputStream is = new ByteArrayInputStream(canon.canonicalize(outputStream.toByteArray()));


                rt2.setDigestValue(computeDigestWithStream(is));
            } catch (Exception e) {
                e.printStackTrace();
            }

使用这种方法:

public static byte[] computeDigestWithStream(ByteArrayInputStream stream) throws Exception {
    final MessageDigest digest =
            MessageDigest.getInstance("GOST3411");

    // processing data
    final DigestInputStream digestStream =
            new DigestInputStream(stream, digest);
    while (digestStream.available() != 0) {
        digestStream.read();
    }

最后我从块中生成一个签名(其中存在元素):

        ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream();
        Source xmlSource2 = new DOMSource(sm.getSOAPBody());
        Result outputTarget2 = new StreamResult(outputStream2);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        transformer.transform(xmlSource2, outputTarget2);
        Init.init();
        Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS);
        InputStream is2 = new ByteArrayInputStream(outputStream2.toByteArray());
        byte[] bb = canon.canonicalize(outputStream2.toByteArray());
        byte[] signval = sign("CryptoProSignature", key, bb);

符号()方法:

public static byte[] sign(String algorithmName, PrivateKey privateKey, byte[] data) throws Exception {
        final Signature sig = Signature.getInstance(algorithmName);
        sig.initSign(privateKey);
        sig.update(data);
        return sig.sign();
    }

如果它是相关的,这就是我从容器中读取 privateKey 的方式:

 KeyStore ks = KeyStore.getInstance("FloppyStore");

        String passwd = "*";
        String kalias = null; 

        ks.load(null, null);

        Enumeration enum1 = ks.aliases();
        for (; enum1.hasMoreElements();) {
            String tAlias = (String) enum1.nextElement();
            if (ks.isKeyEntry(tAlias)) {
                kalias = tAlias;
            }
            System.out.println(tAlias);
        }


        PrivateKey key = (PrivateKey) ks.getKey(kalias, passwd.toCharArray());

谢谢!

4

0 回答 0