gpt4 book ai didi

java.security.SignatureException : Invalid encoding for signature. 签名已由 Azure 验证

转载 作者:行者123 更新时间:2023-12-02 06:43:34 25 4
gpt4 key购买 nike

使用 Signature.verify 验证签名时,我收到“签名编码无效”异常。使用 Azure 服务验证相同签名时,签名已验证。

我有一个哈希数据 (SHA-256)、一个公钥和一个我正在尝试验证的签名。使用 com.microsoft.azure.keyvault.KeyVaultClient.sign 方法接收签名,签名算法为“ES256”。

这有效(使用 ES256 算法):

    com.microsoft.azure.keyvault.KeyVaultClient keyVaultClient;
String keyPairIdentifier;

boolean verify(byte[] hashData, byte[] signature, JsonWebKeySignatureAlgorithm signingAlgorithm) {
com.microsoft.azure.keyvault.models.KeyVerifyResult result = keyVaultClient.verify(keyPairIdentifier, signingAlgorithm, hashData, signature);
return result.value().booleanValue();
}

此操作失败(证书持有存储在 Azure keyvault 中的相同公钥):

    Signature ecdsaSign = Signature.getInstance("SHA256withECDSA");
ecdsaSign.initVerify(certificate.getPublicKey());
ecdsaSign.update(hashData);
ecdsaSign.verify(signature)

预期结果 - true(签名已验证)

实际结果:

java.security.SignatureException: Could not verify signature
at sun.security.ec.ECDSASignature.engineVerify(ECDSASignature.java:325)
at java.security.Signature$Delegate.engineVerify(Signature.java:1222)
at java.security.Signature.verify(Signature.java:655)
at TestKV.KeyVault.VerifyDPSignature.verifySignatureUsingCertificate(VerifyDPSignature.java:143)
at TestKV.KeyVault.VerifyDPSignature.main(VerifyDPSignature.java:104)
Caused by: java.security.SignatureException: Invalid encoding for signature
at sun.security.ec.ECDSASignature.decodeSignature(ECDSASignature.java:400)
at sun.security.ec.ECDSASignature.engineVerify(ECDSASignature.java:322)
... 4 more
Caused by: java.io.IOException: Sequence tag error
at sun.security.util.DerInputStream.getSequence(DerInputStream.java:330)
at sun.security.ec.ECDSASignature.decodeSignature (ECDSASignature.java:376)

最佳答案

dave_thompson_085 - 谢谢!您附加的代码中有一些错误,签名部分的标签应该是0x02,而不是0x30,并且您在复制第一部分后没有增加o。这是更改后的代码:

    byte[] r = new BigInteger(1,Arrays.copyOfRange(signature,0,32)).toByteArray();
byte[] s = new BigInteger(1,Arrays.copyOfRange(signature,32,64)).toByteArray();
byte[] der = new byte[6+r.length+s.length];
der[0] = 0x30; // Tag of signature object
der[1] = (byte)(der.length-2); // Length of signature object
int o = 2;
der[o++] = 0x02; // Tag of ASN1 Integer
der[o++] = (byte)r.length; // Length of first signature part
System.arraycopy (r,0, der,o, r.length);
o += r.length;
der[o++] = 0x02; // Tag of ASN1 Integer
der[o++] = (byte)s.length; // Length of second signature part
System.arraycopy (s,0, der,o, s.length);

格式更改后,我没有收到“序列标记错误”异常。但验证还是失败。

谢谢!

关于java.security.SignatureException : Invalid encoding for signature. 签名已由 Azure 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56824921/

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