用公钥解密

vad*_*adv -4 encryption openssl cryptography go

如何解密用 golang 中的私钥签名的消息?

$ openssl genrsa -out ./server/server.key
Generating RSA private key, 2048 bit long modulus
..................+++
.............................................+++

$ openssl rsa -in ./server/server.key -pubout -out ./client/client.pub
writing RSA key

$ echo "secret" | openssl rsautl -inkey ./server/server.key -sign > ./secret

# decrypt with public key
$ openssl rsautl -inkey ./client/client.pub -pubin -in ./secret
secret
Run Code Online (Sandbox Code Playgroud)

vad*_*adv 7

我完全理解我的问题,它是关于RSA_public_decryptopenssl 的方法:https ://www.openssl.org/docs/man1.1.0/crypto/RSA_public_decrypt.html

我没有发现任何纯 golang 实现。用cgo实现: https: //github.com/dgkang/rsa/blob/master/rsa/rsa.go

UPD,为我工作:

func RSA_public_decrypt(pubKey *rsa.PublicKey, data []byte) []byte {
    c := new(big.Int)
    m := new(big.Int)
    m.SetBytes(data)
    e := big.NewInt(int64(pubKey.E))
    c.Exp(m, e, pubKey.N)
    out := c.Bytes()
    skip := 0
    for i := 2; i < len(out); i++ {
        if i+1 >= len(out) {
            break
        }
        if out[i] == 0xff && out[i+1] == 0 {
            skip = i + 2
            break
        }
    }
    return out[skip:]
}
Run Code Online (Sandbox Code Playgroud)