File: ZipCrc32Cryptograph.cpp

package info (click to toggle)
tuxcmd-modules 0.6.70%2Bds-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,872 kB
  • sloc: cpp: 11,743; ansic: 8,064; makefile: 245
file content (81 lines) | stat: -rw-r--r-- 2,685 bytes parent folder | download | duplicates (4)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive library source distribution and
// is Copyrighted 2000 - 2007 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// For the licensing details refer to the License.txt file.
//
// Web Site: http://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ZipCrc32Cryptograph.h"

bool CZipCrc32Cryptograph::InitDecode(CZipAutoBuffer& password, CZipFileHeader& currentFile, CZipStorage& storage)
{
	CryptInitKeys(password);
	CZipAutoBuffer buf(ZIPARCHIVE_ENCR_HEADER_LEN);
	storage.Read(buf, ZIPARCHIVE_ENCR_HEADER_LEN, false);
	BYTE b = 0;
	for (int i = 0; i < ZIPARCHIVE_ENCR_HEADER_LEN; i++)
	{
		b = buf[i]; // only temporary
		CryptDecode((char&)b);
	}
	// check the last byte
	return currentFile.IsDataDescriptor() ?
		(BYTE(currentFile.m_uModTime >> 8) == b) : (BYTE(currentFile.m_uCrc32 >> 24) == b);
}

void CZipCrc32Cryptograph::InitEncode(CZipAutoBuffer& password, CZipFileHeader& currentFile, CZipStorage& storage)
{
	CZipAutoBuffer buf(ZIPARCHIVE_ENCR_HEADER_LEN);
	// use pseudo-crc since we don't know it yet	
	CryptInitKeys(password);
	srand(UINT(time(NULL)));
	// genereate pseudo-random sequence
	char c;
	char* buffer = (char*)buf;
	for (int i = 0; i < ZIPARCHIVE_ENCR_HEADER_LEN - 2; i++)
	{
		int t1 = rand();
		c = (char)((t1 >> 6) & 0xFF);
		if (!c)
			c = (char)(t1 & 0xFF);
		CryptEncode(c);
		buffer[i] = c;

	}
	long iCrc = (long)currentFile.m_uModTime << 16;	
	c = (char)((iCrc >> 16) & 0xFF);
	CryptEncode(c);
	buffer[ZIPARCHIVE_ENCR_HEADER_LEN - 2] = c;
	c = (char)((iCrc >> 24) & 0xFF);
	CryptEncode(c);
	buffer[ZIPARCHIVE_ENCR_HEADER_LEN - 1] = c;
	storage.Write(buf, ZIPARCHIVE_ENCR_HEADER_LEN, false);
	currentFile.m_uComprSize += ZIPARCHIVE_ENCR_HEADER_LEN;
}

void CZipCrc32Cryptograph::CryptInitKeys(CZipAutoBuffer& password)
{
	m_keys[0] = 305419896L;
	m_keys[1] = 591751049L;
	m_keys[2] = 878082192L;
	for (DWORD i = 0; i < password.GetSize(); i++)
		CryptUpdateKeys(password[i]);
}

void CZipCrc32Cryptograph::CryptUpdateKeys(char c)
{	
	m_keys[0] = CryptCRC32(m_keys[0], c);
	m_keys[1] += m_keys[0] & 0xff;
	m_keys[1] = m_keys[1] * 134775813L + 1;
	c = char(m_keys[1] >> 24);
	m_keys[2] = CryptCRC32(m_keys[2], c);
}