mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-04-07 13:55:33 +08:00
Decryptors ... small refactoring
This commit is contained in:
@ -513,16 +513,75 @@ void ECMADecryptor::SetCryptData(_ecmaCryptData & data)
|
||||
}
|
||||
void ECMADecryptor::Decrypt(char* data , const size_t size, const unsigned long start_iv_block)
|
||||
{
|
||||
if (bVerify)
|
||||
if (!bVerify) return;
|
||||
|
||||
unsigned char* data_out = NULL;
|
||||
Decrypt((unsigned char*)data, size, data_out, start_iv_block);
|
||||
|
||||
if (data_out)
|
||||
{
|
||||
unsigned char* data_out = NULL;
|
||||
Decrypt((unsigned char*)data, size, data_out, start_iv_block);
|
||||
|
||||
if (data_out)
|
||||
memcpy(data, data_out, size);
|
||||
delete []data_out;
|
||||
}
|
||||
}
|
||||
void ECMADecryptor::Decrypt(char* data , const size_t size, const unsigned long stream_pos, const size_t block_size)
|
||||
{
|
||||
if (!bVerify) return;
|
||||
//rc4 only
|
||||
if (cryptData.cipherAlgorithm != CRYPT_METHOD::RC4) return;
|
||||
|
||||
unsigned char* data_out = new unsigned char[size];
|
||||
|
||||
unsigned char* pnCurrDest = data_out;
|
||||
const unsigned char* pnCurrSrc = (unsigned char* )data;
|
||||
long nCurrPos = stream_pos;
|
||||
unsigned short nBytesLeft = size;
|
||||
|
||||
while(nBytesLeft > 0)
|
||||
{
|
||||
// initialize codec for current stream position
|
||||
|
||||
int block_index = (nCurrPos / block_size);
|
||||
{
|
||||
memcpy(data, data_out, size);
|
||||
delete []data_out;
|
||||
_buf pPassword (password);
|
||||
_buf pSalt (cryptData.saltValue);
|
||||
|
||||
_buf hashKey = GenerateHashKey(pSalt, pPassword, cryptData.hashSize, cryptData.spinCount, cryptData.hashAlgorithm, block_index);
|
||||
CorrectHashSize(hashKey, cryptData.keySize, 0);
|
||||
|
||||
if (cryptData.keySize == 5) CorrectHashSize(hashKey, 16, 0); //40-bit crypt key !!!
|
||||
|
||||
rc4Decryption.SetKey(hashKey.ptr, hashKey.size);
|
||||
}
|
||||
|
||||
const long offset = nCurrPos % block_size;
|
||||
{//skip
|
||||
unsigned char pnDummy[ 1024 ];
|
||||
|
||||
size_t nBytesLeft = offset;
|
||||
bool bResult = true;
|
||||
while(bResult && (nBytesLeft > 0))
|
||||
{
|
||||
size_t nBlockLen = nBytesLeft < sizeof(pnDummy) ? nBytesLeft : sizeof(pnDummy);
|
||||
rc4Decryption.ProcessData(pnDummy, pnDummy, nBlockLen);
|
||||
nBytesLeft -= nBlockLen;
|
||||
}
|
||||
}
|
||||
unsigned short nBlockLeft = static_cast<unsigned short>(block_size - offset);
|
||||
unsigned short nDecBytes = nBytesLeft < nBlockLeft ? nBytesLeft : nBlockLeft;
|
||||
|
||||
rc4Decryption.ProcessData(pnCurrDest, pnCurrSrc, static_cast<int>(nDecBytes));
|
||||
|
||||
pnCurrDest += nDecBytes;
|
||||
pnCurrSrc += nDecBytes;
|
||||
nCurrPos += nDecBytes;
|
||||
|
||||
nBytesLeft = nBytesLeft - nDecBytes;
|
||||
}
|
||||
if (data_out)
|
||||
{
|
||||
memcpy(data, data_out, size);
|
||||
delete []data_out;
|
||||
}
|
||||
}
|
||||
bool ECMADecryptor::CheckDataIntegrity(unsigned char* data, int size)
|
||||
@ -564,7 +623,8 @@ bool ECMADecryptor::CheckDataIntegrity(unsigned char* data, int size)
|
||||
|
||||
return (hmac == expected);
|
||||
}
|
||||
void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*& data_out, int start_iv_block)
|
||||
|
||||
void ECMADecryptor::Decrypt(unsigned char* data_inp, int size, unsigned char*& data_out, unsigned long start_iv_block)
|
||||
{
|
||||
data_out = new unsigned char[size];
|
||||
|
||||
|
||||
@ -62,7 +62,8 @@ namespace CRYPT
|
||||
class Decryptor
|
||||
{
|
||||
public:
|
||||
virtual void Decrypt(char* data, const size_t size, const unsigned long stream_pos) = 0;
|
||||
virtual void Decrypt(char* data, const size_t size, const unsigned long stream_pos, const size_t block_size) = 0;
|
||||
virtual void Decrypt(char* data, const size_t size, const unsigned long block_index) = 0;
|
||||
virtual bool SetPassword(std::wstring password) = 0;
|
||||
virtual bool IsVerify() = 0;
|
||||
|
||||
@ -128,9 +129,10 @@ public:
|
||||
ECMADecryptor();
|
||||
virtual ~ECMADecryptor();
|
||||
|
||||
void Decrypt (unsigned char* data, int size, unsigned char*& data_out, int start_iv_block = 0);
|
||||
|
||||
virtual void Decrypt (char* data, const size_t size, const unsigned long stream_pos, const size_t block_size);
|
||||
virtual void Decrypt (char* data, const size_t size, const unsigned long start_iv_block);
|
||||
|
||||
virtual bool SetPassword (std::wstring password);
|
||||
virtual bool IsVerify();
|
||||
|
||||
@ -138,6 +140,8 @@ public:
|
||||
|
||||
void SetCryptData(_ecmaCryptData &data);
|
||||
|
||||
void Decrypt (unsigned char* data, int size, unsigned char*& data_out, unsigned long start_iv_block);
|
||||
|
||||
private:
|
||||
|
||||
std::wstring password;
|
||||
|
||||
@ -565,26 +565,26 @@ bool ECMACryptFile::EncryptOfficeFile(std::wstring file_name_inp, std::wstring f
|
||||
pStorage->close();
|
||||
delete pStorage;
|
||||
|
||||
|
||||
//test back---------------------------------------------------------------------------------test back
|
||||
ECMADecryptor decryptor;
|
||||
|
||||
decryptor.SetCryptData(cryptData);
|
||||
|
||||
if (decryptor.SetPassword(password))
|
||||
{
|
||||
unsigned char* data_out2 = NULL;
|
||||
decryptor.Decrypt(data_out, lengthData, data_out2);
|
||||
|
||||
bool bDataIntegrity = decryptor.CheckDataIntegrity(data_out, lengthData);
|
||||
|
||||
NSFile::CFileBinary test;
|
||||
|
||||
test.CreateFileW(file_name_out + L"-back.oox");
|
||||
test.WriteFile(data_out2, lengthFileSize);
|
||||
test.CloseFile();
|
||||
}
|
||||
//test back---------------------------------------------------------------------------------test back
|
||||
//
|
||||
////test back---------------------------------------------------------------------------------test back
|
||||
// ECMADecryptor decryptor;
|
||||
//
|
||||
// decryptor.SetCryptData(cryptData);
|
||||
//
|
||||
// if (decryptor.SetPassword(password))
|
||||
// {
|
||||
// unsigned char* data_out2 = NULL;
|
||||
// decryptor.Decrypt(data_out, lengthData, data_out2, 0);
|
||||
//
|
||||
// bool bDataIntegrity = decryptor.CheckDataIntegrity(data_out, lengthData);
|
||||
//
|
||||
// NSFile::CFileBinary test;
|
||||
//
|
||||
// test.CreateFileW(file_name_out + L"-back.oox");
|
||||
// test.WriteFile(data_out2, lengthFileSize);
|
||||
// test.CloseFile();
|
||||
// }
|
||||
////test back---------------------------------------------------------------------------------test back
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -717,7 +717,7 @@ bool ECMACryptFile::DecryptOfficeFile(std::wstring file_name_inp, std::wstring f
|
||||
|
||||
lengthData = *((_UINT64*)data);
|
||||
|
||||
decryptor.Decrypt(data + 8, readData, data_out);//todoo сделать покусочное чтение декриптование
|
||||
decryptor.Decrypt(data + 8, readData, data_out, 0);//todoo сделать покусочное чтение декриптование
|
||||
|
||||
if (data_out)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user