序列化 public 密钥以通过网络发送
Serializing a public key for sending over network
我有一些 Client/Server 申请。它工作正常。我想给它添加密码学。我找到了 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密钥,并通过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++ has two of them. The first is Elliptic Curve Integrated Encryption Scheme, and it operates over the field of elliptic curves. The second is Discrete Logarithm Integrated Encryption Scheme,它在整数字段上运行。
这两种方案都是最先进的,它们将密文(和解密)与对方的 public 密钥联系起来。明文是加密的,密文是MAC',所以它提供了confidentiality and authenticity. They also ensure you don't reuse a security context. Integrated Encryption Schemes are some of the most secure schemes you can use because they are IND-CCA2(这是一个非常强大的安全概念)。
您还需要解决之前问题中的 key distribution problem。但是一旦你有了 public 键,系统就可以正常工作了。我只能说"mostly"因为我不知道你在网络级别做了什么来防止插入和重放。
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.
您应该访问 Stack Overflow 上的 Keys and Formats on the Crypto++ wiki. It shows you how to serialize them. Also see Safe way to sending a public key over a socket。
向正确的方向快速推进:使用 Save
和 Load
; 不要使用DEREncode
或BERDecode
。 Save
和 Load
对主题 public 密钥信息进行操作,它除了密钥之外还包括算法标识符。这通常会在以后帮助您,因为密钥类型是 public 密钥的一部分。
我有一些 Client/Server 申请。它工作正常。我想给它添加密码学。我找到了 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密钥,并通过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++ has two of them. The first is Elliptic Curve Integrated Encryption Scheme, and it operates over the field of elliptic curves. The second is Discrete Logarithm Integrated Encryption Scheme,它在整数字段上运行。
这两种方案都是最先进的,它们将密文(和解密)与对方的 public 密钥联系起来。明文是加密的,密文是MAC',所以它提供了confidentiality and authenticity. They also ensure you don't reuse a security context. Integrated Encryption Schemes are some of the most secure schemes you can use because they are IND-CCA2(这是一个非常强大的安全概念)。
您还需要解决之前问题中的 key distribution problem。但是一旦你有了 public 键,系统就可以正常工作了。我只能说"mostly"因为我不知道你在网络级别做了什么来防止插入和重放。
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.
您应该访问 Stack Overflow 上的 Keys and Formats on the Crypto++ wiki. It shows you how to serialize them. Also see Safe way to sending a public key over a socket。
向正确的方向快速推进:使用 Save
和 Load
; 不要使用DEREncode
或BERDecode
。 Save
和 Load
对主题 public 密钥信息进行操作,它除了密钥之外还包括算法标识符。这通常会在以后帮助您,因为密钥类型是 public 密钥的一部分。