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

huangapple go评论91阅读模式
英文:

"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教程中使用的函数(https://github.com/bcgit/pc-dart/blob/master/tutorials/rsa.md - _processInBlocks

Golang代码:

/*
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不会透露错误的详细信息。

非常感谢任何帮助。

英文:

I'm trying to encrypt a message in dart (using pointycastle) and decrypt it in golang (using the standard library). The private and public keys do match. The message is sent over TCP.

dart code:

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

final rsaEncrypter = AsymmetricBlockCipher(&#39;RSA/OAEP&#39;)
  ..init(true, PublicKeyParameter&lt;RSAPublicKey&gt;(publicKey));
final ciphertext =
    rsaProcessInBlocks(rsaEncrypter, Uint8List.fromList(utf8.encode(&#39;Some message&#39;)));

tcpSendMessage(ciphertext); // Dummy function

rsaProcessInBlocks is the function used in the pointycastle's rsa tutorial (https://github.com/bcgit/pc-dart/blob/master/tutorials/rsa.md - _processInBlocks)

golang code:

/*
import (
	&quot;crypto/rand&quot;
	&quot;crypto/rsa&quot;
	&quot;crypto/sha256&quot;
	&quot;crypto/x509&quot;
	&quot;encoding/pem&quot;
)
*/

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(&quot;&quot;))
if err != nil {
    println(err.Error()) // Error happens here
    return
}

I'm getting the following error in golang: crypto/rsa: decryption error.

I tested the encryption and decryption independently in each language and it works alright. I also tested whether the message is sent properly over tcp (which it is)

I'm guessing that a different algorithm is used internally or that the libraries are using different PKCS versions

I tried looking deeper into the error but golang doesn't reveal it for security reasons.

Any help would be greatly appreciated.

答案1

得分: 0

根据Topaco(https://stackoverflow.com/users/9014097/topaco)的说法,Dart代码中没有明确指定摘要,所以我必须使用OAEPEncoding.withSHA256(RSAEngine())而不是AsymmetricBlockCipher('RSA/OAEP')

英文:

as Topaco (https://stackoverflow.com/users/9014097/topaco) said, the digest in the dart code is not explicitly specified, so I have to use OAEPEncoding.withSHA256(RSAEngine()) instead of AsymmetricBlockCipher(&#39;RSA/OAEP&#39;)

huangapple
  • 本文由 发表于 2023年1月12日 01:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75086624.html
匿名

发表评论

匿名网友

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

确定