The cryptography API, or how to keep a secret (3)

Source: Internet
Author: User

Several encryption API functions
[Editor's note: The following indented files are referenced in the msdn library, platform, SDK, and DDK documents.]
Initialize CSP: cryptacquirecontext, cryptreleasecontext
The cryptacquirecontext function is used to obtain the handle of a specific secret container in CSP. Then you can call the selected CSP.
The cryptreleasecontext function is used to release the handle returned by the cryptacquirecontext function. Cryptreleasecontext does not delete any cryptography API object. It only releases the handle of the object.
The cryptacquirecontext function performs two operations. First, try to find the CSP specified in the Variable. If it is found, the function tries to find the secret container that matches the specified secret container name in the CSP. This function can also be used to create or delete a secret container, depending on the parameter value in the function.
The code for obtaining the default secret container in CSP is as follows)
# Include <wincrypt. h> // CryptoAPI Definition
/*
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 for the converted secret container
Bresult = cryptacquirecontext (
& Hprov, // Save the variable of the returned handle
Null, // default secret container
Ms_def_prov, // default CSP
Prov_rsa_full, // type of CSP to be obtained
0); // action not specified
.
.
.
// Perform the operation here
.
.
.
// Release the Container Handle
Cryptreleasecontext (hprov );
If cryptacquirecontext is called successfully, the returned value is non-zero. The variable hprov is the secret Container Handle to be obtained.
To add or create a secret container in the default CSP, write the following code:
# Include <wincrypt. h> // CryptoAPI Definition
/*
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 returned handle
Null, // default secret container
Ms_def_prov, // default CSP
Prov_rsa_full, // type of CSP to be obtained
Crypt_newkeyset); // create a new secret container
.
.
.
// Perform the operation here
.
.
.
// Release the Container Handle
Cryptreleasecontext (hprov );
If cryptacquirecontext is called successfully, the returned value is non-zero. The variable hprov is the new secret Container Handle.
To delete an existing secret container from the default CSP, write the following code:
# Include <wincrypt. h> // CryptoAPI Definition
/*
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;
// Try to delete the secret container
Bresult = cryptacquirecontext (
& Hprov, // Save the variable of the returned handle
Null, // default secret container
Ms_def_prov, // default CSP
Prov_rsa_full, // type of CSP to be obtained
Crypt_deletekeyset); // deletes an existing secret container
If cryptacquirecontext is called successfully, the returned value is non-zero. The secret container indicated by the hprov variable has been deleted, and this secret container is no longer valid.
Hash data: cryptcreatehash, crypthashdata, cryptgethashparam, cryptdestroyhash
When I say "hash" or "hashing" or "hash", it refers to a method or algorithm that generates a value from a piece of data. This may be simply adding all the data bits, or being complicated to performing Fourier transformation on the data. Hashes are also called hash and hashes)
The four functions listed above are used to create or maintain hash values generated from the provided data. They are generally used together:
The cryptcreatehash function is used to initialize hashed data. It returns the handle of the CSP hashed object, which will be used later when the crypthashdata function hashes data.
The next step is to call the cryptgethashparam function to obtain the hash value.
The cryptdestroyhash function releases the handle returned by the cryptcreatehash function. Cryptdestroyhash does not delete any encryption API object. It only releases the handle of the hash object.
The crypthashdata function is used to calculate the password hash from the provided data. This function can be called multiple times to compute a big data block or several parts of a data block. For example, we need to hash the data with long dwbufferlen bytes in the buffer pbuffer. In this example, I only use the calg_md5 hash algorithm for this purpose. The encryption api sdk documentation also provides detailed descriptions of many other algorithms. In this example, only one piece of data is hashed. Once the cryptgethashparam function is called to obtain the hash value, the hash instance object can no longer hash other data.
# Include <wincrypt. h> // CryptoAPI Definition
/*
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 // Hash Value
# Define hp_hashsize 0x0004 // hash value Length
*/
Bool bresult;
Hcrypthash hhash;
DWORD dwbuffersize;
DWORD dwvalue;
Pbyte pbuffer;
// Obtain the hashed object handle
Bresult = cryptcreatehash (
Hprov, // The CSP handle obtained earlier
Calg_md5, // Hash Algorithm
0, // non-secret hash
0, // set 0
& Hhash); // Save the variable of the hashed object handle
// Hash data
Bresult = crypthashdata (
Hhash, // hash object handle
Pbuffer, // data buffer pointer
Dwbufferlen, // Data Length
0); // unspecified Value
// Obtain the size of the hash value
Dwbuffersize = sizeof (DWORD );
Bresult = cryptgethashparam (
Hhash, // hash object handle
Hp_hashsize, // obtain the hash value size
& Dwvalue, // Save the hash value Length Buffer
& Dwbuffersize, // buffer Length
0); // must be set to 0
// Create a buffer for storing hash values
Pbuffer = new char [dwbuffersize];
// Get hash value.
Bresult = cryptgethashparam (
Hhash, // hash object handle
Hp_hashval, // obtain the hash value
Pbuffer, // Save the length of the hash value Buffer
& Dwbuffersize, // buffer Length
0); // must be set to 0
// Release the hash object
Cryptdestroyhash (hhash );
The above example generates a hash value for the data pointed to by pbuffer. If you want to hash other data, use this data to call crypthashdata. The generated hash value will still be the original value. Warning-calling cryptgethashparam with the hp_hashvalue parameter will prevent the use of this object to continue hashed columns.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.