blob: 55ec8e541e439ec41bd82dd8fe8245035b9c028f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
// Crypto/ZipCrypto.cpp
#include "StdAfx.h"
#include "ZipCipher.h"
extern "C"
{
#include "../../../../C/7zCrc.h"
}
namespace NCrypto {
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[2] = CRC_UPDATE_BYTE(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]);
}
}}
|