Home > Back-end >  How to calculate the hash value of the file
How to calculate the hash value of the file

Time:11-29

Want to ask next everybody a great god, and how to calculate the hash value of the file in MFC programming,

CodePudding user response:

This blog http://blog.csdn.net/allh45601/article/details/13768587 used in sha1. H header file don't know where, so sha1_ctx sha1_begin, sha1_hash, sha1_end don't know how to do,

CodePudding user response:

Bo main,
Or baidu search keywords,

CodePudding user response:

Fyi:
 # pragma comment (lib, "crypt32. Lib") 
# pragma comment (lib, "advapi32. Lib")
# define _WIN32_WINNT 0 x0400
#include
#include
#include
# define KEYLENGTH 0 x00800000
Void HandleError (char * s);
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//These additional # define statements are required.
# define ENCRYPT_ALGORITHM CALG_RC4
# define ENCRYPT_BLOCK_SIZE 8
//Declare the function EncryptFile. The function definition
//follows the main.
BOOL EncryptFile (
PCHAR szSource,
PCHAR szDestination,
PCHAR szPassword);
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//the Begin main.
Void main (void) {
CHAR szSource [100].
CHAR szDestination [100].
CHAR szPassword [100].
Printf (" Encrypt a file. \ n \ n ");
Printf (" Enter the name of the file to be encrypted: ");
The scanf (" % s ", szSource);
Printf (" Enter the name of the output file: ");
The scanf (" % s ", szDestination);
Printf (" Enter the password: ");
The scanf (" % s ", szPassword);
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Call EncryptFile to do the actual encryption.
If (EncryptFile (szSource szDestination, szPassword)) {
Printf (" Encryption of the file % s was a success. \ n ", szSource);
Printf (" The encrypted data is in The file % s \ n ", szDestination);
} else {
HandleError (" Error encrypting file!" );
}
}//End of the main
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Code for the function EncryptFile called by the main.
The static BOOL EncryptFile (
PCHAR szSource,
PCHAR szDestination,
PCHAR szPassword)
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//the Parameters passed are:
//szSource, the name of the input, a plaintext file.
//szDestination, the name of the output, the an encrypted file to be
//created.
//szPassword, the password.
{
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Declare and initialize local variables.
The FILE * hSource;
The FILE * hDestination;
HCRYPTPROV HCRYPTPROV;
HCRYPTKEY hkeys;
HCRYPTHASH hHash;
PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Open source file.
If (hSource=fopen (szSource, "rb")) {
Printf (" The source plaintext file, % s, is open. \ n ", szSource);
} else {
HandleError (" Error opening source plaintext file!" );
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Open the destination file.
If (hDestination=fopen (szDestination, "wb")) {
Printf (" Destination is open the file % s. \ n ", szDestination);
} else {
HandleError (" Error opening destination ciphertext file!" );
}
//the following for a CSP handle
If (CryptAcquireContext (
& HCryptProv,
NULL,//NULL said use the default key container, the default key container name
//for the user login name
NULL,
PROV_RSA_FULL,
{0))
Printf (" A cryptographic provider has had acquired. \ n ");
} else {
If (CryptAcquireContext (
& HCryptProv,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_NEWKEYSET))//create a key container
{
//create a key container is successful, and get the CSP handle
Printf (" A new key container has had been created. \ n ");
} else {
HandleError (" Could not create a new key container. \ n ");
}
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//create a session key (the session key)
//the session key is also called the symmetric key for symmetric encryption algorithm,
//(note: a Session CryptAcquireContext refers to from the function to the calling function
//CryptReleaseContext stage, during the session keys can only exist in a session)
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Create a hash object.
If (CryptCreateHash (
HCryptProv,
CALG_MD5,
0,
0,
& HHash)) {
Printf (" A hash object has had been created. \ n ");
} else {
HandleError (" Error during CryptCreateHash! \n");
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//enter the password to create a hash
If (CryptHashData (
HHash,
(BYTE *) szPassword,
Strlen (szPassword),
{0))
Printf (" The password from The had been added to The hash. \ n ");
} else {
HandleError (" Error during CryptHashData. \ n ");
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//session key generated by hash
If (CryptDeriveKey (
HCryptProv,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related