Vc ++ network security programming example (16)-open ssl hash decoding data

Source: Internet
Author: User
Tags md5 hash asymmetric encryption

OpenSSL provides eight symmetric encryption algorithms, 7 of which are group encryption algorithms, and the only stream encryption algorithm is RC4. These seven grouping encryption algorithms are AES, DES, Blowfish, CAST, IDEA, RC2, and RC5, all of which support the electronic cipher book mode (ECB) and the encrypted group link mode (CBC) the packet encryption mode is commonly used, including the encrypted feedback mode (CFB) and the output feedback mode (OFB. Among them, AES uses the encryption feedback mode (CFB) and output feedback mode (OFB). The group length is 128 bits, while other algorithms use 64 bits. In fact, the DES algorithm is not only a common DES algorithm, but also supports three keys and two key 3DES algorithms.

OpenSSL implements four asymmetric encryption algorithms, including DH algorithm, RSA algorithm, DSA algorithm, and elliptic curve algorithm (EC ). DH algorithms are generally used for User Key Exchange. The RSA algorithm can be used for both key exchange and digital signature. Of course, if you can tolerate its slow speed, it can also be used for data encryption. The DSA algorithm is generally used only for digital signatures.

We use VC ++ to implement data decoding using hash encoding. For more information, see code implementation and annotations.

 

# Ifndef _ WIN32_WINNT <br/> # define _ WIN32_WINNT 0x0400 <br/> # endif </p> <p> # include <stdio. h> <br/> # include <windows. h> <br/> # include <wincrypt. h> <br/> # define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING) </p> <p> // function declaration <br/> HCRYPTPROV GetCryptProv (); <br/> void HandleError (char * s); </p> <p> void main (void) <br/>{</p> <p> // variable declaration and initialization <br/> BYTE * pbContent = (BYTE *) "A razzle-dazzle hashed mesed E \ n "<br/>" Hashing is better than trashing. \ n "; // and encoded message <br/> DWORD cbContent = strlen (char *) pbContent) + 1; // message length <br/> HCRYPTPROV hCryptProv; // CSP handle <br/> DWORD HashAlgSize; // algorithm Data Structure size <br/> CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm; // algorithm Data Structure <br/> CMSG_HASHED_ENCODE_INFO HashedEncodeInfo; // hash Data Structure <br/> DWORD cbEncodedBlob; // encode the message length <br/> BYTE * pbEncodedBlob; // encode the message <br/> HCRYPTMSG hMsg; // message Handle <br/> HCRYPTMSG hDupMsg; <br/> DWORD cbData = sizeof (DWORD); <br/> DWORD dwMsgType; <br/> DWORD cbDecoded; <br/> BYTE * pbDecoded; </p> <p> printf ("Start processing. \ n "); <br/> printf (" the message to be hashed and encoded is: \ n "); <br/> printf (" % s \ n ", pbContent); // Display original message. <br/> printf ("the initial message length is % d \ n", cbContent ); </p> <p> // obtain the encryption provider handle <br/> hCryptProv = GetCryptProv (); </p> <p> //----------------------------------------------------- -------------- <Br/> // initialize the algorithm data structure. </p> <p> HashAlgSize = sizeof (HashAlgorithm); <br/> memset (& HashAlgorithm, 0, HashAlgSize); // The value is initialized to 0. <br/> HashAlgorithm. pszObjId = szOID_RSA_MD5; // specify the MD5 hash algorithm. </p> <p> // initialize <br/> // initialize the data structure of hash encoding. <br/> memset (& HashedEncodeInfo, 0, sizeof (CMSG_HASHED_ENCODE_INFO); <br/> HashedEncodeInfo. cbSize = sizeof (CMSG_HA SHED_ENCODE_INFO); <br/> HashedEncodeInfo. hCryptProv = hCryptProv; <br/> HashedEncodeInfo. hashAlgorithm = HashAlgorithm; <br/> HashedEncodeInfo. pvHashAuxInfo = NULL; </p> <p> // obtain the length of the encoded message <br/> if (cbEncodedBlob = CryptMsgCalculateEncodedLength (<br/> MY_ENCODING_TYPE, // encoding type <br/> 0, // flag <br/> CMSG_HASHED, // encode the Message Type <br/> & Hash EdEncodeInfo, // execution Message Type pointer <br/> NULL, <br/> cbContent )) // pre-encoding message length <br/>{< br/> printf ("the length to be allocated is % d bytes. \ n ", <br/> cbEncodedBlob ); <br/>}< br/> else <br/> {<br/> HandleError ("An error occurred while obtaining the encoded message length "); <br/>}< br/> // allocate <br/> // allocate space for the encoded message <br/> if (pbEncodedBlob = (BYTE *) malloc (cbEncodedBlob )) <br/> {<br/> printf ("% d bytes of space allocated. \ n ", <br/> cbEncodedBlob ); <Br/>}< br/> else <br/> {<br/> HandleError ("memory allocation error. "); <br/>}</p> <p> // unlock <br/> // open the pre-encoding message <br/> if (hMsg = CryptMsgOpenToEncode (<br/> MY_ENCODING_TYPE, // encoding type <br/> 0, // flag <br/> CMSG_HASHED, // encode the Message Type <br/> & HashedEncodeInfo, // execution Message Type pointer <br/> NULL, <br/> NULL) <br/>{< br/> printf ("the message to be encoded has been opened. \ n "); <br/>}< br/> else <br/> {<br/> HandleE Rror ("failed to open the pre-encoding message"); <br/>}</p> <p> // ------------------------------------------------------------- <br/> // encode the message, add the message handle <br/> if (CryptMsgUpdate (<br/> hMsg, // message handle <br/> pbContent, // pre-encoding message <br/> cbContent, // message length <br/> TRUE )) // whether it is the last piece of data <br/>{< br/> printf ("the encoded data has been added to the encoding message. \ n "); <br/>}< br/> else <br/> {<br/> HandleError (" An error occurred while coding the message. "); <br/>}</p> <p> //----------------------------------------------- -------------------- <Br/> // copy the message <br/> if (hDupMsg = CryptMsgDuplicate (hMsg) <br/>{< br/> printf ("the message has been copied. \ n "); <br/>}< br/> else <br/> {<br/> HandleError (" this message failed to be copied. "); <br/>}</p> <p> // ----------------------------------------------------------------- <br/> // obtain parameters from the copied message, here we get the encoded message content <br/> if (CryptMsgGetParam (<br/> hDupMsg, // message handle <br/> CMSG_CONTENT_PARAM, // parameter type <br/> 0, // No. <br/> pbEncodedBlob, // Data Pointer <br/> & cbEncodedBlob) // data size <br/> {<br/> printf ("The message is encoded successfully. \ n "); <br/>}< br/> else <br/> {<br/> HandleError (" An error occurred while obtaining the encoded message parameter "); <br/>}</p> <p> // ----------------------------------------------------------------- <br/> // close the message <br/> CryptMsgClose (hMsg ); <br/> CryptMsgClose (hDupMsg); </p> <p> // the following code decode the Hash Message. In general, this code should be in another application. <Br/> // obtain the encoded data and data size from the file or network </p> <p> // unlock <br/> // open the decoded message </ p> <p> if (hMsg = CryptMsgOpenToDecode (<br/> MY_ENCODING_TYPE, // encoding type <br/> 0, // flag <br/> 0, // Message Type <br/> hCryptProv, // CSP handle <br/> NULL, <br/> NULL) <br/> {<br/> printf ("the message to be decoded has been opened. \ n "); <br/>}< br/> else <br/> {<br/> HandleError (" An error occurred while enabling the pre-decoding message "); <br/>}</p> <p> //-------------------- ------------------------------------------- <Br/> // decode the message and add the decoded message to the Message handle. <br/> if (CryptMsgUpdate (<br/> hMsg, // message handle <br/> pbEncodedBlob, // encode the message pointer <br/> cbEncodedBlob, // encode the message length <br/> TRUE )) // whether it is the last piece of data <br/>{< br/> printf ("the decoded data is added to the decoded message. \ n "); <br/>}< br/> else <br/> {<br/> HandleError (" Message decoding failed "); <br/>}</p> <p> // ----------------------------------------------------------------- <br/> // obtain the data type <B R/> if (CryptMsgGetParam (<br/> hMsg, // message handle <br/> CMSG_TYPE_PARAM, // parameter type <br/> 0, // No. <br/> & dwMsgType, // Data Pointer <br/> & cbData )) // data size <br/>{< br/> printf ("The message type has been obtained. \ n "); <br/>}< br/> else <br/> {<br/> HandleError (" Decode CMSG_TYPE_PARAM failed "); <br/>}</p> <p> // determines whether the data type is a hash data structure. <br/> if (dwMsgType = CMSG_HASHED) <br/>{< br/> printf ("this message is a Hash Message. continue. \ n "); <br/>}< br/> else <br/> {<br/> HandleEr Ror ("Message Type error. "); <br/>}< br/> // obtain the decoded message length </p> <p> if (CryptMsgGetParam (<br/> hMsg, // message handle <br/> CMSG_CONTENT_PARAM, // parameter type <br/> 0, // serial number <br/> NULL, // Data Pointer <br/> & cbDecoded) // Data Length <br/> {<br/> printf ("message length % d obtained. \ n ", cbDecoded); <br/>}< br/> else <br/>{< br/> HandleError (" Decode CMSG_CONTENT_PARAM failed "); <br/>}< br/> // Allocate <br/> // allocate memory space </p> <p> if (pbDecoded = (BYTE *) malloc (cbDecoded )) <br/> {<br/> printf ("memory space has been allocated for decoded messages. \ n "); <br/>}< br/> else <br/> {<br/> HandleError (" memory allocation failed "); <br/>}< br/> // obtain the decoded message <br/> <p> if (CryptMsgGetParam (<br/> hMsg, // message handle <br/> CMSG_CONTENT_PARAM ,/ /Parameter type <br/> 0, // No. <br/> pbDecoded, // Data Pointer <br/> & cbDecoded )) // Data Length <br/>{< br/> printf ("message decoded successfully \ n "); <br/> printf ("the decoded message is \ n % s \ n", (LPSTR) pbDecoded ); <br/>}< br/> else <br/> {<br/> HandleError ("Decoding CMSG_CONTENT_PARAM #2 failed "); <br/>}< br/> // verify <br/> // verify the hash value </p> <p> if (CryptMsgControl (<br/> hMsg, // message handle <br/> 0, // flag <br/> CMSG_CTRL_VERIFY_HASH, // control type <br/> NULL) <br/>{< br/> printf ("hash value Verification Successful. \ n "); <br/> printf (" this data has not been tampered. \ n "); <br/>}< br/> else <br/>{< br/> printf (" hash value verification failed. some content in this message has changed. \ n "); <br/>}</p> <p> printf (" the program test is complete without error. \ n "); </p> <p> // release <br/> // release the memory <br/> if (pbEncodedBlob) <br/> free (pbEncodedBlob ); <br/> if (pbDecoded) <br/> free (pbDecoded); </p> <p> Crypt MsgClose (hMsg); </p> <p> // release the CSP handle </p> <p> if (hCryptProv) <br/> CryptReleaseContext (hCryptProv, 0 ); <br/>}// End of main </p> <p> // obtain the encryption provider handle <br/> HCRYPTPROV GetCryptProv () <br/>{< br/> HCRYPTPROV hCryptProv; // encryption service provider handle </p> <p> // obtain the encryption provider handle <br/> if (CryptAcquireContext (<br/> & hCryptProv, // encryption service provider handle <br/> NULL, // key container name. Here the login user name is used <br/> NULL, // encryption service provider, here, the default value is <br/> PROV_RSA_FULL, // type of the encryption service provider, which can provide encryption, signature, and other functions <br /> 0) // flag <br/> {<br/> printf ("the encrypted service provider handle is obtained successfully! \ N "); <br/>}< br/> else <br/> {<br/> // Delete the key set <br/> if (! CryptAcquireContext (& hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_DELETEKEYSET) <br/>{< br/> HandleError ("An error occurred while deleting the key set! "); <Br/>}</p> <p> // create a new key set <br/> if (! CryptAcquireContext (& hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET) <br/>{< br/> HandleError ("An error occurred while recreating a new key set! "); <Br/>}</p> <p >}< br/> return hCryptProv; <br/>}</p> <p> // HandleError: error handler, print error information, and exit the Program <br/> void HandleError (char * s) <br/>{< br/> printf ("an error occurred during program execution! \ N "); <br/> printf (" % s \ n ", s); <br/> printf (" error code: % x \ n. ", GetLastError (); <br/> printf (" the program is terminated! \ N "); <br/> exit (1); <br/>}< br/>

 

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.