簡體   English   中英

在 dart (pointycastle) 和 golang 中嘗試使用 rsa 加密和解密時出現“crypto/rsa: decryption error”

[英]"crypto/rsa: decryption error" when trying to use rsa encryption and decryption in dart (pointycastle) and golang

我正在嘗試加密 dart 中的消息(使用 pointycastle)並在 golang 中解密(使用標准庫)。 私鑰和公鑰確實匹配。 消息通過 TCP 發送。

dart 代碼:

// import 'package:encrypt/encrypt.dart' as enc;
final publicKey =
    enc.RSAKeyParser().parse(serverRsaPublicKey) as RSAPublicKey;

final rsaEncrypter = AsymmetricBlockCipher('RSA/OAEP')
  ..init(true, PublicKeyParameter<RSAPublicKey>(publicKey));
final ciphertext =
    rsaProcessInBlocks(rsaEncrypter, Uint8List.fromList(utf8.encode('Some message')));

tcpSendMessage(ciphertext); // Dummy function

rsaProcessInBlocks是 pointycastle 的 rsa 教程中使用的 function ( https://github.com/bcgit/pc-dart/blob/master/tutorials/rsa.md - _processInBlocks )

高朗代碼:

/*
import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/sha256"
    "crypto/x509"
    "encoding/pem"
)
*/

var block *pem.Block
block, _ = pem.Decode([]byte(RSA_PRIVATE_KEY))
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
    println(err.Error())
    return
}

ciphertext := TcpGetMessage() // Dummy function
plaintext, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, privateKey, ciphertext, []byte(""))
if err != nil {
    println(err.Error()) // Error happens here
    return
}

我在 golang 中收到以下錯誤: crypto/rsa: decryption error

我用每種語言獨立測試了加密和解密,它工作正常。 我還測試了消息是否通過 tcp(它是)正確發送

我猜是內部使用了不同的算法,或者庫使用了不同的 PKCS 版本

我嘗試更深入地研究錯誤,但出於安全原因,golang 沒有透露它。

任何幫助將不勝感激。

正如 Topaco ( https://stackoverflow.com/users/9014097/topaco ) 所說, dart 代碼中的摘要沒有明確指定,所以我必須使用OAEPEncoding.withSHA256(RSAEngine())而不是AsymmetricBlockCipher('RSA/OAEP')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM