无法在Java中验证由Go创建的DSA签名,反之亦然。

huangapple 未分类评论62阅读模式
英文:

Can't verify a DSA signature in Java that was created in Go, and vice versa

问题

我正在尝试在Java中验证由Go创建的签名,反之亦然。然而,在每种情况下,我都没有收到错误,但在验证函数中得到了false。

使用的公钥和私钥是相同的,参数也是相同的,我通过将Java密钥数据传递给Go并使用它来签名和验证数据来进行了检查 - 这是有效的。

我还检查了传入的数据(然后进行MD5哈希处理)在Java和Go中是否相同,并且生成的哈希值是否相同(以防万一) - 它们是相同的。

我知道Java使用ASN1编码进行签名,我找到了一个函数来从Go提供的单独的R和S值重新构建ASN1编码,我使用它作为模板将Java生成的ASN1字节数组拆分为Go的R和S值。在Java中,如果我拆分并重新构建ASN1字节数组,我可以验证签名,但无论我做什么,Go都无法验证Java签名,Java也无法验证由Go签名的签名。

它们在使用DSA算法的版本上有区别吗?我看到Java使用SHA1withDSA,而Go提到它符合FIPS 186-3规范。

英文:

I'm trying to verify a signature in Java that was created in Go, and vice versa. However, in each case I'm getting no error, but false on the verify function.

The public and private key being used are the same, as are the parameters, I have checked this by passing the Java key data to Go, and using it to sign and verify the data - this works.

I have also checked that the data being passed in (which is then MD5 hashed) is the same in both Java and Go, and that the resulting hash is the same (just in case) - they are.

I know that Java uses the ASN1 encoding for it's signature, and I found a function to rebuild this from the separate R and S values provided by Go, and I used that as a template to split the ASN1 byte[] generated by Java into R and S values for Go. In Java, if I split, and rebuild the ASN1 byte[], I can verify the signature, but no matter what I do, Go won't, and Java won't verify a signature signed by Go.

Is there a difference in the versions of the DSA algorithm they use? I see that Java uses SHA1withDSA and go mentions that it conforms to the FIPS 186-3 specitication

答案1

得分: 0

你不需要手动处理 ASN1 []bytes 的拆分,应该使用 crypto 包中的函数。

https://golang.org/pkg/crypto/x509/#Certificate.CheckSignature 看起来接受一个 SignatureAlgorithm 参数(DSAWithSHA1 是一个有效的值)。

英文:

You shouldn't have to handle splitting the ASN1 []bytes by hand, you should be using functions from the crypto pkg.

https://golang.org/pkg/crypto/x509/#Certificate.CheckSignature looks like it takes a SignatureAlgorithm as an arg (and DSAWithSHA1 is a valid value).

答案2

得分: -1

好的,我会为你翻译以下内容:

好的,DSA可能有所不同,但至少对于RSA,解决方案是Java不希望首先对值进行哈希处理(我猜它在内部进行了哈希处理),而GO则需要进行哈希处理。通过将函数更改为使用RSA而不是DSA,并考虑上述情况,这个解决方案有效。

https://stackoverflow.com/questions/29422738/verify-rsa-signpkcs1v15-signature-generated-in-golang-in-java

英文:

Ok, DSA may be different, but for RSA at least the solution was that Java didn't want the value to be hashed first (I guess it hashes internally) while GO did, changing the functions to use RSA rather than DSA and considering the above, this worked.

https://stackoverflow.com/questions/29422738/verify-rsa-signpkcs1v15-signature-generated-in-golang-in-java

huangapple
  • 本文由 发表于 2016年4月6日 03:59:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/36435908.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定