Статус: Участник
Группы: Участники
Зарегистрирован: 05.03.2008(UTC) Сообщений: 11
|
CryptEncryptMessage очень тормозит при обработке файла в 7Мб. А при обработке 20 Мб результата не дождался в течении часа и прервал выполнение. Почему так происходит? Подскажите, плиз. Листинг функции фифрования: Код:
MYSTRUCTURE mCrypt(FILES Files, MYOUTMESSAGE inputMessage, bool test_or_not)
{
MYSTRUCTURE outInfo;
int errorNumber;
char * outMessage, *alg, *ProvName;
FILE *stream;
BYTE* pbContent;
DWORD cbContent;
wchar_t * recipientName;
int x;
HCRYPTPROV hProv;
//--
if (test_or_not==false)
{
recipientName=L"Name";
}
else
{
recipientName=L"Name1"
}
//--
if ((strlen(Files.inputFile)==0) || (strlen(Files.outputFile)==0))
{
cbContent = inputMessage.Number;
pbContent = (BYTE*)malloc(cbContent);
pbContent = inputMessage.Message;
}
else
{
if ((stream = fopen(Files.inputFile, "rb")) == NULL)
{
outInfo.outMessage.Message="Невозможно открыть входной файл";
outInfo.errorNumber=1;
return outInfo;
}
x = FileSizeByName(Files.inputFile);
pbContent = (BYTE*)malloc(x);
fread(pbContent, x, 1, stream);
fclose(stream);
cbContent = x;
}
HCERTSTORE hStoreHandle = 0;
PCCERT_CONTEXT pRecipientCert = NULL;
PCCERT_CONTEXT RecipientCertArray[1];
CRYPT_ENCRYPT_MESSAGE_PARA EncryptParams;
BYTE* pbEncryptedBlob = NULL;
DWORD cbEncryptedBlob;
// Open a system certificate store.
if(hStoreHandle = CertOpenSystemStore(
NULL,
"MY"))
{
// Get a pointer to the recipient's certificate by calling GetRecipientCert.
if(pRecipientCert = CertFindCertificateInStore(
hStoreHandle,
MY_TYPE,
0,
CERT_FIND_SUBJECT_STR,
recipientName,
NULL))
{
RecipientCertArray[0] = pRecipientCert;
DWORD EncryptAlgSize;
CRYPT_ALGORITHM_IDENTIFIER EncryptAlgorithm;
DWORD EncryptParamsSize;
//memset(&EncryptAlgorithm, 0, EncryptAlgSize);
EncryptAlgorithm.Parameters.cbData = 0;
EncryptAlgorithm.Parameters.pbData = NULL;
EncryptAlgorithm.pszObjId = alg;
EncryptAlgSize = sizeof(EncryptAlgorithm);
EncryptParamsSize = sizeof(EncryptParams);
memset(&EncryptParams, 0, EncryptParamsSize);
EncryptParams.cbSize = EncryptParamsSize;
EncryptParams.dwMsgEncodingType = MY_TYPE;
EncryptParams.hCryptProv = 0;
EncryptParams.ContentEncryptionAlgorithm = EncryptAlgorithm;
// Call CryptEncryptMessage.
if(CryptEncryptMessage(
&EncryptParams,
1,
RecipientCertArray,
pbContent,
cbContent,
NULL,
&cbEncryptedBlob))
{
// Allocate memory for the returned BLOB.
if(pbEncryptedBlob = (BYTE*)malloc(cbEncryptedBlob))
{
// Call CryptEncryptMessage again to encrypt the content.
if(CryptEncryptMessage(
&EncryptParams,
1,
RecipientCertArray,
pbContent,
cbContent,
pbEncryptedBlob,
&cbEncryptedBlob))
{
if ((strlen(Files.inputFile)==0) || (strlen(Files.outputFile)==0))
{
outMessage=(char *)pbEncryptedBlob;
errorNumber = 0;
outInfo.outMessage.Number=cbEncryptedBlob;
}
else
{
if ((stream = fopen(Files.outputFile, "wb")) == NULL)
{
outMessage = "Невозможно открыть выходной файл";
errorNumber = 4;
}
else
{
fwrite(pbEncryptedBlob, cbEncryptedBlob, 1, stream);
fclose(stream);
outMessage="";
errorNumber = 0;
outInfo.outMessage.Number=cbEncryptedBlob;
}
}
}
else
{
outMessage = "Encryption failed.";
errorNumber = 5;
}
}
else
{
outMessage = "Memory allocation error while encrypting.";
errorNumber = 3;
}
}
else
{
outMessage = "Getting EncrypBlob size failed.";
errorNumber = 6;
}
}
else
{
outMessage = "A recipient's certificate not found.";
errorNumber = 7;
}
}
else
{
outMessage = "Error getting store handle.";
errorNumber = 2;
}
outInfo.outMessage.Message=outMessage;
outInfo.errorNumber=errorNumber;
return outInfo;
}
|