powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / C++ [игнор отключен] [закрыт для гостей] / Дешифровка данных из файла в память
1 сообщений из 1, страница 1 из 1
Дешифровка данных из файла в память
    #33691175
egghead
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
У меня есть зашифрованный файл (процедура, изменяющая БД) (хэш). Мне нужно считать оттуда данные. Их дешифровать и обновить БД. Но дешифрованные данные не должны нигде сохраняться постоянно. т.е. считать из файла, дешифровать их (в память(CMemFile) или в строку) и обновить БД. А файл сам должен остаться зашифрованным. Как мне лучше это сделать?
Сейчас я использую так:
//Get handle to the default provider
if ( ! CryptAcquireContext(
&hCryptProv,
NULL,
MS_ENHANCED_PROV,
PROV_RSA_FULL,
0))
{
AfxMessageBox("Ошибка при выполнении CryptAcquireContext!");
return FALSE;
}

//Create a hash object
if ( !CryptCreateHash(
hCryptProv,
CALG_MD5,
0,
0,
&hHash))
{
AfxMessageBox("Ошибка при выполнении CryptCreateHash!");
return FALSE;
}

//Hash the password
if( !CryptHashData(
hHash,
(BYTE *)"Some Common Data",
sizeof("Some Common Data"),
0))
{
AfxMessageBox("Ошибка при выполнении CryptHashData!");
return FALSE;
}

//derive a session key from the hash object
if ( ! CryptDeriveKey(
hCryptProv,
ENCRYPT_ALGORITHM /*CALG_RC2*/,
hHash,
/*KEYLENGTH*/ CRYPT_EXPORTABLE,
&hKey))
{
CString str;
str.AppendFormat("%x", GetLastError());
AfxMessageBox("Ошибка при выполнении CryptDeriveKey!");
return FALSE;
}

//destroy the hash object
CryptDestroyHash(hHash);
hHash = 0 ;

//--------------------------------------------------------------------
// The session key is now ready. If it is not a key derived from a
// password, the session key encrypted with the encrypter's private
// key has been written to the destination file.

//--------------------------------------------------------------------
// Determine number of bytes to encrypt at a time.
// This must be a multiple of ENCRYPT_BLOCK_SIZE.
// ENCRYPT_BLOCK_SIZE is set by a #define statement.
dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;

//--------------------------------------------------------------------
// Determine the block size. If a block cipher is used,
// it must have room for an extra block.

if(ENCRYPT_BLOCK_SIZE > 1)
dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
else
dwBufferLen = dwBlockLen;

//Allocate memory
if (! (pbBuffer = (BYTE *)malloc(dwBufferLen)))
{
AfxMessageBox("Ошибка при выделении памяти!");
return FALSE;
}

// In a do loop, encrypt the source file and write to the source file.
do {
//--------------------------------------------------------------------
// Read up to dwBlockLen bytes from the source file.
dwCount = fread(pbBuffer, 1, dwBlockLen, hSource);
if(ferror(hSource))
{
AfxMessageBox("Ошибка при чтении незашифрованного текста!");
return FALSE;
}

// Encrypt data.
if(!CryptEncrypt(
hKey,
0,
feof(hSource),
0,
pbBuffer,
&dwCount,
dwBufferLen))
{
AfxMessageBox("Ошибка при выполнении CryptEncrypt!");
return FALSE;
}
...
Рейтинг: 0 / 0
1 сообщений из 1, страница 1 из 1
Форумы / C++ [игнор отключен] [закрыт для гостей] / Дешифровка данных из файла в память
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]