我试图通过套接字将签名消息从套接字发送到客户端。我在验证邮件时遇到问题。我尝试了基于wiki上提供的代码示例的过滤和非过滤方式。我尝试了几乎所有可以在谷歌上找到的东西,但我却遭受了沉重的打击。无法验证消息RSA签名套接字方案
如何在套接字上执行此签名验证方案?为了展示我一直在尝试做的事情,我留下了所有我一直想要评论的内容。
SERVER:
try
{
AutoSeededRandomPool rng;
stringstream ss;
string signature;
// byte* signature = new byte[tdata->signer.MaxSignatureLength()];
// if(NULL == signature)
// {
// throw Exception(Exception::OTHER_ERROR, "Invalid Signature");
// }
//
// size_t length = tdata->signer.SignMessage(rng, (const byte*) sendBuf.c_str(),
// sendBuf.length(), signature);
//string signature;
// byte* message = (byte*) sendBuf.c_str();
// SecByteBlock signature(tdata->signer.MaxSignatureLength(sendBuf.size()));
//
// size_t signatureLen = tdata->signer.SignMessageWithRecovery(rng, message,
// sendBuf.size(), NULL, 0, signature);
//
// cout << "Signature Length: " << signatureLen << endl;
// ss << signatureLen;
// string len = ss.str();
// string send;
// send = (char*)signature.data();
// send += " " + len;
StringSource(sendBuf, true,
new SignerFilter(rng, tdata->signer, new StringSink(signature),
true // putMessage
)// SignerFilter
);// StringSource
// cout << "Signature: " << std::hex << signature << endl;
// // Encode the message as an Integer
// m = Integer((const byte *) sendBuf.c_str(), sendBuf.size());
//
// //Encrypt
// c = tdata->privateKey.CalculateInverse(rng, m);
//
// //Turn the encrypted value into a string
// ss << c;
// sendBuf = ss.str();
// ss.str("");
// ss.clear();
// cout << "Cipher Sent: " << sendBuf << endl;
tdata->sockSource.Send((byte*)signature.c_str(), sizeof(signature));
这里是客户端
CLIENT:
string signature, recovered;
stringstream ss("");
AutoSeededRandomPool rng;
byte byteBuf[ 1000 ];
try
{
// Retrieve message from socket
cout << "Waiting for reply from server..." << endl;
sockServer.Receive(byteBuf, sizeof(byteBuf));
ss << byteBuf;
string signature = ss.str();
ss.str("");
ss.clear();
// byte sig[2048];
// bool result = verifier.VerifyMessage((const byte*)message.c_str(),
// message.length(), sig, 2048);
//
// if(true == result)
// {
// cout << "Message Verified" << endl;
// }
// else
// {
// cout << "Message Verification Failed" << endl;
// }
// cout << endl << std::hex << "Cipher Received: " << byteBuf << endl;
//
// SecByteBlock recovered(
// verifier.MaxRecoverableLengthFromSignatureLength(length));
//
// DecodingResult result = verifier.RecoverMessage(recovered, NULL, 0,
// byteBuf, length);
//
// if (!result.isValidCoding)
// {
// throw Exception(Exception::OTHER_ERROR, "Invalid Signature");
// }
//
// ////////////////////////////////////////////////
// // Use recovered message
// size_t recoveredLen = result.messageLength;
//
// assert(
// 0
// == memcmp(byteBuf, (const byte*)recovered,
// std::min(sizeof(byteBuf), recoveredLen)));
// Convert message to a string
// StringSource ss1(byteBuf, sizeof(byteBuf), true,
// new StringSink(temp));
//
// ss << byteBuf;
// signature = ss.str();
// ss.str("");
// ss.clear();
//
// cout << "Signature received: " << std::hex << signature << endl;
//
StringSource(signature, true,
new SignatureVerificationFilter(verifier,
new StringSink(recovered),
SignatureVerificationFilter::THROW_EXCEPTION
| SignatureVerificationFilter::PUT_MESSAGE) // SignatureVerificationFilter
);// StringSource
cout << "Verified signature on message" << endl;
return recovered;
// return recovered;
// //Convert the string to an Integer so we can calculate the inverse
// c = Integer(recBuf.c_str());
// r = serverKey.ApplyFunction(c);
// cout << "r: " << r << endl;
//
// // Recover the original message
// size_t req = r.MinEncodedSize();
// recovered.resize(req);
// r.Encode((byte *) recovered.data(), recovered.size());
// cout << "Recovered: " << recovered << endl;
}
catch (Exception& e)
{
cerr << "caught Exception..." << endl;
cerr << e.what() << endl;
sockServer.ShutDown(SHUT_RDWR);
return "";
}
在移动到套接字之前,您应该先练习本地文件。也许谷歌[Protocol Buffers](http://developers.google.com/protocol-buffers/)将有助于抽象出套接字细节。他们也可以工作,所以您不必担心清除代码中的低级错误。 – jww