Use winapi for hash Encryption

Source: Internet
Author: User

# Include <iostream> <br/> # include <vector> <br/> using namespace STD; <br/> # include <windows. h> <br/> # include <wincrypt. h> <br/> # include <stdlib. h> <br/> # include <share. h> <br/> # include <tchar. h> <br/> # include <Io. h> <br/> # define bolcksize 184320 <br/> DWORD computehash (const byte * pbdata, DWORD dwdatalen, alg_id algid, <br/> byte * & pbouthash, DWORD & dwhashlen, bool showdbinfo = false); <br/> int main () <BR/>{< br/> vector <byte *> hashvec; <br/> file * file = _ tfsopen (_ T ("protocol_4f_54.txt "), _ T ("rb"), _ sh_denyno); <br/> If (! File) <br/>{< br/> printf ("Open File fail! /N "); <br/> return-1; <br/>}< br/> int filesize = _ filelengthi64 (file-> _ file ); <br/> printf ("file size: % d/N", filesize); <br/> // set the buffer size <br/> // setvbuf (file, null, _ iofbf, 1024*8*2); </P> <p> byte * pbouthash; <br/> DWORD dwhashlen; <br/> // char pbdata [9728000] = "wangxijie/0"; <br/> char * pbdata = new char [bolcksize]; <br/> int dwdatalen = strlen (pbdata); <br/> int Togo = 0; <br/> size_t reallen; <br/> while (Togo <filesize) <B R/>{< br/> reallen = fread (pbdata, 1, bolcksize, file); <br/> computehash (byte *) & pbdata [0], reallen, calg_md5, pbouthash, dwhashlen); <br/> hashvec. push_back (pbouthash); <br/> for (INT I = 0; I <dwhashlen; I ++) <br/> printf ("% 02x ", pbouthash [I]); <br/> printf ("/n % d/N", reallen); <br/> Togo ++ = reallen; <br/>}</P> <p> byte * reshash = new byte [hashvec. size () * 16]; <br/> for (INT I = 0; I <pashvec. size (); I ++) <br/>{< br/> memcpy (reshas H + I * 16, hashvec [I], 16); <br/> Delete [] hashvec [I]; <br/> // For (Int J = 0; j <16; j ++) <br/> // printf ("% 02x", (reshash + I * 16) [J]); <br/> // printf ("/N"); <br/>}< br/> computehash (byte *) & reshash [0], hashvec. size () * 16, calg_md5, pbouthash, dwhashlen); <br/> for (I = 0; I <dwhashlen; I ++) <br/> printf ("% x", pbouthash [I]); <br/> printf ("/N"); <br/> Delete [] pbdata; <br/> fclose (File); <br/> Delete pbouthash; <br/> return 0; <br />}< Br/> DWORD computehash (const byte * pbdata, DWORD dwdatalen, alg_id algid, <br/> byte * & pbouthash, DWORD & dwhashlen, bool showdbinfo) <br/>{< br/> char username [10] = "wangxijie"; <br/> hcrypthash hhash = {0}; <br/> hcryptprov; <br/> If (cryptacquirecontext (<br/> & hcryptprov, // return CSP handle <br/> username, // password container name <br/> null, // use the default CSP name (Microsoft RSA base provider) when null <br/> prov_rsa_full, // CSP type <br/> 0) // Flag values <br/>{< br/> // The key container named username exists, then we have obtained the CSP handle <br/> If (showdbinfo) <br/>{< br/> printf ("a crypto context with the % s key container/N", username); <br/> printf ("has been acquired. /n "); <br/>}< br/> else // if the key container does not exist, we need to create this key container <br/>{< br/> If (cryptacquirecontext (<br/> & hcryptprov, <br/> username, <br/> null, <br/> prov_rsa_full, <br/> crypt_newkeyset) // create a key container named username <br/ >{< Br/> // The key container is successfully created and the CSP handle is obtained. <br/> If (showdbinfo) <br/> printf ("a new key container has been created. % s/n ", username); <br/>}< br/> else <br/>{< br/> If (showdbinfo) <br/> printf ("cocould not create a new key container. /n "); <br/>}< br/>}// end of else </P> <p> If (cryptcreatehash (<br/> hcryptprov, <br/> algid, <br/> 0, <br/> 0, <br/> & hhash) <br/>{< br/> If (showdbinfo) <br/> printf ("A hash object has been created ./N "); <br/>}< br/> else <br/>{< br/> If (showdbinfo) <br/> printf (" error during cryptcreatehash! /N "); <br/>}</P> <p> DWORD dwreturn; <br/> // char pbdata [100] =" wangxijie/0 "; <br/> // int dwdatalen = strlen (pbdata); <br/> If (! Crypthashdata (hhash, (byte *) & pbdata [0], dwdatalen, 0) <br/>{< br/> dwreturn = getlasterror (); <br/> cryptdestroyhash (hhash); <br/> cryptreleasecontext (hcryptprov, 0); <br/> return dwreturn; <br/>}</P> <p> // DWORD dwhashlen; <br/> DWORD dwlen = sizeof (dwhashlen); <br/> cryptgethashparam (hhash, hp_hashsize, (byte *) (& dwhashlen), & dwlen, 0); </P> <p> // byte * pbouthash; <br/> pbouthash = new byte [dwhashlen]; <br/> dwlen = dwhashlen; <br/> cryptgethashparam (hhash, hp_hashval, pbouthash, & dwlen, 0 ); </P> <p> // For (INT I = 0; I <dwhashlen; I ++) <br/> // printf ("% x ", pbouthash [I]); <br/> // printf ("/N"); </P> <p> // destory <br/> cryptdestroyhash (hhash ); <br/> If (hcryptprov) <br/> cryptreleasecontext (hcryptprov, 0); <br/> return 0; <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.