~ubuntu-branches/ubuntu/precise/p7zip/precise-updates

« back to all changes in this revision

Viewing changes to CPP/7zip/Crypto/Zip/ZipCrypto.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2009-02-14 20:12:27 UTC
  • mfrom: (1.1.11 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090214201227-go63qxm9ozfdma60
Tags: 4.65~dfsg.1-1
* New upstream release.
* Remove wx2.8 Build-Depends added by mistakes (7zG is not yet
  intended to be built).
* Use dh_clean without -k.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Crypto/ZipCrypto.cpp
2
 
 
3
 
#include "StdAfx.h"
4
 
 
5
 
#include "ZipCipher.h"
6
 
extern "C" 
7
 
8
 
#include "../../../../C/7zCrc.h"
9
 
}
10
 
 
11
 
namespace NCrypto {
12
 
namespace NZip {
13
 
 
14
 
void CCipher::UpdateKeys(Byte b)
15
 
{
16
 
  Keys[0] = CRC_UPDATE_BYTE(Keys[0], b);
17
 
  Keys[1] += Keys[0] & 0xff;
18
 
  Keys[1] = Keys[1] * 134775813L + 1;
19
 
  Keys[2] = CRC_UPDATE_BYTE(Keys[2], (Byte)(Keys[1] >> 24));
20
 
}
21
 
 
22
 
void CCipher::SetPassword(const Byte *password, UInt32 passwordLength)
23
 
{
24
 
  Keys[0] = 305419896L;
25
 
  Keys[1] = 591751049L;
26
 
  Keys[2] = 878082192L;
27
 
  for (UInt32 i = 0; i < passwordLength; i++)
28
 
    UpdateKeys(password[i]);
29
 
}
30
 
 
31
 
Byte CCipher::DecryptByteSpec()
32
 
{
33
 
  UInt32 temp = Keys[2] | 2;
34
 
  return (Byte)((temp * (temp ^ 1)) >> 8);
35
 
}
36
 
 
37
 
Byte CCipher::DecryptByte(Byte encryptedByte)
38
 
{
39
 
  Byte c = (Byte)(encryptedByte ^ DecryptByteSpec());
40
 
  UpdateKeys(c);
41
 
  return c;
42
 
}
43
 
 
44
 
Byte CCipher::EncryptByte(Byte b)
45
 
{
46
 
  Byte c = (Byte)(b ^ DecryptByteSpec());
47
 
  UpdateKeys(b);
48
 
  return c;
49
 
}
50
 
 
51
 
void CCipher::DecryptHeader(Byte *buffer)
52
 
{
53
 
  for (int i = 0; i < 12; i++)
54
 
    buffer[i] = DecryptByte(buffer[i]);
55
 
}
56
 
 
57
 
void CCipher::EncryptHeader(Byte *buffer)
58
 
{
59
 
  for (int i = 0; i < 12; i++)
60
 
    buffer[i] = EncryptByte(buffer[i]);
61
 
}
62
 
 
63
 
}}