求助各位大神,java 当中的
RSAPrivateCrtKeySpec rsaPriKey = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient);
KeyFactory keyf = KeyFactory.getInstance("RSA");
PrivateKey key = keyf.generatePrivate(rsaPriKey);
String input = "测试";
Signature signature = Signature.getInstance("SHA1withRSA");
signature.initSign(key);
signature.update(input.getBytes("utf-8"));
用 go 怎么实现,有没有相应的包?尤其是第一部分根据一堆参数生成 rsa key 的部分没有找到 go 怎么写。
1
neighbads 2019-05-17 15:39:44 +08:00
|
2
usufu OP 没找到该用那个方法
|
3
qwerthhusn 2019-05-17 17:34:31 +08:00
其实 RSA 私钥就两个字段 PrivateExponent 和 modules,只要有个这两个其他的就能算出来
|
4
usufu OP 研究了一下,解决了。记录如下:
``` pubkey := rsa.PublicKey{ N:Modulus, E:int(Exponent.Int64()), } preValues := rsa.PrecomputedValues{ Dp:DP, Dq:DQ, Qinv:InverseQ, } priKey := rsa.PrivateKey{ PublicKey: pubkey, D:D, Primes: []*big.Int{P, Q}, Precomputed:preValues, } derBytes := x509.MarshalPKCS1PrivateKey(&priKey) PrivateKey, _ := x509.ParsePKCS1PrivateKey(derBytes) rand := rand.Reader msg := []byte("the message to be signed...") hashed := sha1.Sum(msg) signbyte, err := rsa.SignPKCS1v15(rand, PrivateKey, crypto.SHA1, hashed[:]) if err != nil { log.Fatal(err) } signature := base64.StdEncoding.EncodeToString(signbyte) fmt.Println(signature) expresult := `NDarItrQEHlpT6jAOmz/OgL/nahM1tesCQyN3NcYU8b+O0QPElP3hIQJlYklZnxJaoUZON+8Ja5R+b3lODcsXesRm81bypYCzF5sjPu8P2jvS4A4RE2T3+Pn+JRgUhu5ilxZcrCVYDt1buKcN5uC+VEb+ixSgvWQHY835UCGqow=` if strings.Compare(signature, expresult) == 0 { fmt.Println("signature correct...") } else { fmt.Println("Signature NOT correct...") } ``` |