gpt4 book ai didi

java - 签名长度不正确

转载 作者:行者123 更新时间:2023-11-29 09:28:05 25 4
gpt4 key购买 nike

我需要在 java 中验证签名。我得到一个带有多个参数的 url,其中之一是签名(十六进制格式)。签名的消息是所有其他参数串联的 SHA-256 散列。我还有带有用于检查的公钥的证书。我用于测试的所有值都是通过一个应该是正确的示例提供给我的,我只是创建了连接字符串。

这是我运行的代码:

// signed message --> hash of concat
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update( concat.getBytes() );
byte[] message = digest.digest();
System.out.println("message length "+message.length); // --> 32


// signature belonging to the message --> checkValue
System.out.println("check value length " +checkValue.length()); // --> 512
byte[] sigBytes = checkValue.getBytes();
System.out.println("check value bytes "+sigBytes.length); // --> 512

// public certificate of the CA
File file3 = new File(certificatePath);
byte[] encCertRSA = new byte[(int) file3.length()];
FileInputStream fis3 = new FileInputStream(file3);
fis3.read(encCertRSA);
fis3.close();
InputStream is = new ByteArrayInputStream( encCertRSA );

CertificateFactory f = CertificateFactory.getInstance("X.509");
X509Certificate certRSA = (X509Certificate)f.generateCertificate(is);
certRSA.checkValidity();
PublicKey pubKeyRSA = certRSA.getPublicKey();

Signature sig = Signature.getInstance("SHA256withRSA");
sig.initVerify(pubKeyRSA);

// supply the Signature object with the data for which a signature was generated --> hash of concat
sig.update(message);

boolean isValid = sig.verify( sigBytes );

System.out.println("The signature of the email verifies: " + isValid);

这是我得到的错误:

java.security.SignatureException: Signature length not correct: got 512 but was expecting 256
at sun.security.rsa.RSASignature.engineVerify(Unknown Source)
at java.security.Signature$Delegate.engineVerify(Unknown Source)
at java.security.Signature.verify(Unknown Source)

我做错了什么吗?我期望签名的长度为 256,而不是 512。我运行了一个测试,对签名值的子字符串进行匹配以匹配 256 的长度,但我没有收到上面的错误,但是 sig.verify 返回 false .

最佳答案

如果你看一下你写的代码有 System.out.println("check value bytes "+sigBytes.length);//--> "512" 然后是 boolean isValid = sig.verify( sigBytes ); 看起来你的 sigBytes 变量在你检查之前已经有 512 的长度

关于java - 签名长度不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41298305/

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