我的代码看起来是这样的,我在这里跟踪了每个未定义的方法,找到了包含相关参数的调用函数。
我很难理解API。所以我在这里可以生成公钥/私钥,并且可以生成
一
会话密钥,但如何基于公钥生成会话密钥?
我在这里遗漏了什么/假设(错了)?
void Crypto::GenerateKeyPair( Buffer& publicKey, Buffer& privateKey ) throw(WinError)
{
/* CryptAcquireContext ( PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) */
CryptContext context = CryptoProviders::NewContext(PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
/* CryptGenKey( AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &key) */
CryptKey key = context.GenerateKeyExchangePair();
/* CryptExportKey( PUBLICKEYBLOB, pPub); */
key.ExportPublicKey(publicKey);
/* CryptExportKey( PRIVATEKEYBLOB, pPriv); */
key.ExportPrivateKey(privateKey);
}
void Crypto::GenerateSessionKey( Buffer& sessionKey ) throw(WinError)
{
/* CryptAcquireContext ( PROV_RSA_FULL, 0 ) */
CryptContext context = CryptoProviders::NewContext(PROV_RSA_FULL, 0);
/* CryptGenKey( CALG_RC4, CRYPT_EXPORTABLE ) */
/* CryptGetUserKey( AT_KEYEXCHANGE ) */
/* CryptExportKey( SIMPLEBLOB ) */
context.GenerateSessionKey(sessionKey);
}
void Crypto::EncryptData( const Buffer& publicKey, const Buffer& plaintext, Buffer& encrypted )
{
/* CryptAcquireContext ( PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) */
CryptContext hProvider(PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
/* CryptImportKey( 0 ) */
CryptKey key = hProvider.ImportKey(publicKey);
/* CryptEncrypt() */
key.Encrypt(plaintext, encrypted);
}
void Crypto::DecryptData( const Buffer& privateKey, const Buffer& encrypted, Buffer& plaintext )
{
/* CryptAcquireContext ( PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) */
CryptContext hProvider(PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
/* CryptImportKey( 0 ) */
CryptKey key = hProvider.ImportKey(privateKey);
/* CryptDecrypt() */
key.Decrypt(encrypted, plaintext);
}