From d9666cf046a8453b33b3e2fbf4d82295a9f87df3 Mon Sep 17 00:00:00 2001 From: Igor Pavlov Date: Sat, 20 Jan 2007 00:00:00 +0000 Subject: 4.44 beta --- CPP/7zip/Crypto/Zip/StdAfx.h | 8 ++++ CPP/7zip/Crypto/Zip/ZipCipher.cpp | 85 +++++++++++++++++++++++++++++++++++++++ CPP/7zip/Crypto/Zip/ZipCipher.h | 59 +++++++++++++++++++++++++++ CPP/7zip/Crypto/Zip/ZipCrypto.cpp | 65 ++++++++++++++++++++++++++++++ CPP/7zip/Crypto/Zip/ZipCrypto.h | 26 ++++++++++++ 5 files changed, 243 insertions(+) create mode 100755 CPP/7zip/Crypto/Zip/StdAfx.h create mode 100755 CPP/7zip/Crypto/Zip/ZipCipher.cpp create mode 100755 CPP/7zip/Crypto/Zip/ZipCipher.h create mode 100755 CPP/7zip/Crypto/Zip/ZipCrypto.cpp create mode 100755 CPP/7zip/Crypto/Zip/ZipCrypto.h (limited to 'CPP/7zip/Crypto/Zip') diff --git a/CPP/7zip/Crypto/Zip/StdAfx.h b/CPP/7zip/Crypto/Zip/StdAfx.h new file mode 100755 index 00000000..e7fb6986 --- /dev/null +++ b/CPP/7zip/Crypto/Zip/StdAfx.h @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif diff --git a/CPP/7zip/Crypto/Zip/ZipCipher.cpp b/CPP/7zip/Crypto/Zip/ZipCipher.cpp new file mode 100755 index 00000000..639776ce --- /dev/null +++ b/CPP/7zip/Crypto/Zip/ZipCipher.cpp @@ -0,0 +1,85 @@ +// Crypto/ZipCipher.h + +#include "StdAfx.h" + +#include "ZipCipher.h" +#include "Windows/Defs.h" + +#include "../../Common/StreamUtils.h" +#include "../Hash/RandGen.h" + +namespace NCrypto { +namespace NZip { + +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); + + UInt32 processedSize; + _cipher.EncryptHeader(header); + RINOK(WriteStream(outStream, header, kHeaderSize, &processedSize)); + if (processedSize != kHeaderSize) + return E_FAIL; + return S_OK; +} + +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; +} + +HRESULT CDecoder::ReadHeader(ISequentialInStream *inStream) +{ + Byte header[kHeaderSize]; + UInt32 processedSize; + RINOK(ReadStream(inStream, header, kHeaderSize, &processedSize)); + if (processedSize != kHeaderSize) + return E_FAIL; + _cipher.DecryptHeader(header); + return S_OK; +} + +STDMETHODIMP CDecoder::Init() +{ + 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; +} + +}} diff --git a/CPP/7zip/Crypto/Zip/ZipCipher.h b/CPP/7zip/Crypto/Zip/ZipCipher.h new file mode 100755 index 00000000..d750336c --- /dev/null +++ b/CPP/7zip/Crypto/Zip/ZipCipher.h @@ -0,0 +1,59 @@ +// Crypto/ZipCipher.h + +#ifndef __CRYPTO_ZIPCIPHER_H +#define __CRYPTO_ZIPCIPHER_H + +#include "Common/MyCom.h" +#include "Common/Types.h" + +#include "../../ICoder.h" +#include "../../IPassword.h" + +#include "ZipCrypto.h" + +namespace NCrypto { +namespace NZip { + +class CEncoder : + public ICompressFilter, + public ICryptoSetPassword, + public ICryptoSetCRC, + public CMyUnknownImp +{ + CCipher _cipher; + UInt32 _crc; +public: + MY_UNKNOWN_IMP2( + ICryptoSetPassword, + ICryptoSetCRC + ) + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); + STDMETHOD(CryptoSetCRC)(UInt32 crc); + HRESULT WriteHeader(ISequentialOutStream *outStream); +}; + + +class CDecoder: + public ICompressFilter, + public ICryptoSetPassword, + public CMyUnknownImp + // public CBuffer2 +{ + CCipher _cipher; +public: + + MY_UNKNOWN_IMP1(ICryptoSetPassword) + + STDMETHOD(Init)(); + STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size); + + HRESULT ReadHeader(ISequentialInStream *inStream); + STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size); +}; + +}} + +#endif diff --git a/CPP/7zip/Crypto/Zip/ZipCrypto.cpp b/CPP/7zip/Crypto/Zip/ZipCrypto.cpp new file mode 100755 index 00000000..79f0953c --- /dev/null +++ b/CPP/7zip/Crypto/Zip/ZipCrypto.cpp @@ -0,0 +1,65 @@ +// Crypto/ZipCrypto.cpp + +#include "StdAfx.h" + +#include "ZipCipher.h" +#include "../../../Common/CRC.h" + +namespace NCrypto { +namespace NZip { + +static inline UInt32 ZipCRC32(UInt32 c, Byte b) +{ + return CCRC::Table[(c ^ b) & 0xFF] ^ (c >> 8); +} + +void CCipher::UpdateKeys(Byte b) +{ + Keys[0] = ZipCRC32(Keys[0], b); + Keys[1] += Keys[0] & 0xff; + Keys[1] = Keys[1] * 134775813L + 1; + Keys[2] = ZipCRC32(Keys[2], (Byte)(Keys[1] >> 24)); +} + +void CCipher::SetPassword(const Byte *password, UInt32 passwordLength) +{ + Keys[0] = 305419896L; + Keys[1] = 591751049L; + Keys[2] = 878082192L; + for (UInt32 i = 0; i < passwordLength; i++) + UpdateKeys(password[i]); +} + +Byte CCipher::DecryptByteSpec() +{ + UInt32 temp = Keys[2] | 2; + return (Byte)((temp * (temp ^ 1)) >> 8); +} + +Byte CCipher::DecryptByte(Byte encryptedByte) +{ + Byte c = (Byte)(encryptedByte ^ DecryptByteSpec()); + UpdateKeys(c); + return c; +} + +Byte CCipher::EncryptByte(Byte b) +{ + Byte c = (Byte)(b ^ DecryptByteSpec()); + UpdateKeys(b); + return c; +} + +void CCipher::DecryptHeader(Byte *buffer) +{ + for (int i = 0; i < 12; i++) + buffer[i] = DecryptByte(buffer[i]); +} + +void CCipher::EncryptHeader(Byte *buffer) +{ + for (int i = 0; i < 12; i++) + buffer[i] = EncryptByte(buffer[i]); +} + +}} diff --git a/CPP/7zip/Crypto/Zip/ZipCrypto.h b/CPP/7zip/Crypto/Zip/ZipCrypto.h new file mode 100755 index 00000000..6b4ecaaa --- /dev/null +++ b/CPP/7zip/Crypto/Zip/ZipCrypto.h @@ -0,0 +1,26 @@ +// Crypto/ZipCrypto.h + +#ifndef __CRYPTO_ZIP_CRYPTO_H +#define __CRYPTO_ZIP_CRYPTO_H + +namespace NCrypto { +namespace NZip { + +const int kHeaderSize = 12; +class CCipher +{ + UInt32 Keys[3]; + void UpdateKeys(Byte b); + Byte DecryptByteSpec(); +public: + void SetPassword(const Byte *password, UInt32 passwordLength); + Byte DecryptByte(Byte encryptedByte); + Byte EncryptByte(Byte b); + void DecryptHeader(Byte *buffer); + void EncryptHeader(Byte *buffer); + +}; + +}} + +#endif -- cgit v1.2.3