mirror of
https://github.com/Xevion/easy7zip.git
synced 2026-01-31 14:24:09 -06:00
9.11
This commit is contained in:
committed by
Kornel Lesiński
parent
db5eb6d638
commit
993daef9cb
@@ -15,18 +15,26 @@ namespace NZip {
|
||||
void CCipher::UpdateKeys(Byte b)
|
||||
{
|
||||
Keys[0] = CRC_UPDATE_BYTE(Keys[0], b);
|
||||
Keys[1] += Keys[0] & 0xff;
|
||||
Keys[1] = Keys[1] * 134775813L + 1;
|
||||
Keys[1] = (Keys[1] + (Keys[0] & 0xFF)) * 0x8088405 + 1;
|
||||
Keys[2] = CRC_UPDATE_BYTE(Keys[2], (Byte)(Keys[1] >> 24));
|
||||
}
|
||||
|
||||
void CCipher::SetPassword(const Byte *password, UInt32 passwordLen)
|
||||
STDMETHODIMP CCipher::CryptoSetPassword(const Byte *password, UInt32 passwordLen)
|
||||
{
|
||||
Keys[0] = 305419896L;
|
||||
Keys[1] = 591751049L;
|
||||
Keys[2] = 878082192L;
|
||||
for (UInt32 i = 0; i < passwordLen; i++)
|
||||
Keys[0] = 0x12345678;
|
||||
Keys[1] = 0x23456789;
|
||||
Keys[2] = 0x34567890;
|
||||
UInt32 i;
|
||||
for (i = 0; i < passwordLen; i++)
|
||||
UpdateKeys(password[i]);
|
||||
for (i = 0; i < 3; i++)
|
||||
Keys2[i] = Keys[i];
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CCipher::Init()
|
||||
{
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
Byte CCipher::DecryptByteSpec()
|
||||
@@ -35,94 +43,46 @@ Byte CCipher::DecryptByteSpec()
|
||||
return (Byte)((temp * (temp ^ 1)) >> 8);
|
||||
}
|
||||
|
||||
Byte CCipher::DecryptByte(Byte b)
|
||||
HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream, UInt32 crc)
|
||||
{
|
||||
Byte c = (Byte)(b ^ DecryptByteSpec());
|
||||
UpdateKeys(c);
|
||||
return c;
|
||||
}
|
||||
|
||||
Byte CCipher::EncryptByte(Byte b)
|
||||
{
|
||||
Byte c = (Byte)(b ^ DecryptByteSpec());
|
||||
UpdateKeys(b);
|
||||
return c;
|
||||
}
|
||||
|
||||
void CCipher::DecryptHeader(Byte *buf)
|
||||
{
|
||||
for (unsigned i = 0; i < kHeaderSize; i++)
|
||||
buf[i] = DecryptByte(buf[i]);
|
||||
}
|
||||
|
||||
void CCipher::EncryptHeader(Byte *buf)
|
||||
{
|
||||
for (unsigned i = 0; i < kHeaderSize; i++)
|
||||
buf[i] = EncryptByte(buf[i]);
|
||||
}
|
||||
|
||||
STDMETHODIMP CEncoder::CryptoSetPassword(const Byte *data, UInt32 size)
|
||||
{
|
||||
_cipher.SetPassword(data, size);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CEncoder::CryptoSetCRC(UInt32 crc)
|
||||
{
|
||||
_crc = crc;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CEncoder::Init()
|
||||
{
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream)
|
||||
{
|
||||
Byte header[kHeaderSize];
|
||||
g_RandomGenerator.Generate(header, kHeaderSize - 2);
|
||||
|
||||
header[kHeaderSize - 1] = Byte(_crc >> 24);
|
||||
header[kHeaderSize - 2] = Byte(_crc >> 16);
|
||||
|
||||
_cipher.EncryptHeader(header);
|
||||
return WriteStream(outStream, header, kHeaderSize);
|
||||
Byte h[kHeaderSize];
|
||||
g_RandomGenerator.Generate(h, kHeaderSize - 2);
|
||||
h[kHeaderSize - 1] = (Byte)(crc >> 24);
|
||||
h[kHeaderSize - 2] = (Byte)(crc >> 16);
|
||||
RestoreKeys();
|
||||
Filter(h, kHeaderSize);
|
||||
return WriteStream(outStream, h, kHeaderSize);
|
||||
}
|
||||
|
||||
STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size)
|
||||
{
|
||||
UInt32 i;
|
||||
for (i = 0; i < size; i++)
|
||||
data[i] = _cipher.EncryptByte(data[i]);
|
||||
return i;
|
||||
}
|
||||
|
||||
STDMETHODIMP CDecoder::CryptoSetPassword(const Byte *data, UInt32 size)
|
||||
{
|
||||
_cipher.SetPassword(data, size);
|
||||
return S_OK;
|
||||
for (UInt32 i = 0; i < size; i++)
|
||||
{
|
||||
Byte b = data[i];
|
||||
data[i] = (Byte)(b ^ DecryptByteSpec());;
|
||||
UpdateKeys(b);
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream)
|
||||
{
|
||||
Byte header[kHeaderSize];
|
||||
RINOK(ReadStream_FAIL(inStream, header, kHeaderSize));
|
||||
_cipher.DecryptHeader(header);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP CDecoder::Init()
|
||||
{
|
||||
Byte h[kHeaderSize];
|
||||
RINOK(ReadStream_FAIL(inStream, h, kHeaderSize));
|
||||
RestoreKeys();
|
||||
Filter(h, kHeaderSize);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size)
|
||||
{
|
||||
UInt32 i;
|
||||
for (i = 0; i < size; i++)
|
||||
data[i] = _cipher.DecryptByte(data[i]);
|
||||
return i;
|
||||
for (UInt32 i = 0; i < size; i++)
|
||||
{
|
||||
Byte c = (Byte)(data[i] ^ DecryptByteSpec());
|
||||
UpdateKeys(c);
|
||||
data[i] = c;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user