当前位置: 首页>>代码示例>>C++>>正文


C++ CryptAcquireContext函数代码示例

本文整理汇总了C++中CryptAcquireContext函数的典型用法代码示例。如果您正苦于以下问题:C++ CryptAcquireContext函数的具体用法?C++ CryptAcquireContext怎么用?C++ CryptAcquireContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了CryptAcquireContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: entropy_fun

int entropy_fun(unsigned char buf[], unsigned int len)
{
    HCRYPTPROV provider;
    unsigned __int64 pentium_tsc[1];
    unsigned int i;
    int result = 0;


    if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
    {
        result = CryptGenRandom(provider, len, buf);
        CryptReleaseContext(provider, 0);
        if (result)
            return len;
    }

    QueryPerformanceCounter((LARGE_INTEGER *)pentium_tsc);

    for(i = 0; i < 8 && i < len; ++i)
        buf[i] = ((unsigned char*)pentium_tsc)[i];

    return i;
}
开发者ID:manuelkasper,项目名称:objective-zip,代码行数:23,代码来源:entropy.c

示例2: arc4_seed_win32

static int
arc4_seed_win32(void)
{
	/* This is adapted from Tor's crypto_seed_rng() */
	static int provider_set = 0;
	static HCRYPTPROV provider;
	unsigned char buf[ADD_ENTROPY];

	if (!provider_set) {
		if (!CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
		    CRYPT_VERIFYCONTEXT)) {
			if (GetLastError() != (DWORD)NTE_BAD_KEYSET)
				return -1;
		}
		provider_set = 1;
	}
	if (!CryptGenRandom(provider, sizeof(buf), buf))
		return -1;
	arc4_addrandom(buf, sizeof(buf));
	memset(buf, 0, sizeof(buf));
	arc4_seeded_ok = 1;
	return 0;
}
开发者ID:OpenSXCE-org,项目名称:FireFox-43-port-for-all-OpenSolaris-distros,代码行数:23,代码来源:arc4random.c

示例3: getrandom_chars

unsigned int
getrandom_chars(int desired, unsigned char *buf, int lenbuf) {
	HCRYPTPROV hcryptprov;
	BOOL err;

	if (buf == NULL || lenbuf <= 0 || desired > lenbuf)
		return (0);
	/*
	 * The first time we just try to acquire the context
	 */
	err = CryptAcquireContext(&hcryptprov, NULL, NULL, PROV_RSA_FULL,
				  CRYPT_VERIFYCONTEXT);
	if (!err){
		return (0);
	}
	if (!CryptGenRandom(hcryptprov, desired, buf)) {
		CryptReleaseContext(hcryptprov, 0);
		return (0);
	}

	CryptReleaseContext(hcryptprov, 0);
	return (desired);
}
开发者ID:Hal-Murray,项目名称:ntp,代码行数:23,代码来源:randfile.c

示例4: CreateKeyset

BOOL CreateKeyset(HCRYPTPROV *hProv) {
	HCRYPTKEY hXchgKey;
	LONG error;
	if(!CryptAcquireContext(hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET))
		return FALSE;

    if(!CryptGenKey(*hProv,AT_SIGNATURE,0,&hXchgKey))  {
		error=GetLastError();
		CryptReleaseContext(*hProv, 0);
		SetLastError(error);
		return FALSE;
	}
    CryptDestroyKey(hXchgKey);
    
	if(!CryptGenKey(*hProv,AT_KEYEXCHANGE,CRYPT_EXPORTABLE,&hXchgKey)) {
		error=GetLastError();
		CryptReleaseContext(*hProv, 0);
		SetLastError(error);
		return FALSE;
	}
	CryptDestroyKey(hXchgKey);
	return TRUE;
}
开发者ID:carsten-clauss,项目名称:MP-MPICH,代码行数:23,代码来源:Encryption.cpp

示例5: exsltCryptoCryptoApiHash

/**
 * exsltCryptoCryptoApiHash:
 * @ctxt: an XPath parser context
 * @algorithm: hashing algorithm to use
 * @msg: text to be hashed
 * @msglen: length of text to be hashed
 * @dest: buffer to place hash result
 *
 * Helper function which hashes a message using MD4, MD5, or SHA1.
 * Uses Win32 CryptoAPI.
 */
static void
exsltCryptoCryptoApiHash (xmlXPathParserContextPtr ctxt,
			  ALG_ID algorithm, const char *msg,
			  unsigned long msglen,
			  char dest[HASH_DIGEST_LENGTH]) {
    HCRYPTPROV hCryptProv;
    HCRYPTHASH hHash;

    if (!CryptAcquireContext (&hCryptProv, NULL, NULL, PROV_RSA_FULL,
			      CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
	exsltCryptoCryptoApiReportError (ctxt, __LINE__);
	return;
    }

    hHash = exsltCryptoCryptoApiCreateHash (ctxt, hCryptProv,
					    algorithm, msg, msglen,
					    dest, HASH_DIGEST_LENGTH);
    if (0 != hHash) {
	CryptDestroyHash (hHash);
    }

    CryptReleaseContext (hCryptProv, 0);
}
开发者ID:ArielleBassanelli,项目名称:gempak,代码行数:34,代码来源:crypto.c

示例6: read_RSA_key

/**
 * Loads an RSA private key from the specified key container
 */
RSA_key_t read_RSA_key(const char *container)
{
    int idx, found;
    const char *_container;

    // First find available private key slot
    for (idx = 0, found = 0; (idx < MAXLIST) && (!found); idx++) {
        if (private_key_list[idx].provider == 0) {
            found = 1;
        }
    }
    if (!found) {
        log(0, 0, "Couldn't find empty key slot for private key");
        return (RSA_key_t)NULL;
    }
    idx--;

    if (!strcmp(container, "")) {
        _container = DEF_KEY_CONTAINER;
    } else {
        _container = container;
    }

    if (!CryptAcquireContext(&private_key_list[idx].provider, _container,
                             NULL, prov_type, machine_keyset)) {
        mserror("CryptAcquireContext failed");
        return (RSA_key_t)NULL;
    }

    if (!CryptGetUserKey(private_key_list[idx].provider, AT_KEYEXCHANGE,
                         &private_key_list[idx].key)) {
        mserror("CryptGetUserKey failed");
        return (RSA_key_t)NULL;
    }

    return (RSA_key_t)private_key_list[idx].key;
}
开发者ID:b-cuts,项目名称:uftp,代码行数:40,代码来源:encrypt_cryptoapi.c

示例7: init_random

static void init_random()
{    
#ifdef WIN32
    HCRYPTPROV wctx;
#else
    FILE   *fp   = 0;
#endif
    
    unsigned char buff[64];

    if (g_initialized)
        return;
    
#ifdef WIN32

        CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
        
        CryptGenRandom(wctx, sizeof(buff), (BYTE*) buff);
        
        CryptReleaseContext(wctx, 0);

        g_initialized = 1;
        
#else
        fp = fopen("/dev/urandom", "r");
        
        if (fp)
        {
            size_t read = fread(buff, sizeof(buff), 1, fp);
            g_initialized = read == 1;
            fclose(fp);
        }
#endif

    if (g_initialized)
       RAND_seed( buff, sizeof(buff) );
}
开发者ID:cocagne,项目名称:csrp,代码行数:37,代码来源:srp.c

示例8: randombytes

void randombytes(void *ptr, size_t length)
{

    char failed = 0;

    #ifdef WIN32

    static HCRYPTPROV prov = 0;
    if (prov == 0) {
        if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
            failed = 1;
        }
    }
    if (!failed && !CryptGenRandom(prov, length, ptr)) {
        failed = 1;
    }

    #else

    int fh;
    if ((fh = open("/dev/urandom", O_RDONLY)) >= 0 || (fh = open("/dev/random", O_RDONLY)) >= 0) {
        const ssize_t ret = read(fh, ptr, length);
        if (ret < 0 || (size_t) ret != length) {
            failed = 1;
        }
        close(fh);
    } else {
        failed = 1;
    }

    #endif

    if (failed) {
        ErrorExit("%s: ERROR: randombytes failed for all possible methods for accessing random data", __local_name);
    }
}
开发者ID:ariosx,项目名称:ossec-hids,代码行数:36,代码来源:randombytes.c

示例9: memset

bool CEncryptSyncData::InitEncryptHandle(const char * pchrPassWord)
{
	memset(m_chKeyPassWord,0,sizeof(m_chKeyPassWord));
	memcpy(m_chKeyPassWord,pchrPassWord,strlen(pchrPassWord));
	if(!CryptAcquireContext(&hCryptProv,NULL,NULL,PROV_RSA_FULL,0))
	{
		return false;
	}
	if(!CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash))
	{
		return false;
	}
	if(!CryptHashData(hHash,(BYTE *)m_chKeyPassWord,strlen(m_chKeyPassWord),0)) 
	{
		return false;
	}
	if(!CryptDeriveKey(hCryptProv,ENCRYPT_ALGORITHM, hHash, KEYLENGTH,&hKey))
	{ 
		return false;
	}
 	CryptDestroyHash(hHash); 
	hHash = 0; 
	return true;
}
开发者ID:guangminghan,项目名称:CyclonicGameEngine,代码行数:24,代码来源:EncryptSyncData.cpp

示例10: mbedtls_platform_entropy_poll

int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
                           size_t *olen )
{
    HCRYPTPROV provider;
    ((void) data);
    *olen = 0;

    if( CryptAcquireContext( &provider, NULL, NULL,
                              PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
    {
        return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
    }

    if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
    {
        CryptReleaseContext( provider, 0 );
        return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
    }

    CryptReleaseContext( provider, 0 );
    *olen = len;

    return( 0 );
}
开发者ID:kiibohd,项目名称:controller,代码行数:24,代码来源:entropy_poll.c

示例11: cryptographicallyRandomValuesFromOS

void cryptographicallyRandomValuesFromOS(unsigned char* buffer, size_t length)
{
#if OS(UNIX)
    int fd = open("/dev/urandom", O_RDONLY, 0);
    if (fd < 0)
        CRASH(); // We need /dev/urandom for this API to work...

    if (read(fd, buffer, length) != static_cast<ssize_t>(length))
        CRASH();

    close(fd);
#elif OS(WINDOWS)
    HCRYPTPROV hCryptProv = 0;
    if (!CryptAcquireContext(&hCryptProv, 0, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
        CRASH();
    if (!CryptGenRandom(hCryptProv, length, buffer))
        CRASH();
    CryptReleaseContext(hCryptProv, 0);
#else
    #error "This configuration doesn't have a strong source of randomness."
    // WARNING: When adding new sources of OS randomness, the randomness must
    //          be of cryptographic quality!
#endif
}
开发者ID:CannedFish,项目名称:deepin-webkit,代码行数:24,代码来源:OSRandomSource.cpp

示例12: GenRandomBytes

void GenRandomBytes(void* dest, size_t size)
{
#ifdef _WIN32
	HCRYPTPROV prov;
	if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
		Sys::Error("CryptAcquireContext failed: %s", Win32StrError(GetLastError()));

	if (!CryptGenRandom(prov, size, (BYTE*)dest))
		Sys::Error("CryptGenRandom failed: %s", Win32StrError(GetLastError()));

	CryptReleaseContext(prov, 0);
#elif defined(__native_client__)
	size_t bytes_written;
	if (nacl_secure_random(dest, size, &bytes_written) != 0 || bytes_written != size)
		Sys::Error("nacl_secure_random failed");
#else
	int fd = open("/dev/urandom", O_RDONLY);
	if (fd == -1)
		Sys::Error("Failed to open /dev/urandom: %s", strerror(errno));
	if (read(fd, dest, size) != (ssize_t) size)
		Sys::Error("Failed to read from /dev/urandom: %s", strerror(errno));
	close(fd);
#endif
}
开发者ID:norfenstein,项目名称:unvqx,代码行数:24,代码来源:System.cpp

示例13: SetFromPassword

/**
 *
 *  Generate System key from pass phrase -> level 2
 *  Derives 128-bit value from MD5
 *
 */
BOOL SystemKey::SetFromPassword (std::wstring pwd) {
  HCRYPTPROV hProv;
  HCRYPTHASH hHash;

  if (CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL,
      CRYPT_VERIFYCONTEXT)) {
    if (CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash)) {
      if (CryptHashData (hHash, (PBYTE)pwd.c_str(),
          pwd.length() * sizeof(wchar_t), 0)) {

        DWORD dwHashLen = SYSTEM_KEY_LEN;
        CryptGetHashParam (hHash, HP_HASHVAL, key, &dwHashLen, 0);
        dwError = GetLastError();
      }
      CryptDestroyHash (hHash);
    } else {
      dwError = GetLastError ();
    }
    CryptReleaseContext (hProv, 0);
  } else {
    dwError = GetLastError ();
  }
  return dwError == ERROR_SUCCESS;
}
开发者ID:infosecsmith,项目名称:ntds_decode,代码行数:30,代码来源:systemkey.cpp

示例14: getHbootKeyFromBootKeyAndF

bool mod_hash::getHbootKeyFromBootKeyAndF(BYTE hBootKey[0x10], BYTE bootKey[0x10], BYTE * AccountsF)
{
	bool reussite = false;
	unsigned char qwe[] = "[email protected]#$%^&*()qwertyUIOPAzxcvbnmQQQQQQQQQQQQ)(*@&%";
	unsigned char num[] = "0123456789012345678901234567890123456789";

	HCRYPTPROV hCryptProv = NULL;
	HCRYPTHASH hHash = NULL;
	if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
	{
		BYTE md5hash[0x10] = {0};
		DWORD dwHashDataLen = sizeof(md5hash);
		CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash);
		CryptHashData(hHash, AccountsF + 0x70, 0x10, 0);
		CryptHashData(hHash, qwe, sizeof(qwe), 0);
		CryptHashData(hHash, bootKey, 0x10, 0);
		CryptHashData(hHash, num, sizeof(num), 0);
		CryptGetHashParam(hHash, HP_HASHVAL, md5hash, &dwHashDataLen, 0);
		CryptDestroyHash(hHash);
		CryptReleaseContext(hCryptProv, 0);
		reussite = mod_crypto::genericDecrypt(AccountsF + 0x80, 0x10, md5hash, 0x10, CALG_RC4, hBootKey, 0x10);
	}
	return reussite;
}
开发者ID:DarkGreising,项目名称:mimikatz-en,代码行数:24,代码来源:mod_hash.cpp

示例15: getentropy

/*
 * On Windows, CryptGenRandom is supposed to be a well-seeded
 * cryptographically strong random number generator.
 */
int
getentropy(void *buf, size_t len)
{
	HCRYPTPROV provider;

	if (len > 256) {
		errno = EIO;
		return -1;
	}

	if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
	    CRYPT_VERIFYCONTEXT) != 0)
		goto fail;
	if (CryptGenRandom(provider, len, buf) != 0) {
		CryptReleaseContext(provider, 0);
		goto fail;
	}
	CryptReleaseContext(provider, 0);
	return (0);

fail:
	errno = EIO;
	return (-1);
}
开发者ID:SylvestreG,项目名称:bitrig,代码行数:28,代码来源:getentropy_win.c


注:本文中的CryptAcquireContext函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。