我有一些客户端/服务器应用程序。它工作正常。我想为其添加密码学。我找到 Crypto++ 库,并使用它制作一些简单的项目:用于 DES 编码和 RSA 编码。它包含两个类:EncoderDes
和 EncoderRSA
:
class EncoderDES
{
public:
EncoderDES();
std::string encode(std::string plainText);
std::string decode(std::string cypher);
std::string toReadable(std::string cypher);
void doIt();
private:
AutoSeededRandomPool prng;
SecByteBlock key;
byte iv[];
};
class EncoderRSA
{
public:
EncoderRSA();
void keyGeneration();
void substitutePublicKey(Integer e, Integer n);
Integer encode(std::string plainText);
std::string decode(Integer cypher);
private:
AutoSeededRandomPool prng;
RSA::PublicKey publicKey;
RSA::PrivateKey privateKey;
};
我认为,服务器必须生成DES key ,并通过RSA将其传递给每个客户端。在这一步中我有一些问题: 1. 如何发送(以及如何接收)RSA::PublicKey? 2. 如何发送(以及如何接收)SecByteBlock?
(我无法发送它们,因为我不能: 1. 将 RSA::PublicKey 转换为 char* 2. 将 char* 转换为 RSA::PublicKey 3.将SecByteBlock转换为字符串。)
我只能将字符串转换为 SecByteBlock:
SecByteBlock stringToKey(string decodedKey) {
SecByteBlock receivedKey(decodedKey.data(), decodedKey.size());
return receivedKey;
}
但不确定它是否正确。
如何解决这个问题?
最佳答案
I have some Client/Server application...
在你深入兔子洞之前,先废弃大部分现有设计。
要加密客户端和服务器之间的通信,请使用 Integrated Encryption Scheme 。 Crypto++ 有两个。第一个是Elliptic Curve Integrated Encryption Scheme ,并且它在椭圆曲线域上运行。第二个是 Discrete Logarithm Integrated Encryption Scheme ,并且它在整数域上运行。
这两种方案都是最先进的,它们将密文(和解密)与对方的公钥联系起来。明文经过加密,密文经过 MAC 处理,因此提供 confidentiality and authenticity 。它们还确保您不会重复使用安全上下文。集成加密方案是您可以使用的最安全的方案之一,因为它们是 IND-CCA2 (这是一个非常强烈的安全概念)。
您仍然需要解决 key distribution problem从你之前的问题来看。但是一旦你有了公钥,系统就基本上可以工作了。我只能说“大部分”,因为我不知道你在做什么来防止网络级别的插入和重放。
I can't send them, because I can't: 1. convert RSA::PublicKey to char* 2. convert char* to RSA::PublicKey 3. convert SecByteBlock to string.
您应该访问Keys and Formats在 Crypto++ wiki 上。它向您展示了如何序列化它们。另请参阅Safe way to sending a public key over a socket关于堆栈溢出。
快速朝正确方向推进:使用保存
和加载
;并且不要使用DEREncode
或BERDecode
。 Save
和Load
对主体公钥信息进行操作,除了 key 之外还包括算法标识符。这通常会对您以后有所帮助,因为 key 类型是公钥的一部分。
关于c++ - 序列化公钥以通过网络发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633527/