首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用java.security验证xmldsig签名

如何使用java.security验证xmldsig签名
EN

Stack Overflow用户
提问于 2018-06-07 22:58:06
回答 1查看 463关注 0票数 3

我需要使用java.security包验证带有封装xml-dsig签名的文档。加载后,我解组文档,并根据xsd - http://www.w3.org/2000/09/xmldsig#获得签名对象

然后:

代码语言:javascript
复制
@Service
public class XmlSignatureCheckerImpl implements XmlSignatureChecker {
    private static final String ENCRYPTION_ALGORITHM = "RSA";

    private static final String HASH_ENCRYPTION_ALGORITHM = "SHA1withRSA";

    @Override
    @Nullable
    public PublicKey getPublicKey(byte[] exp, byte[] mod) {
        BigInteger modulus = new BigInteger(1, mod);
        BigInteger exponent = new BigInteger(1, exp);
        RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);

        KeyFactory fact;
        try {
            fact = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
            return fact.generatePublic(rsaPubKey);
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    @Nullable
    public Boolean verify(byte[] message, byte[] signature, PublicKey publicKey) {
        final Signature sig;
        try {
            sig = Signature.getInstance(HASH_ENCRYPTION_ALGORITHM);
            sig.initVerify(publicKey);
            sig.update(message);
            boolean verify = sig.verify(Base64.encodeBase64Chunked(signature));
            return verify;
        } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
            e.printStackTrace();
        }
        return null;
    }
}

调用getPublicKey并验证,结果我得到了签名长度不匹配,如果我没有编码签名我没有得到不匹配,但也验证是假的,但我使用的测试数据是完全有效的。放弃寻找错误,帮帮我。请。文件编码为UFT-8。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50744409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档