In Java you don't need to hash the message before signing or verifying it. That means that the bytes being sent to sig.update shouldn't be hashedBytes, but the inputBytes.
验证Java中在golang中生成的rsa.SignPKCS1v15签名
I am trying to get Java to verify a signed SHA-1 hash but it keeps returning false. I have the following code in Go which generates an RSA Key Pair and signs and returns any message that hits the /sign endpoint along with the hex encoded hash and the public key modulus and exponent:
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
)
var PrivKey *rsa.PrivateKey
type Message struct {
Message string `json:"message"`
}
func (msg *Message) Decode(r io.Reader) error {
return json.NewDecoder(r).Decode(&msg)
}
type Signature struct {
Hash string `json:"hash"`
Signature string `json:"signature"`
N string `json:"N"`
E string `json:"E"`
}
func hash(msg string) []byte {
sh := crypto.SHA1.New()
sh.Write([]byte(msg))
hash := sh.Sum(nil)
return hash
}
func SignWithKey(msg Message) Signature {
hash := hash(msg.Message)
bytes, err := rsa.SignPKCS1v15(rand.Reader, PrivKey, crypto.SHA1, hash)
if err != nil {
panic(err)
}
signature := hex.EncodeToString(bytes)
sig := Signature{
hex.EncodeToString(hash),
signature,
PrivKey.PublicKey.N.String(),
strconv.Itoa(PrivKey.PublicKey.E),
}
return sig
}
func sign(w http.ResponseWriter, r *http.Request) {
fmt.Println("/sign")
var msg Message
err := msg.Decode(r.Body)
if err != nil {
panic(err)
}
fmt.Println("Signing: " + msg.Message)
signature := SignWithKey(msg)
js, err := json.Marshal(signature)
fmt.Println(string(js))
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
func LoadKeys() {
// generate private key
var err error
PrivKey, err = rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
fmt.Println(err)
}
}
func main() {
fmt.Println("Loading Keys")
LoadKeys()
fmt.Println("Keys Loaded")
http.HandleFunc("/sign", sign)
http.ListenAndServe(":8080", nil)
}
On the Java/Android side I have this code which after sending the relevant bits hits this function with the unparsed JSON object, but once it gets to the Signature verify part it always returns false:
protected void onPostExecute(String result) {
if (result == null) {
tv.setText("NULL");
return;
}
JsonElement jelement = new JsonParser().parse(result);
JsonObject jobject = jelement.getAsJsonObject();
String signature = jobject.getAsJsonPrimitive("signature").getAsString();
BigInteger N = jobject.getAsJsonPrimitive("N").getAsBigInteger();
BigInteger E = jobject.getAsJsonPrimitive("E").getAsBigInteger();
String hash = jobject.getAsJsonPrimitive("hash").getAsString();
java.security.spec.RSAPublicKeySpec spec = new java.security.spec.RSAPublicKeySpec(N, E);
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pk = keyFactory.generatePublic(spec);
MessageDigest digest = MessageDigest.getInstance("SHA1");
byte[] inputBytes = msg.getBytes("UTF8");
byte[] hashedBytes = digest.digest(inputBytes);
Signature sig = Signature.getInstance("SHA1withRSA", "SC");
sig.initVerify( pk );
sig.update( hashedBytes );
boolean ret = sig.verify( Hex.decode(signature) );
if (ret) {
tv.setText(output + "Verified");
} else {
tv.setText(output + "NOT VERIFIED");
}
}
catch (Exception e) {
Log.i("error", e.toString());
}
}
}
- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答
1条回答
为你推荐
- 如何在golang中读取pkcs12内容,我在PHP中有示例
- php
- 1个回答
- Golang RSA加密,错误为“无法解析公钥”
- rsa
- encryption
- 1个回答
- 使用RSA进行AES密钥交换的AES加密通信
- rsa
- aes
- encryption
- 1个回答
- Ruby RSA公钥加密到Golang
- rsa
- ruby
- encryption
- 1个回答
- RSA在Java和Go之间进行加密和解密
- java
- rsa
- encryption
- 1个回答
- 开始-如何从字符串设置RSA公钥模数?
- rsa
- php
- 1个回答
- 在jwt-go中解析JWT Auth令牌时,密钥的类型无效
- rsa
- jwt
- 1个回答
- 为什么我用openssl和golang生成的RSA签名不同?
- rsa
- openssl
- 3个回答
- Golang使用rsa签署结构
- rsa
- cryptography
- 1个回答
- 验证Java中在golang中生成的rsa.SignPKCS1v15签名
- android
- java
- rsa
- 1个回答
- 使用golang解密使用php openssl_encrypt加密的文件
- openssl
- php
- encryption
- 1个回答
- 如何使用Go编程语言使用从PEM文件读取的RSA私钥进行加密?
- rsa
- pem
- 2个回答
- Golang令牌验证错误
- rsa
- jwt
- 4个回答
- 如何在Go和Android之间使用RSA
- android
- rsa
- encryption
- 1个回答
- 无法从Golang中的iOS解析base64 DER编码的ASN.1公钥
- rsa
- ios
- 1个回答
- 在Go中验证有效负载签名
- rsa
- cryptography
- 2个回答
- 如何验证appengine.SignBytes返回的签名?
- rsa
- 1个回答
- 使用RSA私钥加密消息(如OpenSSL的RSA_private_encrypt)
- rsa
- pem
- 4个回答