在我们开始之前,我有一个服务器和一个客户端。我希望向客户端发送一个加密字符串,其中包含服务器的 Diffie-Hellman 公共静态密钥和公共临时密钥。为此,我使用服务器的私有 RSA 密钥发送加密字符串,客户端使用服务器的公共 RSA 密钥解密。
现在我需要这样做的原因是因为服务器是唯一具有公钥/私钥对的服务器。这很好,因为使用一对密钥加密仍然会切断 MITM 攻击对 Diffie-Hellman 的一侧,并且根据我的要求,没关系。
将静态和临时密钥转换为十六进制编码字符串并通过套接字发送给我一个私钥加密阶段的问题。
我的服务器正在做:
DH2 dhA(dh);
SecByteBlock sprivA(dhA.StaticPrivateKeyLength()), spubA(
dhA.StaticPublicKeyLength());
SecByteBlock eprivA(dhA.EphemeralPrivateKeyLength()), epubA(
dhA.EphemeralPublicKeyLength());
dhA.GenerateStaticKeyPair(rnd, sprivA, spubA);
dhA.GenerateEphemeralKeyPair(rnd, eprivA, epubA);
string sendBuf, recvBuf;
string saEncoded, eaEncoded, encoding;
cout << "spubA: " << (char*) spubA.data() << endl << "epubA: "
<< (char*) epubA.data() << endl;
SecByteBlock nil;
nil.CleanNew(HMAC< SHA256 >::DEFAULT_KEYLENGTH);
HMAC< SHA256 > hmac;
hmac.SetKey(nil.data(), nil.size());
HashFilter filter(hmac, new HexEncoder(new StringSink(encoding)));
filter.Put(spubA.data(), spubA.size());
filter.MessageEnd();
saEncoded = encoding;
encoding = "";
filter.Put(epubA.data(), epubA.size());
filter.MessageEnd();
eaEncoded = encoding;
encoding = "";
// StringSource saSource(spubA, sizeof(spubA), true,
// new HexEncoder(new StringSink(saEncoded)));
//
// StringSource eaSource(epubA, sizeof(epubA), true,
// new HexEncoder(new StringSink(eaEncoded)));
//
sendBuf = saEncoded + " " + eaEncoded;
cout << "Send Buffer: " << sendBuf << endl;
SendMsg(sendBuf, tdata);
其中SendMsg()
包含加密过程。
它在这一点上失败:
void SendMsg( string sendBuf, struct ThreadData * tdata )
{
AutoSeededRandomPool rng;
Integer m, c, r;
stringstream ss;
try
{
// Encode the message as an Integer
m = Integer((const byte *) sendBuf.c_str(), sendBuf.size());
//Encrypt
c = tdata->privateKey.CalculateInverse(rng, m); //HERE!
带有错误消息:
InvertibleRSAFunction: computational error during private key operation
Diffie-Hellman 部分中当前未注释掉的代码是从HERE获得的。注释代码的问题在于,当客户端接收到十六进制编码的字符串时,它已经丢失了数据并且无法就共享密钥达成一致。但它确实通过套接字。
可以显示一个示例:
Server:
spubA: &a�|՜D2�tu�cJ����B�R�8�*i�x?N���p��Q�����K��+O �"��P:k�d|3�����6Z
epubA: 4v������M�E�`l�K��[dN�|Q^r�-ż�����A~D�>4$�9���"v�*:Y��s�O���J��ow�M�߬�C�9n�;���Z�D�6lp�V��oowZ��WSv��",��A3��XL��8��
Send Buffer: 2661DC7CD59C4432AF747584634AF69BE60298429C52C738 3476CBCCFAA3B0A14DBE45E3606CC84B171DAC1CCE5B644E
Client:
Recovered: 2661DC7CD59C4432AF747584634AF69BE60298429C52C738 3476CBCCFAA3B0A14DBE45E3606CC84B171DAC1CCE5B644E
SA: 2661DC7CD59C4432AF747584634AF69BE60298429C52C738
EA: 3476CBCCFAA3B0A14DBE45E3606CC84B171DAC1CCE5B644E
Decoded SA: &a�|՜D2�tu�cJ����B�R�8
Decoded EA: 4v������M�E�`l�K��[dN
对于这个实例,我尝试在客户端执行以下操作:
// Get spubA and epubA from server
recovered = recoverMsg(serverKey, sockServer);
//Calculate shared secret.
string sa, ea;
ss.str(recovered);
ss >> sa >> ea;
ss.str("");
ss.clear();
cout << "SA: " << sa << endl << "EA: " << ea << endl;
string decodedSA, decodedEA;
StringSource decodeSA(sa, true,
new HexDecoder(new StringSink(decodedSA)));
StringSource decodeEA(ea, true,
new HexDecoder(new StringSink(decodedEA)));
cout << "Decoded SA: " << decodedSA << endl;
cout << "Decoded EA: " << decodedEA << endl;
SecByteBlock spubA((const byte*) decodedSA.data(), decodedSA.size());
if ( spubA.size() < dhB.StaticPublicKeyLength() ) spubA.CleanGrow(
dhB.StaticPublicKeyLength());
else spubA.resize(dhB.StaticPublicKeyLength());
SecByteBlock epubA((const byte*) decodedEA.data(), decodedEA.size());
if ( epubA.size() < dhB.EphemeralPublicKeyLength() ) epubA.CleanGrow(
dhB.EphemeralPublicKeyLength());
else epubA.resize(dhB.EphemeralPublicKeyLength());
但我仍然得到相同的结果。
有人知道我如何用服务器的私钥加密它并正确地通过套接字发送吗?