![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Unverified User
Join Date: Jun 2005
Location: NJ
Posts: 23
Rep Power: 0
![]() |
SpcFileWipe algorithm in Secure Programming Cookbook not working
hey, havent been posting at any forums forever.. im starting to get back to coding though and this will be my forum of choice. greetings to dawei, thedark, and nxxion...
well, i was reading the book "secure programming cookbook" , and i figured instead of manually copying the code for a secure file deletion algorithm that looked interesting i'd google for SpcFileWipe---the name of the algorithm in the book. i found someone else who had typed out some of the algorithms from the book. the particular problem with the algorithm im having is that the following section gives an error (GetLastError returns -2146893804 (-0x7FF6FFEC in hex i believe)) .. if (CryptAcquireContext(&hProvider, 0, 0, 0, CRYPT_VERIFYCONTEXT)==FALSE)
{
char asd[80];
wsprintf(asd,"%d",GetLastError());
MessageBox(0,asd,asd,0);
return FALSE;
}this link didn't show anything about this error return code message... http://msdn.microsoft.com/library/de...irecontext.asp i tried changing the following code to this, and the error no longer happens but program goes into some sort of infinite loop: if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)==FALSE)
{
char asd[80];
wsprintf(asd,"%d",GetLastError());
MessageBox(0,asd,asd,0);
return FALSE;
}this is the entire source code of the algorithms in the book.. i do not understand everything fully, however i have not studied this stuff in depth yet... if anyone sees anything stupid with the code that simply jumps out please let me know... no emergency for help here heh. curious what's wrong with this script however... mainly just looking to get back into some coding and posting at a c++ forum again.. im going to watch some baseball but ill check back later and look over the code myself a bit more as well.. regards, --c0ldshadow #include <windows.h>
#include <wincrypt.h>
//#include "spc.h"
#define SPC_WIPE_BUFSIZE 4096
static BOOL RandomPass(HANDLE hFile, HCRYPTPROV hProvider, DWORD dwFileSize)
{
BYTE pbBuffer[SPC_WIPE_BUFSIZE];
DWORD cbBuffer, cbTotalWritten, cbWritten;
if (SetFilePointer(hFile, 0, 0, FILE_BEGIN) == 0xFFFFFFFF) return FALSE;
while (dwFileSize > 0) {
cbBuffer = (dwFileSize > sizeof(pbBuffer) ? sizeof(pbBuffer) : dwFileSize);
if (!CryptGenRandom(hProvider, cbBuffer, pbBuffer)) return FALSE;
for (cbTotalWritten = 0; cbBuffer > 0; cbTotalWritten += cbWritten)
if (!WriteFile(hFile, pbBuffer + cbTotalWritten, cbBuffer - cbTotalWritten,
&cbWritten, 0)) return FALSE;
dwFileSize -= cbTotalWritten;
}
return TRUE;
}
static BOOL PatternPass(HANDLE hFile, BYTE *pbBuffer, DWORD cbBuffer, DWORD dwFileSize) {
DWORD cbTotalWritten, cbWrite, cbWritten;
if (!cbBuffer || SetFilePointer(hFile, 0, 0, FILE_BEGIN) == 0xFFFFFFFF) return FALSE;
while (dwFileSize > 0) {
cbWrite = (dwFileSize > cbBuffer ? cbBuffer : dwFileSize);
for (cbTotalWritten = 0; cbWrite > 0; cbTotalWritten += cbWritten)
if (!WriteFile(hFile, pbBuffer + cbTotalWritten, cbWrite - cbTotalWritten,
&cbWritten, 0)) return FALSE;
dwFileSize -= cbTotalWritten;
}
return TRUE;
}
BOOL SpcWipeFile(HANDLE hFile) {
BYTE pbBuffer[SPC_WIPE_BUFSIZE];
DWORD dwCount, dwFileSize, dwIndex, dwPass;
HCRYPTPROV hProvider;
static BYTE pbSinglePats[16] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
};
static BYTE pbTriplePats[6][3] = {
{ 0x92, 0x49, 0x24 }, { 0x49, 0x24, 0x92 }, { 0x24, 0x92, 0x49 },
{ 0x6d, 0xb6, 0xdb }, { 0xb6, 0xdb, 0x6d }, { 0xdb, 0x6d, 0xb6 }
};
static DWORD cbPattern = sizeof(pbTriplePats[0]);
if ((dwFileSize = GetFileSize(hFile, 0)) == INVALID_FILE_SIZE)
{
return FALSE;
}
if (!dwFileSize) return TRUE;
if (CryptAcquireContext(&hProvider, 0, 0, 0, CRYPT_VERIFYCONTEXT)==FALSE)
{
char asd[80];
wsprintf(asd,"%d",GetLastError());
MessageBox(0,asd,asd,0);
return FALSE;
}
for (dwPass = 0; dwPass < 4; dwPass++)
if (!RandomPass(hFile, hProvider, dwFileSize)) {
CryptReleaseContext(hProvider, 0);
return FALSE;
}
memset(pbBuffer, pbSinglePats[5], sizeof(pbBuffer));
if (!PatternPass(hFile, pbBuffer, sizeof(pbBuffer), dwFileSize)) {
CryptReleaseContext(hProvider, 0);
return FALSE;
}
memset(pbBuffer, pbSinglePats[10], sizeof(pbBuffer));
if (!PatternPass(hFile, pbBuffer, sizeof(pbBuffer), dwFileSize)) {
CryptReleaseContext(hProvider, 0);
return FALSE;
}
cbPattern = sizeof(pbTriplePats[0]);
for (dwPass = 0; dwPass < 3; dwPass++) {
dwCount = sizeof(pbBuffer) / cbPattern;
for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
CopyMemory(pbBuffer + (dwIndex * cbPattern), pbTriplePats[dwPass],
cbPattern);
if (!PatternPass(hFile, pbBuffer, cbPattern * dwCount, dwFileSize)) {
CryptReleaseContext(hProvider, 0);
return FALSE;
}
}
for (dwPass = 0; dwPass < sizeof(pbSinglePats); dwPass++) {
memset(pbBuffer, pbSinglePats[dwPass], sizeof(pbBuffer));
if (!PatternPass(hFile, pbBuffer, sizeof(pbBuffer), dwFileSize)) {
CryptReleaseContext(hProvider, 0);
return FALSE;
}
}
for (dwPass = 0; dwPass < sizeof(pbTriplePats) / cbPattern; dwPass++) {
dwCount = sizeof(pbBuffer) / cbPattern;
for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
CopyMemory(pbBuffer + (dwIndex * cbPattern), pbTriplePats[dwPass],
cbPattern);
if (!PatternPass(hFile, pbBuffer, cbPattern * dwCount, dwFileSize)) {
CryptReleaseContext(hProvider, 0);
return FALSE;
}
}
for (dwPass = 0; dwPass < 4; dwPass++)
if (!RandomPass(hFile, hProvider, dwFileSize)) {
CryptReleaseContext(hProvider, 0);
return FALSE;
}
CryptReleaseContext(hProvider, 0);
return TRUE;
}
__________________
DeepTide The way is shut. It was made by those who are dead and the Dead keep it. The way is shut. |
|
|
|
|
|
#2 | |
|
Expert Programmer
Join Date: Jun 2005
Posts: 893
Rep Power: 4
![]() |
-2146893804 comes out as hex FFFFFFFF80090014, which is "Invalid provider type specified." according to the error lookup tool that came with VS7.
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|