文件夹加密 //-------------------------------------------------------------------- // Create a hash object. if(!CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash)) { HandleError("Error during CryptCreateHash!"); } //-------------------------------------------------------------------- // Hash in the password data. if(!CryptHashData( hHash, (BYTE *)szPassword, strlen(szPassword), 0)) { HandleError("Error during CryptHashData!"); } //-------------------------------------------------------------------- // Derive a session key from the hash object. if(!CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey)) { HandleError("Error during CryptDeriveKey!"); } //-------------------------------------------------------------------- // Destroy the hash object. CryptDestroyHash(hHash); hHash = 0; } //-------------------------------------------------------------------- // The decryption key is now available, either having been imported // from a BLOB read in from the source file or having been created // using the password. This point in the program is not reached if // the decryption key is not available.
//-------------------------------------------------------------------- // Determine the number of bytes to decrypt at a time. // This must be a multiple of ENCRYPT_BLOCK_SIZE. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; dwBufferLen = dwBlockLen; //-------------------------------------------------------------------- // Allocate memory. if(!(pbBuffer = (BYTE *)malloc(dwBufferLen))) { HandleError("Out of memory!\n"); } //-------------------------------------------------------------------- // Decrypt source file, and write to destination file. do { //-------------------------------------------------------------------- // Read up to dwBlockLen bytes from source file. dwCount = fread( pbBuffer, 1, dwBlockLen, hSource); if(ferror(hSource)) { HandleError("Error reading ciphertext!"); } //-------------------------------------------------------------------- // Decrypt data. if(!CryptDecrypt( hKey, 0, feof(hSource), 0, pbBuffer, &dwCount)) { HandleError("Error during CryptDecrypt!"); } //-------------------------------------------------------------------- // Write data to destination file. fwrite( pbBuffer, 1, dwCount, hDestination); if(ferror(hDestination)) { HandleError("Error writing plaintext!"); } } while(!feof(hSource)); status = TRUE; //-------------------------------------------------------------------- // Close files. if(hSource) fclose(hSource); if(hDestination) fclose(hDestination);
//-------------------------------------------------------------------- // Destroy session key. if(hKey) CryptDestroyKey(hKey); //-------------------------------------------------------------------- // Destroy hash object. if(hHash) CryptDestroyHash(hHash); //-------------------------------------------------------------------- // Release provider handle. if(hCryptProv) CryptReleaseContext(hCryptProv, 0); return status; } // End of Decryptfile//-------------------------------------------------------------------- // This example uses the function HandleError, a simple error // handling function, to print an error message to the standard error // (stderr) file and exit the program. // For most applications, replace this function with one // that does more extensive error reporting.void HandleError(char *s) { fprintf(stderr,"An error occurred in running the program. \n"); fprintf(stderr,"%s\n",s); fprintf(stderr, "Error number %x.\n", GetLastError()); fprintf(stderr, "Program terminating. \n"); exit(1); } // End of HandleError
//--------------------------------------------------------------------
// Create a hash object.
if(!CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
HandleError("Error during CryptCreateHash!");
}
//--------------------------------------------------------------------
// Hash in the password data. if(!CryptHashData(
hHash,
(BYTE *)szPassword,
strlen(szPassword),
0))
{
HandleError("Error during CryptHashData!");
}
//--------------------------------------------------------------------
// Derive a session key from the hash object. if(!CryptDeriveKey(
hCryptProv,
ENCRYPT_ALGORITHM,
hHash,
KEYLENGTH,
&hKey))
{
HandleError("Error during CryptDeriveKey!");
}
//--------------------------------------------------------------------
// Destroy the hash object. CryptDestroyHash(hHash);
hHash = 0;
}
//--------------------------------------------------------------------
// The decryption key is now available, either having been imported
// from a BLOB read in from the source file or having been created
// using the password. This point in the program is not reached if
// the decryption key is not available.
//--------------------------------------------------------------------
// Determine the number of bytes to decrypt at a time.
// This must be a multiple of ENCRYPT_BLOCK_SIZE. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
dwBufferLen = dwBlockLen; //--------------------------------------------------------------------
// Allocate memory. if(!(pbBuffer = (BYTE *)malloc(dwBufferLen)))
{
HandleError("Out of memory!\n");
}
//--------------------------------------------------------------------
// Decrypt source file, and write to destination file. do {
//--------------------------------------------------------------------
// Read up to dwBlockLen bytes from source file. dwCount = fread(
pbBuffer,
1,
dwBlockLen,
hSource);
if(ferror(hSource))
{
HandleError("Error reading ciphertext!");
}
//--------------------------------------------------------------------
// Decrypt data.
if(!CryptDecrypt(
hKey,
0,
feof(hSource),
0,
pbBuffer,
&dwCount))
{
HandleError("Error during CryptDecrypt!");
}
//--------------------------------------------------------------------
// Write data to destination file. fwrite(
pbBuffer,
1,
dwCount,
hDestination);
if(ferror(hDestination))
{
HandleError("Error writing plaintext!");
}
}
while(!feof(hSource));
status = TRUE; //--------------------------------------------------------------------
// Close files.
if(hSource)
fclose(hSource);
if(hDestination)
fclose(hDestination);
//--------------------------------------------------------------------
// Free memory. if(pbKeyBlob)
free(pbKeyBlob);if(pbBuffer)
free(pbBuffer);
//--------------------------------------------------------------------
// Destroy session key. if(hKey)
CryptDestroyKey(hKey); //--------------------------------------------------------------------
// Destroy hash object.
if(hHash)
CryptDestroyHash(hHash); //--------------------------------------------------------------------
// Release provider handle. if(hCryptProv)
CryptReleaseContext(hCryptProv, 0); return status;
} // End of Decryptfile//--------------------------------------------------------------------
// This example uses the function HandleError, a simple error
// handling function, to print an error message to the standard error
// (stderr) file and exit the program.
// For most applications, replace this function with one
// that does more extensive error reporting.void HandleError(char *s)
{
fprintf(stderr,"An error occurred in running the program. \n");
fprintf(stderr,"%s\n",s);
fprintf(stderr, "Error number %x.\n", GetLastError());
fprintf(stderr, "Program terminating. \n");
exit(1);
} // End of HandleError