早上好,
我有一个通过UDP插座通信的客户端服务器应用程序。我想与RSA(编码键)和AES编码通信以编码数据报。
客户端位于C ,C#
中的服务器端我正在经常试图用RSA编码AES IV键,但是在C#中解码时会出现错误:
异常类型: system.security.cryptography.cryptographicexception
例外消息:加密_oaepdecoding
这是我的编码代码(客户端,C ) [编辑]将rsaes_pkcs1v15_encryptor更改为rsaes_oaep_sha_encryptor
const CryptoPP::Integer n("107289343054719278577597018805838066296333011963085747309982087864392842699433873606133118875978275304651444098131280023618603357722259282514858925191134541966986361546234507079678544203468616135436686852577772762581654429498496768721214543879181421353486700409082948114039206485653595743465270256058198245113.");
const CryptoPP::Integer e("17.");
[...]
void Crypto::CryptRSA(const std::string & bufferIn, std::string & bufferOut, const CryptoPP::Integer &n, const CryptoPP::Integer &e)
{
CryptoPP::AutoSeededRandomPool rnd;
CryptoPP::RSA::PublicKey pubKey;
pubKey.Initialize(n, e);
CryptoPP::RSAES_OAEP_SHA_Encryptor encryptor(pubKey);
size_t ecl = encryptor.CiphertextLength(bufferIn.size());
CryptoPP::SecByteBlock ciphertext(ecl);
encryptor.Encrypt(rnd, (CryptoPP::byte*)bufferIn.c_str(), bufferIn.size(), ciphertext);
bufferOut = std::string((char*)ciphertext.data(), ecl);
}
这是我的解码代码(服务器端。C#)
private static string _keyN = "107289343054719278577597018805838066296333011963085747309982087864392842699433873606133118875978275304651444098131280023618603357722259282514858925191134541966986361546234507079678544203468616135436686852577772762581654429498496768721214543879181421353486700409082948114039206485653595743465270256058198245113";
private static string _keyE = "17";
private static string _keyD = "50489102613985542860045655908629678257097887982628586969403335465596631858557116991121467706342717790424208987355896481702872168339886721183463023619357421741798172532326737925480536247565713413538718832057918801452980775480097195493999319542331774866185094818177243836015292183598722700529776296282728256145";
[...]
public static byte[] DecryptRSA(byte[] encrypted)
{
BigInteger n, e, d;
BigInteger.TryParse(_keyN, out n);
BigInteger.TryParse(_keyE, out e);
BigInteger.TryParse(_keyD, out d);
CspParameters csp = new CspParameters();
csp.KeyContainerName = "RSA Test (OK to Delete)";
csp.ProviderType = 1;
csp.KeyNumber = 1;
var rsa = new RSACryptoServiceProvider(csp);
rsa.PersistKeyInCsp = false;
var param = new RSAParameters()
{
Modulus = n.ToByteArray().Skip(1).ToArray(),
Exponent = e.ToByteArray().Skip(1).ToArray(),
D = d.ToByteArray().Skip(1).ToArray(),
};
rsa.ImportParameters(param);
return rsa.Decrypt(encrypted.ToArray(), true);
}
所以,我想知道我在代码中做错了什么。
在C 中,我可以编码和解码我的数据报,但是当我尝试使用C#解码时,它确实不起作用。
谢谢您的英语不好。
biginteger.tobytearray将值返回为 little-endian 。
虽然RSAParameters
字段确实是大端(如下所述)。
您可以尝试以下操作:
Modulus = n.ToByteArray().Reverse().ToArray()