Crypto API study Note 3

Source: Internet
Author: User

Encoding and decoding data
The following is the topic. Now we will talk about how to encoding and decoding data. It still starts from a program.
# Include <stdio. h>
# Include <windows. h>
# Include <wincrypt. h>
# Define my_encoding_type (pkcs_7_asn_encoding | x509_asn_encoding)
Void myhandleerror (char * s );

Void main (void)
{
Hcryptmsg hmsg; pointing to a message handle
Byte * pbcontent; a byte Pointer Points to the message
DWORD cbcontent; message length
DWORD cbencodedblob; size of the ecode blob
Byte * pbencodedblob; a byte Pointer Points to encode blob

DWORD cbdata = sizeof (DWORD); data size
DWORD cbdecoded; decode content size
Byte * pbdecoded; pointer to decode

Pbcontent = (byte *) "security is our only business ";
Cbcontent = strlen (char *) pbcontent) + 1;

Printf ("the original message => % s/n", pbcontent );

If (cbencodedblob = cryptmsgcalculateencodedlength (
My_encoding_type: Specifies the encode type, which has been predefined at the beginning of the program. my_encoding_type is pkcs_7_asn_encoding | x509_asn_encoding
0, // flags
Cmsg_data, which defines the data type. Here, it is specified as a byte string.
Null,
Null,
Cbcontent) content size
The function is used to calculate the maximum length required for the specified message encode. By calculation, the memory space is allocated for a blob.
{
Printf ("the length of the data has been calculated./N ");
}
Else
{
Myhandleerror ("getting cbencodedblob length failed ");
}
Allocate memory space for encode blob

If (pbencodedblob = (byte *) malloc (cbencodedblob ))
{
Printf ("memory has been allocated for the signed message./N ");
}
Else
{
Myhandleerror ("memory allocation failed ");
}

If (hmsg = cryptmsgopentoencode (cryptmsgopentoencode is encode, open a message
My_encoding_type, encode type, file description
0, // flags
Cmsg_data: Specifies the message type. cmsg_data indicates that the type is useless.
Null, not found now, null
Null, same as above
Null) is not stream encryption. this parameter is null.
{
Printf ("the message to be encoded has been opened./N ");
}
Else
{
Myhandleerror ("opentoencode failed ");
}
If (cryptmsgupdate adds data to the message, you can add the data segment to the message through a loop.
Hmsg, a careful handle
Pbcontent, pointer to data
Cbcontent, data size
True) True indicates that this is the last piece of data. When a message is opened, if cmsg_detached_flag is used, this is set to false; otherwise, this is true.
{
Printf ("content has been added to the encoded message./N ");
}
Else
{
Myhandleerror ("msgupdate failed ");
}

If (cryptmsggetparam is used to obtain parameters in a message
Hmsg, a message handle
Cmsg_bare_content_param: specifies the type of the parameter to be retrieved.
0,
Pbencodedblob, a memory address for receiving data
& Cbencodedblob) The Blob size, that is, the size of the data received above.
{
Printf ("message encoded successfully./N ");
}
Else
{
Myhandleerror ("msggetparam failed ");
}
Release message handle
If (hmsg)
Cryptmsgclose (hmsg );

If (hmsg = cryptmsgopentodecode
My_encoding_type,
0,
Cmsg_data,
Null,
Null,
Null ))
{
Printf ("the message to decode is open./N ");
}
Else
{
Myhandleerror ("opentodecode failed ");
}
The following process is similar to encode. The called function is the same as above, but the process is reversed.

Printf ("/nthe length of the encoded message is % d./n ",
Cbencodedblob );

If (cryptmsgupdate (
Hmsg, // handle to the message
Pbencodedblob, // pointer to the encoded blob
Cbencodedblob, // size of the encoded blob
True) // last call
{
Printf ("the encoded blob has been added to the message./N ");
}
Else
{
Myhandleerror ("decode msgupdate failed ");
}
If (cryptmsggetparam (the call to cryptmsggetparam is different from the preceding one. The call is called twice. The first call is mainly to get the message size and the second call is to get the memory address of the message.
Hmsg, message handle
Cmsg_content_param, // parameter type
0,
Null, // address for returned
// Information
& Cbdecoded) // size of the returned
// Information
{
Printf ("the decoded message size is % d./N", cbdecoded );
}
Else
{
Myhandleerror ("decode cmsg_content_param failed ");
}
If (pbdecoded = (byte *) malloc (cbdecoded ))
{
Printf ("memory has been allocated for the decoded message./N ");
}
Else
{
Myhandleerror ("decoding memory allocation failed .");
}
If (cryptmsggetparam (
Hmsg, // handle to the message
Cmsg_content_param, // parameter type
0, // Index
Pbdecoded, // address for returned
// Information
& Cbdecoded) // size of the returned
// Information
{
Printf ("The message is % S./N", (lpstr) pbdecoded );
}
Else
{
Myhandleerror ("decode cmsg_content_param #2 failed ");
}
If (pbencodedblob)
Free (pbencodedblob );
If (pbdecoded)
Free (pbdecoded );
If (hmsg)
Cryptmsgclose (hmsg );

Printf ("this program ran to completion without error./N ");

} // End of main

Next let's take a look at how to hash a conversation key, which can be used to encrypt a message and a file. We still start with a program.
# Include <stdio. h>
# Include <windows. h>
# Include <wincrypt. h>
# Define my_encoding_type (pkcs_7_asn_encoding | x509_asn_encoding)
Void myhandleerror (char * s );

Void main ()
{

//--------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Declare variables.

Hcryptprov;
Hcrypthash hhash;
Hcryptkey hkey;

//--------------------------------------------------------------------
// Begin processing.

Printf ("process beginning. Creating a session key./N ");

If (cryptacquirecontext (first, get a default CSP handle
& Hcryptprov,
Null,
Null,
Prov_rsa_full,
0 ))
{
Printf ("cryptacquirecontext complete./N ");
}
Else
{
Myhandleerror ("acquisition of context failed .");
}

If (cryptcreatehash (create a hash object of the calg_md5 algorithm. This hash object uses the MD5 algorithm.
Hcryptprov, specifying a CSP handle
Calg_md5, specify the algorithm
0,
0,
& Hhash ))
{
Printf ("an empty hash object has been created./N ");
}
Else
{
Myhandleerror ("error during cryptbeginhash! /N ");
}

If (cryptgenkey (create key
Hcryptprov, passing in a CSP handle
Calg_rc2, indicating the algorithm used for key authentication
Crypt_exportable, indicating that the key can be exported to the CSP, used outside the application
& Hkey ))
{
Printf ("a random session key has been created./N ");
}
Else
{
Myhandleerror ("error during cryptgenkey! /N ");
}
If (crypthashsessionkey (hash the generated key)
Hhash,
Hkey,
0 ))
{
Printf ("the session key has been hashed./N ");
}
Else
{
Myhandleerror ("error during crypthashsessionkey! /N ");
}
You can add code and encrypt it with the generated key.

If (hhash)
{
If (! (Cryptdestroyhash (hhash )))
Myhandleerror ("error during cryptdestroyhash ");
}

If (hkey)
{
If (! (Cryptdestroykey (hkey )))
Myhandleerror ("error during cryptdestroykey ");
}

// Release the CSP.

If (hcryptprov)
{
If (! (Cryptreleasecontext (hcryptprov, 0 )))
Myhandleerror ("error during cryptreleasecontext ");
}

Printf ("create random session key completed without error./N ");
} // End main
The appendix is the doc document. I hope you will give more valuable comments.

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.