gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:18 27 4
gpt4 key购买 nike

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

然后:

@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。

最佳答案

关于java - 如何使用 java.security 验证 xmldsig 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50744409/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com