本文整理汇总了C++中CryptDeriveKey函数的典型用法代码示例。如果您正苦于以下问题:C++ CryptDeriveKey函数的具体用法?C++ CryptDeriveKey怎么用?C++ CryptDeriveKey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了CryptDeriveKey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: DeriveKey
bool Encryption::DeriveKey()
{
if(!CryptDeriveKey(hCryptProv, CALG_RC2, hHash, CRYPT_EXPORTABLE, &hKey))
return false;
return true;
}
示例2: init
void Crytography:: init()
{
hProv = 0;
hKey = 0;
hXchgKey = 0;
hHash = 0;
//status = FALSE;
//_szPassword;
//_szPassword = "khoa";
CString Loi="";
if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0)) {
// printf("Error %x during CryptAcquireContext!\n", GetLastError());
AfxMessageBox("Error during CryptAcquireContext!",MB_OK);
return;
}
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
// printf("Error %x during CryptCreateHash!\n", GetLastError());
//MessageBox("Error %x during CryptCreateHash!\n"+GetLastError());
AfxMessageBox("Error during CryptCreateHash!",MB_OK);
return;
//goto done;
}
// Hash in the password data.
if(!CryptHashData(hHash, (const unsigned char *)_szPassword, (DWORD)strlen(_szPassword), 0)) {
//MessageBox("Error %x during CryptHashData!\n"+GetLastError());
AfxMessageBox("Error during CryptHashData!",MB_OK);
return;
//printf("Error %x during CryptHashData!\n", GetLastError());
//goto done;
}
// Derive a session key from the hash object.
if(_typeOfCrytography=="RC2")
{
if(!CryptDeriveKey(hProv, ENCRYPT_ALGORITHM1, hHash, KEYLENGTH, &hKey)) {
//MessageBox("Error %x during CryptDeriveKey!\n"+GetLastError());
AfxMessageBox("Error during CryptDeriveKey!",MB_OK);
return;
// printf("Error %x during CryptDeriveKey!\n", GetLastError());
// goto done;
}
}
else//RC4
{
if(!CryptDeriveKey(hProv, ENCRYPT_ALGORITHM2, hHash, KEYLENGTH, &hKey)) {
//MessageBox("Error %x during CryptDeriveKey!\n"+GetLastError());
AfxMessageBox("Error during CryptDeriveKey!",MB_OK);
return;
// printf("Error %x during CryptDeriveKey!\n", GetLastError());
// goto done;
}
}
// Destroy the hash object.
CryptDestroyHash(hHash);
hHash = 0;
}
示例3: good2
/* good2() reverses the bodies in the if statement */
static void good2()
{
if(staticTrue)
{
{
BYTE payload[100];
DWORD payloadLen = strlen(PAYLOAD);
HCRYPTPROV hCryptProv = (HCRYPTPROV)NULL;
HCRYPTHASH hHash = (HCRYPTHASH)NULL;
HCRYPTKEY hKey = (HCRYPTKEY)NULL;
char hashData[100] = HASH_INPUT;
do
{
/* Copy plaintext into payload buffer */
memcpy(payload, PAYLOAD, payloadLen);
/* Aquire a Context */
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0))
{
break;
}
/* FIX: All required steps are present */
/* Create hash handle */
if(!CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hHash))
{
break;
}
/* Hash the input string */
if(!CryptHashData(hHash, (BYTE*)hashData, strlen(hashData), 0))
{
break;
}
/* Derive an AES key from the hash */
if(!CryptDeriveKey(hCryptProv, CALG_AES_256, hHash, 0, &hKey))
{
break;
}
/* Encrypt the payload */
if(!CryptEncrypt(hKey, 0, 1, 0, payload, &payloadLen, sizeof(payload)))
{
break;
}
}
while (0);
if (hKey)
{
CryptDestroyKey(hKey);
}
if (hHash)
{
CryptDestroyHash(hHash);
}
if (hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
}
/* Do something with the encrypted data */
printBytesLine(payload, payloadLen);
}
}
}
开发者ID:gpwi970725,项目名称:testJuliet2,代码行数:61,代码来源:CWE325_Missing_Required_Cryptographic_Step__w32_CryptCreateHash_05.c
示例4: Encrypt
/*
* Simple, native encryption using RC4, inspired from one of Howard's
* old books.
*
* In a nutshell: Acquire the CSP handle, create an empty hash, put
* the hash of the key in it, derive the crypto key from it. Note
* (from Howard) the key also stores the algorithm in order to
* perform the encryption.
*
* Return: TRUE if completed successfully, FALSE otherwise.
*/
BOOL Encrypt(LPBYTE bKey, LPBYTE bPlaintext, LPBYTE bCipherText, DWORD dwHowMuch) {
HCRYPTPROV hProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
LPBYTE lpExpKey;
DWORD dwBuff = dwHowMuch;
CopyMemory(bCipherText, bPlaintext, dwHowMuch);
lpExpKey = Expand(bKey);
if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT))
return FALSE;
if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
return FALSE;
if (!CryptHashData(hHash, lpExpKey, strlen((char *) lpExpKey), 0))
return FALSE;
if (!CryptDeriveKey(hProv, CALG_RC4, hHash,
CRYPT_EXPORTABLE,
&hKey))
return FALSE;
if (!CryptEncrypt(hKey, 0, TRUE, 0,
bCipherText,
&dwBuff,
dwHowMuch))
return FALSE;
if (hKey) CryptDestroyKey(hKey);
if (hHash) CryptDestroyHash(hHash);
if (hProv) CryptReleaseContext(hProv, 0);
return TRUE;
}
示例5: CreateKey
HCRYPTKEY CreateKey(const BYTE *a1, DWORD a2)
{
HCRYPTKEY result;
HCRYPTHASH v3;
HCRYPTKEY phKey;
v3 = 0;
if ( a1 && a2 )
{
if ( Init() )
{
if ( CryptCreateHash(g_hProv, 0x8004u, 0, 0, &v3) )
{
if ( CryptHashData(v3, a1, a2, 0) )
CryptDeriveKey(g_hProv, 0x6801u, v3, 0, &phKey);
CryptDestroyHash(v3);
}
result = phKey;
}
else
{
result = -1;
}
}
else
{
result = 0;
}
return result;
}
示例6: swCryptDeriveKey
//-----------------------------------------------------------------------------
// swCryptDeriveKey()
//-----------------------------------------------------------------------------
// Calcul de la clé de chiffrement des mots de passe par dérivation du mot de
// passe maitre
//-----------------------------------------------------------------------------
// [in] cszMasterPwd = mot de passe maitre
// [out] hKey = handle de clé
// [out] pAESKeyData = bloc de données pour ceux qui voudraient reconstruire la clé
//-----------------------------------------------------------------------------
// Retour : 0 si OK
//-----------------------------------------------------------------------------
int swCryptDeriveKey(const char *pszMasterPwd,HCRYPTKEY *phKey,BYTE *pAESKeyData,BOOL bForceOldDerivationFunction)
{
TRACE((TRACE_ENTER,_F_,""));
BOOL brc;
int rc=-1;
HCRYPTHASH hHash=NULL;
// si la clé a déjà été créée, on la libère
if (*phKey!=NULL) { CryptDestroyKey(*phKey); *phKey=NULL; }
// création d'un hash
brc=CryptCreateHash(ghProv,CALG_SHA1,0,0,&hHash);
if (!brc) { TRACE((TRACE_ERROR,_F_,"CryptCreateHash()")); goto end; }
if (bForceOldDerivationFunction)
{
// hash du mot de passe
brc=CryptHashData(hHash,(unsigned char*)pszMasterPwd,strlen(pszMasterPwd),0);
if (!brc) { TRACE((TRACE_ERROR,_F_,"CryptHashData()")); goto end; }
// dérivation
brc=CryptDeriveKey(ghProv,CALG_AES_256,hHash,0,phKey);
TRACE((TRACE_INFO,_F_,"CryptDeriveKey(CALG_AES_256)"));
}
else if (atoi(gszCfgVersion)<93)
{
// hash du mot de passe
brc=CryptHashData(hHash,(unsigned char*)pszMasterPwd,strlen(pszMasterPwd),0);
if (!brc) { TRACE((TRACE_ERROR,_F_,"CryptHashData()")); goto end; }
// dérivation
brc=CryptDeriveKey(ghProv,CALG_3DES,hHash,0,phKey);
TRACE((TRACE_INFO,_F_,"CryptDeriveKey(CALG_3DES)"));
}
else // nouveau mécanisme +secure en 0.93 (PBKDF2) ISSUE#56
{
// Obtient 256 bits (32 octets) auprès de PBKDF2 pour générer une clé AES-256
if (!swIsPBKDF2KeySaltReady()) { TRACE((TRACE_ERROR,_F_,"swIsPBKDF2SaltReady()=FALSE")); goto end; }
if (swPBKDF2(pAESKeyData,AES256_KEY_LEN,pszMasterPwd,gSalts.bufPBKDF2KeySalt,PBKDF2_SALT_LEN,10000)!=0) goto end;
if (swCreateAESKeyFromKeyData(pAESKeyData,phKey)!=0) goto end;
}
if (!brc) { TRACE((TRACE_ERROR,_F_,"CryptDeriveKey()=0x%08lx",GetLastError())); goto end; }
rc=0;
end:
if (hHash!=NULL) CryptDestroyHash(hHash);
TRACE((TRACE_LEAVE,_F_,"rc=%d",rc));
return rc;
}
示例7: aes_decrypt
BOOL aes_decrypt(BYTE *inbuf, BYTE *outbuf, size_t buf_size, LPCWSTR key_str, size_t key_len)
{
if (inbuf == NULL || outbuf == NULL) return FALSE;
BOOL dwStatus = FALSE;
BOOL bResult = FALSE;
wchar_t info[] = L"Microsoft Enhanced RSA and AES Cryptographic Provider";
HCRYPTPROV hProv;
if (!CryptAcquireContextW(&hProv, NULL, info, PROV_RSA_AES, CRYPT_VERIFYCONTEXT)){
dwStatus = GetLastError();
printf("CryptAcquireContext failed: %x\n", dwStatus);
CryptReleaseContext(hProv, 0);
return dwStatus;
}
HCRYPTHASH hHash;
if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash)){
dwStatus = GetLastError();
printf("CryptCreateHash failed: %x\n", dwStatus);
CryptReleaseContext(hProv, 0);
return dwStatus;
}
if (!CryptHashData(hHash, (BYTE*)key_str, key_len, 0)) {
DWORD err = GetLastError();
printf ("CryptHashData Failed : %#x\n", err);
return dwStatus;
}
HCRYPTKEY hKey;
if (!CryptDeriveKey(hProv, CALG_AES_128, hHash, 0,&hKey)){
dwStatus = GetLastError();
printf("CryptDeriveKey failed: %x\n", dwStatus);
CryptReleaseContext(hProv, 0);
return dwStatus;
}
const size_t chunk_size = BLOCK_LEN;
DWORD read = 0;
DWORD written = 0;
DWORD ciphertextLen = BLOCK_LEN;
memcpy(outbuf, inbuf, chunk_size);
if (CryptDecrypt(hKey, NULL, FALSE, 0,outbuf, &ciphertextLen)) {
//printf ("[+] crypt OK!\n");
dwStatus = TRUE;
}
CryptReleaseContext(hProv, 0);
CryptDestroyKey(hKey);
CryptDestroyHash(hHash);
return dwStatus;
}
示例8: CWE325_Missing_Required_Cryptographic_Step__w32_CryptHashData_17_bad
void CWE325_Missing_Required_Cryptographic_Step__w32_CryptHashData_17_bad()
{
int j;
for(j = 0; j < 1; j++)
{
{
BYTE payload[100];
DWORD payloadLen = strlen(PAYLOAD);
HCRYPTPROV hCryptProv = (HCRYPTPROV)NULL;
HCRYPTHASH hHash = (HCRYPTHASH)NULL;
HCRYPTKEY hKey = (HCRYPTKEY)NULL;
char hashData[100] = HASH_INPUT;
do
{
/* Copy plaintext into payload buffer */
memcpy(payload, PAYLOAD, payloadLen);
/* Aquire a Context */
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0))
{
break;
}
/* Create hash handle */
if(!CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hHash))
{
break;
}
/* FLAW: Missing required step (CryptHashData) does not use hash input when generating AES key */
/* Derive an AES key from the hash */
if(!CryptDeriveKey(hCryptProv, CALG_AES_256, hHash, 0, &hKey))
{
break;
}
/* Encrypt the payload */
if(!CryptEncrypt(hKey, 0, 1, 0, payload, &payloadLen, sizeof(payload)))
{
break;
}
}
while (0);
if (hKey)
{
CryptDestroyKey(hKey);
}
if (hHash)
{
CryptDestroyHash(hHash);
}
if (hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
}
/* Do something with the encrypted data */
printBytesLine(payload, payloadLen);
}
}
}
开发者ID:gpwi970725,项目名称:testJuliet2,代码行数:56,代码来源:CWE325_Missing_Required_Cryptographic_Step__w32_CryptHashData_17.c
示例9: Password
void Password(char*parol,int len)
{
if(hProv!=NULL)CryptReleaseContext(hProv,0);
CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
CryptCreateHash(hProv,CALG_SHA,0,0,&hash);
CryptHashData(hash,parol,len,0);
CryptDeriveKey(hProv,CALG_RC4,hash,0,&key);
CryptDestroyHash(hash);
}
示例10: CWE325_Missing_Required_Cryptographic_Step__w32_CryptCreateHash_05_bad
void CWE325_Missing_Required_Cryptographic_Step__w32_CryptCreateHash_05_bad()
{
if(staticTrue)
{
{
BYTE payload[100];
DWORD payloadLen = strlen(PAYLOAD);
HCRYPTPROV hCryptProv = (HCRYPTPROV)NULL;
HCRYPTHASH hHash = (HCRYPTHASH)NULL;
HCRYPTKEY hKey = (HCRYPTKEY)NULL;
char hashData[100] = HASH_INPUT;
do
{
/* Copy plaintext into payload buffer */
memcpy(payload, PAYLOAD, payloadLen);
/* Aquire a Context */
if(!CryptAcquireContext(&hCryptProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, 0))
{
break;
}
/* FLAW: Missing required step (CryptCreateHash) causes the payload to remain in plaintext form */
/* Hash the input string */
if(!CryptHashData(hHash, (BYTE*)hashData, strlen(hashData), 0))
{
break;
}
/* Derive an AES key from the hash */
if(!CryptDeriveKey(hCryptProv, CALG_AES_256, hHash, 0, &hKey))
{
break;
}
/* Encrypt the payload */
if(!CryptEncrypt(hKey, 0, 1, 0, payload, &payloadLen, sizeof(payload)))
{
break;
}
}
while (0);
if (hKey)
{
CryptDestroyKey(hKey);
}
if (hHash)
{
CryptDestroyHash(hHash);
}
if (hCryptProv)
{
CryptReleaseContext(hCryptProv, 0);
}
/* Do something with the encrypted data */
printBytesLine(payload, payloadLen);
}
}
}
开发者ID:gpwi970725,项目名称:testJuliet2,代码行数:55,代码来源:CWE325_Missing_Required_Cryptographic_Step__w32_CryptCreateHash_05.c
示例11: DWORD
std::string CStringUtils::Decrypt(const std::string& s, const std::string& password)
{
std::string decryptstring;
HCRYPTPROV hProv = NULL;
// Get handle to user default provider.
if (CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
HCRYPTHASH hHash = NULL;
// Create hash object.
if (CryptCreateHash(hProv, CALG_SHA_512, 0, 0, &hHash))
{
// Hash password string.
DWORD dwLength = DWORD(sizeof(WCHAR)*password.size());
if (CryptHashData(hHash, (BYTE *)password.c_str(), dwLength, 0))
{
HCRYPTKEY hKey = NULL;
// Create block cipher session key based on hash of the password.
if (CryptDeriveKey(hProv, CALG_AES_256, hHash, CRYPT_EXPORTABLE, &hKey))
{
dwLength = DWORD(s.size() + 1024); // 1024 bytes should be enough for padding
std::unique_ptr<BYTE[]> buffer(new BYTE[dwLength]);
std::unique_ptr<BYTE[]> strIn(new BYTE[s.size() + 1]);
if (buffer && strIn)
{
if (CStringUtils::FromHexString(s, strIn.get()))
{
// copy encrypted password to temporary buffer
memcpy(buffer.get(), strIn.get(), s.size());
dwLength = DWORD(s.size() / 2);
CryptDecrypt(hKey, 0, true, 0, (BYTE *)buffer.get(), &dwLength);
decryptstring = std::string((char*)buffer.get(), dwLength);
if (!decryptstring.empty() && (decryptstring[0] == '*'))
{
decryptstring = decryptstring.substr(1);
}
else
decryptstring.clear();
}
}
CryptDestroyKey(hKey); // Release provider handle.
}
}
CryptDestroyHash(hHash); // Destroy session key.
}
CryptReleaseContext(hProv, 0);
}
else
DebugBreak();
return decryptstring;
}
示例12: exsltCryptoCryptoApiRc4Decrypt
static void
exsltCryptoCryptoApiRc4Decrypt (xmlXPathParserContextPtr ctxt,
const unsigned char *key,
const unsigned char *msg, int msglen,
unsigned char *dest, int destlen) {
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
DWORD dwDataLen;
unsigned char hash[HASH_DIGEST_LENGTH];
if (msglen > destlen) {
xsltTransformError (xsltXPathGetTransformContext (ctxt), NULL,
NULL,
"exslt:crypto : internal error exsltCryptoCryptoApiRc4Encrypt dest buffer too small.\n");
return;
}
if (!CryptAcquireContext (&hCryptProv, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
exsltCryptoCryptoApiReportError (ctxt, __LINE__);
return;
}
hHash = exsltCryptoCryptoApiCreateHash (ctxt, hCryptProv,
CALG_SHA1, key,
RC4_KEY_LENGTH, hash,
HASH_DIGEST_LENGTH);
if (!CryptDeriveKey
(hCryptProv, CALG_RC4, hHash, 0x00800000, &hKey)) {
exsltCryptoCryptoApiReportError (ctxt, __LINE__);
goto fail;
}
/* Now encrypt data. */
dwDataLen = msglen;
memcpy (dest, msg, msglen);
if (!CryptDecrypt (hKey, 0, TRUE, 0, dest, &dwDataLen)) {
exsltCryptoCryptoApiReportError (ctxt, __LINE__);
goto fail;
}
fail:
if (0 != hHash) {
CryptDestroyHash (hHash);
}
CryptDestroyKey (hKey);
CryptReleaseContext (hCryptProv, 0);
}
示例13: generateKey
HCRYPTKEY generateKey(HCRYPTPROV provider, ALG_ID algid, LPTSTR password) {
BOOL res;
DWORD data;
HCRYPTKEY key;
HCRYPTHASH hash;
if (!CryptCreateHash(provider, CALG_SHA, 0, 0, &hash))
return 0;
data = strlen(password);
if (!CryptHashData(hash, (BYTE *)password, data, 0)) {
CryptDestroyHash(hash);
return 0;
}
res = CryptDeriveKey(provider, algid, hash, CRYPT_EXPORTABLE, &key);
CryptDestroyHash(hash);
return (res ? key : 0);
}
示例14: GenKey
HCRYPTKEY GenKey(HCRYPTPROV hCrProv, QString keyText, QByteArray* key, DWORD& keyBlobLen, bool flag)
{
HCRYPTKEY hKey = 0;
HCRYPTHASH hHash = 0;
if (flag)
{
CryptGenKey(hCrProv, CALG_AES_256, CRYPT_EXPORTABLE, &hKey);
}
else
{
CryptCreateHash(hCrProv, CALG_MD5, 0, 0, &hHash);
CryptHashData(hHash, (BYTE *)keyText.toUtf8().constData(), keyText.length(), 0);
CryptDeriveKey(hCrProv, CALG_AES_256, hHash, 0, &hKey);
}
CryptExportKey(hKey, 0, PLAINTEXTKEYBLOB, 0, NULL, &keyBlobLen);
key->resize(keyBlobLen);
CryptExportKey(hKey, 0, PLAINTEXTKEYBLOB, 0, (BYTE*)key->data(), &keyBlobLen);
return hKey;
}
示例15: derive_cryptkey_ptr
inline cryptkey_ptr_t derive_cryptkey_ptr(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData, DWORD dwFlags)
{
HCRYPTKEY tmp;
if( !CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &tmp ) )
{
DWORD const errc = GetLastError();
STCRYPT_THROW_EXCEPTION( exception::cryptoapi_error() << exception::cryptoapi_einfo(errc) );
}
std::auto_ptr<HCRYPTKEY> HCRYPTKEY_mem;
try {
HCRYPTKEY_mem.reset(new HCRYPTKEY(tmp));
}catch(...){
BOOL const r = CryptDestroyKey(tmp); assert(r);
throw;
}
return cryptkey_ptr_t ( HCRYPTKEY_mem.release(), delete_HCRYPTKEY );
}