英文:
"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 '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
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 (
"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
}
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('RSA/OAEP')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论