The Cryptography API, or How to Keep A Secret (3)

zhaozj2021-02-17  48

Several encryption API functions

[Editor Note: The following indented part of the file is referenced from MSDN Library, Platform, SDK, and DDK documents. ]

Initialization CSP: CryptacquiRecontext, CryptreleaseContext

The function CRYPTACQUIRECONTEXT is used to obtain a handle of a specific secret container in the CSP. The returned handle then you can call the selected CSP.

The function CRYPTRELEASECONTEXT is used to release the handle returned by the function CRYPTACQUIRECONText. CryptreleaseContext does not delete any Cryptography API object, which only releases the handle of the object.

The function CRYPTACQUIRECONTEXT performs two operations. First try to find the CSP specified in the variable, if found, the function tries to find the secret container that match the specified demon container name in the CSP. This function can also be used to establish, delete the secret container, depending on the parameter value in the function.

The code to obtain default CSP is as follows: (Translation: The original article code is in English, translated as Chinese, here is the same)

#include // Definition for CryptoAPI

/ *

For non-C / C users, the constants used here are as follows:

#define ms_def_prov "Microsoft Base Cryptographic Provider V1.0"

#define prov_rsa_full 1

* /

Bool Bresult;

Hcryptproc HPROV;

// Try to get the handle of the transition to the secret container

BRESULT = CRYPTACQUIRECONTEXT

& hprov, / / ​​Save the variable of the return handle

NULL, / / ​​Default

MS_DEF_PROV, // Default CSP

PROV_RSA_FULL, // The CSP type to be obtained

0); // Not specified action

.

.

.

/ / Perform operation here

.

.

.

/ / Release the container handle

CryptreleaseContext (HPROV);

If the CryptAcquiReconText call is successful, the return value is non-zero, the variable hprov is the demon container handle to be obtained.

To add or create a secret container in the default CSP, the code to write is as follows:

#include // Definition for CryptoAPI

/ *

For non-C / C users, the constants used here are as follows:

#define ms_def_prov "Microsoft Base Cryptographic Provider V1.0"

#define prov_rsa_full 1

#define crypt_newkeyset 0x8

* /

Bool Bresult;

Hcryptproc HPROV;

// Try to add a new secret container

BRESULT = CRYPTACQUIRECONTEXT

& hprov, / / ​​Save the variable of the return handle

NULL, / / ​​Default

MS_DEF_PROV, // Default CSP

PROV_RSA_FULL, // The CSP type to be obtained

Crypt_newkeyset; // Create a new secret container

.

.

.

/ / Perform operation here

.

.

.

/ / Release the container handle

CryptreleaseContext (HPROV); if the CryptAcquiRecontext call is successful, the return value is non-zero, the variable hprov is a new secret container handle.

To remove an existing secret container from the default CSP, the code to write is as follows:

#include // Definition for CryptoAPI

/ *

For non-C / C users, the constants used here are as follows:

#define ms_def_prov "Microsoft Base Cryptographic Provider V1.0"

#define prov_rsa_full 1

#define crypt_deleteKeySet 0x10

* /

Bool Bresult;

Hcryptproc HPROV;

// attempt to delete the secret container

BRESULT = CRYPTACQUIRECONTEXT

& hprov, / / ​​Save the variable of the return handle

NULL, / / ​​Default

MS_DEF_PROV, // Default CSP

PROV_RSA_FULL, // The CSP type to be obtained

Crypt_DeleteKeyset; // Delete the existing secret container

If the CryptAcquiRecontext call is successful, the return value is non-zero, the variable HPROV points to the secret container has been deleted, and this demon container is no longer valid.

Hash Data: CryptCreatehash, CrypthashData, Cryptgethashparam, CryptDestroyhash

When I said the "Hash" or "Hashing" or "hash", it refers to a method or algorithm that sends a value from a piece of data. This may be simple to add all data bits or complex to Fourier transform to data. (Translation: Hurry is also known as hash, mixture)

The four functions listed above are used to create or maintain a hash value generated from the supplied data, generally used:

Function CRYPTCREATEHSH is initialized when data is used. It returns the handle of the CSP hash object, which is used when the subsequent CryptHashData function hash data. The next step is to call the CRYPTGETHASHPARAM function to get the column value. Function CRYPTDESTROYHASH Release the handle returned by the function cryptcreatehash. CryptDestroyhash does not delete any encrypted API object, which only releases the handle of the hash object.

The CryptHashData function is used to calculate the password hash from the supplied data. This function can be called multiple times to calculate several parts of a large data block or data block. For example, we have to have data for DWBufferlen bytes in the buffer PBuffer. In this example I only use the CALG_MD5 hash algorithm to achieve this. The encryption API SDK document also provides a detailed description of many other algorithms. This example assumes that only one piece of data is used. Once the CryptgetHashParam function is called, the hash value is obtained, and this hash instance object cannot have other data.

#include // Definition for CryptoAPI

/ *

For non-C / C users, the constants used here are as follows:

#define alg_class_hash (4 << 13)

#define alg_type_any (0)

#define alg_sid_md5 3 # Define Calg_md5 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MD5)

#define hp_hashval 0x0002 // has a hash value

#define hp_hashsize 0x0004 // has a hash value

* /

Bool Bresult;

Hcrypthash hhash;

DWORD DWBUFFERSIZE;

DWORD DWVALUE;

PBYTE PBUFFER;

// Get a hash object handle

BRESULT = CRYPTCREATEHASH

HPROV, / / ​​CSP handle

CALG_MD5, // hash algorithm

0, // Non-dense hash

0, // set 0

& hhash); / / Save the variable of the hash object handle

// hash data

BRESULT = CRYPTHASHDATA (

Hhash, // has a sanctuary

PBuffer, // data buffer pointer

Dwbufferlen, // data length

0); // Undeflected value

// Get the hash value size

DwBuffersize = SizeOf (DWORD);

BRESULT = CRYPTGETHASHPARAM

Hhash, // has a sanctuary

HP_HASHSIZE, // Get a hash value size

& dwvalue, // Save Ratings Length Buffer

& dwbuffersize, // Buffer length

0); // must set 0

// Create a buffer that saves a quota value

PBuffer = new char [dwbuffersize];

// Get hash value.

BRESULT = CRYPTGETHASHPARAM

Hhash, // has a sanctuary

HP_HASHVAL, // Get a hash value

PBuffer, / / ​​Save the Ratings Length Buffer

& dwbuffersize, // Buffer length

0); // must set 0

// Release the hash object

CryptDestroyhash (Hhash);

The above example generates a hash value for the data pointed to by PBUFFER. If you have to have other data, use this data to call CRYPTHASHDATA, and the resulting hash value will still be the original value. Warned - Call CRYPTGETHASHPARAM with HP_HashValue parameters will prevent the use of this object to continue the hash.

转载请注明原文地址:https://www.9cbs.com/read-30248.html

New Post(0)