diff options
author | Igor Pavlov <ipavlov@users.sourceforge.net> | 2007-01-20 03:00:00 +0300 |
---|---|---|
committer | Kornel LesiĆski <kornel@geekhood.net> | 2016-05-28 02:15:49 +0300 |
commit | d9666cf046a8453b33b3e2fbf4d82295a9f87df3 (patch) | |
tree | c722ed19b844b53042aec0c1d7d2f8381140a5ed /CPP/7zip/Crypto/Zip/ZipCrypto.cpp | |
parent | 804edc5756fede54dbb1aefda6d39d306111938d (diff) |
4.44 beta
Diffstat (limited to 'CPP/7zip/Crypto/Zip/ZipCrypto.cpp')
-rwxr-xr-x | CPP/7zip/Crypto/Zip/ZipCrypto.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
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]); +} + +}} |