如何交换公钥
How to exchange public keys?
我有客户端和服务器,并希望在它们之间有加密通信通道。下面是一些生成公钥和私钥的简单代码:
RSA::PrivateKey privateKey;
privateKey.Initialize()
///////////////////////////////////////
// Generate Parameters
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(this->rng, 3072);
///////////////////////////////////////
// Generated Parameters
const Integer& n = params.GetModulus();
const Integer& p = params.GetPrime1();
const Integer& q = params.GetPrime2();
const Integer& d = params.GetPrivateExponent();
const Integer& e = params.GetPublicExponent();
params.
///////////////////////////////////////
// Dump
cout << "RSA Parameters:" << endl;
cout << " n: " << n << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
cout << " d: " << d << endl;
cout << " e: " << e << endl;
cout << endl;
///////////////////////////////////////
// Create Keys
RSA::PrivateKey privateKey(params);
RSA::PublicKey publicKey(params);
但是我如何在服务器端获得客户端的公钥和在客户端获得服务器端的公钥呢?是否存在比在文件中序列化公钥,在另一端发送文件,接收文件并反序列化更简单的方法?
类Chat
有DataTransferingInterface
实例,指向Server
或Client
(取决于用户在开始时是否选择)。一些理解代码:
类聊天:
class Chat : public OwnerServerInterface, public OwnerClientInterface
public:
//a lot of methods
protected:
virtual void handshakeServerSide(int clientSocket, void *objectForSaveIn, void *dataToSend);
virtual void handshakeClientSide(int serverSocket, void *objectForSaveIn, void *dataToSend);
private:
DataTransferingInterface* interface;
在handshakeServerSide()
和handshakeClientSide()
方法中,服务器的Chat
实例和客户端的Chat
实例进行握手(数据交换)。在这种方法中,服务器必须发送自己的公钥,并获取客户端的公钥。但是怎么做呢?
…我如何在服务器端获得客户端的公钥,在客户端获得服务器的公钥?
这就是众所周知的密钥分配问题。在小范围内,这可能是(也可能不是)可控的,但当你扩大规模时,这是一个非常棘手的问题。
答案取决于您的特定实例问题,这看起来像一个N-way聊天程序。在Stack Overflow这样的编程网站上,这个问题实在太多了。
也许你应该仔细研究一下这个问题,包括多方或群迪菲-赫尔曼方案。还要考虑一下,当一个成员离开一个团队时,你希望发生什么。然后在信息安全堆栈交换上提出有针对性的问题
下面的代码是非常简单的Crypto++ RSA算法的类包装器:
EncoderRSA.h
class EncoderRSA
{
public:
EncoderRSA();
void keyGeneration();
void substitutePublicKey(Integer e, Integer n);
Integer encode(std::string plainText);
std::string decode(Integer cypher);
Integer getE();
Integer getN();
private:
AutoSeededRandomPool prng;
RSA::PublicKey publicKey; // For encrypt plain text
RSA::PrivateKey privateKey; // For decrypt plain text
};
EncoderRSA.cpp
void EncoderRSA::keyGeneration() {
InvertibleRSAFunction params;
params.GenerateRandomWithKeySize(this->prng, 3072);
RSA::PrivateKey privateKey(params);
RSA::PublicKey publicKey(params);
//because you can't do: this->privateKey(params);
this->privateKey = privateKey;
this->publicKey = publicKey;
}
void EncoderRSA::substitutePublicKey(Integer e, Integer n) {
this->publicKey.Initialize(n, e);
}
Integer EncoderRSA::encode(std::string plainText) {
Integer m((const byte*)plainText.data(), plainText.size());
Integer c = this->publicKey.ApplyFunction(m);
return c;
}
std::string EncoderRSA::decode(Integer cypher) {
Integer r = this->privateKey.CalculateInverse(this->prng, cypher);
std::string decoded;
size_t req = r.MinEncodedSize();
decoded.resize(req);
r.Encode((byte*)decoded.data(), decoded.size());
return decoded;
}
Integer EncoderRSA::getE() {
return this->publicKey.GetPublicExponent();
}
Integer EncoderRSA::getN() {
return this->publicKey.GetModulus();
}
服务器必须生成自己的密钥(公钥和私钥),保存它,单独保存自己的公钥(如果确切:PublicExponent和PublicModulus)(在Integer
变量中),从客户端获取公钥,用客户端的公钥替换自己的公钥。
客户端也一样
- 将TPM公钥序列化为DER或PEM
- 使用公钥加密消息:BER 解码错误
- 比较两个公钥与OpenSSL API
- 以 x64 配置访问证书公钥
- 使用 char 数组公钥的 OpenSSL 进行 RSA 加密
- 如何获取两个 std::map 的公钥?
- BERDecodeError尝试使用公钥验证签名时出错
- 从字符串 Crypto++ 导入 RSA 公钥/私钥
- 使用 gpgme 在 c++ 中导出 gpg 公钥
- 如何在C++中将 JWK 公钥转换为 PEM 格式
- 打印私人/公钥
- 一代 RSA-2048 密钥(公钥和私钥)
- RSA公共加密使用X509证书C 的公钥
- 加密 和Python之间的Diffie-Hellman密钥交换
- 在 SSH 上的公钥-私钥身份验证上的 libcurl 实现
- 使用 WinHttpQueryOption 获取公钥的大小
- 使用公钥 X509 V3 (PKCS7) 使用 AES 128 模式 cbc 加密文件
- Libgit2 - 无法验证 SSH 会话:无法打开公钥文件
- 使用Crypto++库仅从x压缩坐标检索ECDSA公钥
- 如何交换公钥