我的代码看起来像这样,我将此处的每个未定义方法都追溯到包含相关参数的调用函数。
我很难理解 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);
}