diff options
Diffstat (limited to 'CPP/7zip/Crypto/ZipCrypto.cpp')
-rwxr-xr-x | CPP/7zip/Crypto/ZipCrypto.cpp | 118 |
1 files changed, 39 insertions, 79 deletions
diff --git a/CPP/7zip/Crypto/ZipCrypto.cpp b/CPP/7zip/Crypto/ZipCrypto.cpp index f5972c57..baaaf98e 100755 --- a/CPP/7zip/Crypto/ZipCrypto.cpp +++ b/CPP/7zip/Crypto/ZipCrypto.cpp @@ -15,114 +15,74 @@ 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]); -} - -Byte CCipher::DecryptByteSpec() -{ - UInt32 temp = Keys[2] | 2; - return (Byte)((temp * (temp ^ 1)) >> 8); -} - -Byte CCipher::DecryptByte(Byte b) -{ - 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); + for (i = 0; i < 3; i++) + Keys2[i] = Keys[i]; return S_OK; } -STDMETHODIMP CEncoder::CryptoSetCRC(UInt32 crc) +STDMETHODIMP CCipher::Init() { - _crc = crc; return S_OK; } -STDMETHODIMP CEncoder::Init() +Byte CCipher::DecryptByteSpec() { - return S_OK; + UInt32 temp = Keys[2] | 2; + return (Byte)((temp * (temp ^ 1)) >> 8); } -HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream) +HRESULT CEncoder::WriteHeader(ISequentialOutStream *outStream, UInt32 crc) { - 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; } }} |