ZipCrypto.cpp (p7zip_15.14.1_src_all) | : | ZipCrypto.cpp (p7zip_16.02_src_all) | ||
---|---|---|---|---|
skipping to change at line 16 | skipping to change at line 16 | |||
#include "../Common/StreamUtils.h" | #include "../Common/StreamUtils.h" | |||
#include "RandGen.h" | #include "RandGen.h" | |||
#include "ZipCrypto.h" | #include "ZipCrypto.h" | |||
namespace NCrypto { | namespace NCrypto { | |||
namespace NZip { | namespace NZip { | |||
#define UPDATE_KEYS(b) { \ | #define UPDATE_KEYS(b) { \ | |||
Key0 = CRC_UPDATE_BYTE(Key0, b); \ | key0 = CRC_UPDATE_BYTE(key0, b); \ | |||
Key1 = (Key1 + (Key0 & 0xFF)) * 0x8088405 + 1; \ | key1 = (key1 + (key0 & 0xFF)) * 0x8088405 + 1; \ | |||
Key2 = CRC_UPDATE_BYTE(Key2, (Byte)(Key1 >> 24)); } \ | key2 = CRC_UPDATE_BYTE(key2, (Byte)(key1 >> 24)); } \ | |||
#define DECRYPT_BYTE_1 UInt32 temp = Key2 | 2; | #define DECRYPT_BYTE_1 UInt32 temp = key2 | 2; | |||
#define DECRYPT_BYTE_2 ((Byte)((temp * (temp ^ 1)) >> 8)) | #define DECRYPT_BYTE_2 ((Byte)((temp * (temp ^ 1)) >> 8)) | |||
STDMETHODIMP CCipher::CryptoSetPassword(const Byte *data, UInt32 size) | STDMETHODIMP CCipher::CryptoSetPassword(const Byte *data, UInt32 size) | |||
{ | { | |||
UInt32 Key0 = 0x12345678; | UInt32 key0 = 0x12345678; | |||
UInt32 Key1 = 0x23456789; | UInt32 key1 = 0x23456789; | |||
UInt32 Key2 = 0x34567890; | UInt32 key2 = 0x34567890; | |||
for (UInt32 i = 0; i < size; i++) | for (UInt32 i = 0; i < size; i++) | |||
UPDATE_KEYS(data[i]); | UPDATE_KEYS(data[i]); | |||
KeyMem0 = Key0; | KeyMem0 = key0; | |||
KeyMem1 = Key1; | KeyMem1 = key1; | |||
KeyMem2 = Key2; | KeyMem2 = key2; | |||
return S_OK; | return S_OK; | |||
} | } | |||
STDMETHODIMP CCipher::Init() | STDMETHODIMP CCipher::Init() | |||
{ | { | |||
return S_OK; | return S_OK; | |||
} | } | |||
HRESULT CEncoder::WriteHeader_Check16(ISequentialOutStream *outStream, UInt16 cr c) | HRESULT CEncoder::WriteHeader_Check16(ISequentialOutStream *outStream, UInt16 cr c) | |||
skipping to change at line 63 | skipping to change at line 63 | |||
// h[kHeaderSize - 2] = (Byte)(crc); | // h[kHeaderSize - 2] = (Byte)(crc); | |||
h[kHeaderSize - 1] = (Byte)(crc >> 8); | h[kHeaderSize - 1] = (Byte)(crc >> 8); | |||
RestoreKeys(); | RestoreKeys(); | |||
Filter(h, kHeaderSize); | Filter(h, kHeaderSize); | |||
return WriteStream(outStream, h, kHeaderSize); | return WriteStream(outStream, h, kHeaderSize); | |||
} | } | |||
STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size) | STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size) | |||
{ | { | |||
UInt32 Key0 = this->Key0; | UInt32 key0 = this->Key0; | |||
UInt32 Key1 = this->Key1; | UInt32 key1 = this->Key1; | |||
UInt32 Key2 = this->Key2; | UInt32 key2 = this->Key2; | |||
for (UInt32 i = 0; i < size; i++) | for (UInt32 i = 0; i < size; i++) | |||
{ | { | |||
Byte b = data[i]; | Byte b = data[i]; | |||
DECRYPT_BYTE_1 | DECRYPT_BYTE_1 | |||
data[i] = (Byte)(b ^ DECRYPT_BYTE_2); | data[i] = (Byte)(b ^ DECRYPT_BYTE_2); | |||
UPDATE_KEYS(b); | UPDATE_KEYS(b); | |||
} | } | |||
this->Key0 = Key0; | this->Key0 = key0; | |||
this->Key1 = Key1; | this->Key1 = key1; | |||
this->Key2 = Key2; | this->Key2 = key2; | |||
return size; | return size; | |||
} | } | |||
HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream) | HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream) | |||
{ | { | |||
return ReadStream_FAIL(inStream, _header, kHeaderSize); | return ReadStream_FAIL(inStream, _header, kHeaderSize); | |||
} | } | |||
void CDecoder::Init_BeforeDecode() | void CDecoder::Init_BeforeDecode() | |||
{ | { | |||
RestoreKeys(); | RestoreKeys(); | |||
Filter(_header, kHeaderSize); | Filter(_header, kHeaderSize); | |||
} | } | |||
STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size) | STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size) | |||
{ | { | |||
UInt32 Key0 = this->Key0; | UInt32 key0 = this->Key0; | |||
UInt32 Key1 = this->Key1; | UInt32 key1 = this->Key1; | |||
UInt32 Key2 = this->Key2; | UInt32 key2 = this->Key2; | |||
for (UInt32 i = 0; i < size; i++) | for (UInt32 i = 0; i < size; i++) | |||
{ | { | |||
DECRYPT_BYTE_1 | DECRYPT_BYTE_1 | |||
Byte b = (Byte)(data[i] ^ DECRYPT_BYTE_2); | Byte b = (Byte)(data[i] ^ DECRYPT_BYTE_2); | |||
UPDATE_KEYS(b); | UPDATE_KEYS(b); | |||
data[i] = b; | data[i] = b; | |||
} | } | |||
this->Key0 = Key0; | this->Key0 = key0; | |||
this->Key1 = Key1; | this->Key1 = key1; | |||
this->Key2 = Key2; | this->Key2 = key2; | |||
return size; | return size; | |||
} | } | |||
}} | }} | |||
End of changes. 8 change blocks. | ||||
22 lines changed or deleted | 22 lines changed or added |