Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Encrypt.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: Encrypt.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Simple Encryption / Decryption class
13 */
14 
15 
16 #include "MemDebug.h"
17 #include "Encrypt.h"
18 
19 // +--------------------------------------------------------------------+
20 
21 void Print(const char* fmt, ...);
22 
23 static long k[4] = {
24  0x3B398E26,
25  0x40C29501,
26  0x614D7630,
27  0x7F59409A
28 };
29 
30 static void encypher(long* v)
31 {
32  DWORD y=v[0];
33  DWORD z=v[1];
34  DWORD sum=0;
35  DWORD delta=0x9e3779b9; // a key schedule constant
36  DWORD n=32; // num iterations
37 
38  while (n-->0) { // basic cycle start
39  sum += delta;
40  y += (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1];
41  z += (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3];
42  }
43 
44  v[0]=y;
45  v[1]=z;
46 }
47 
48 static void decypher(long* v)
49 {
50  DWORD y=v[0];
51  DWORD z=v[1];
52  DWORD sum=0;
53  DWORD delta=0x9e3779b9; // a key schedule constant
54  DWORD n=32; // num iterations
55 
56  sum=delta<<5;
57 
58  while (n-->0) {
59  z-= (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3];
60  y-= (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1];
61  sum-=delta;
62  }
63 
64  v[0]=y;
65  v[1]=z;
66 }
67 
68 // +-------------------------------------------------------------------+
69 
70 Text
72 {
73  int len = block.length();
74 
75  if (len < 1)
76  return Text();
77 
78  // pad to eight byte chunks
79  if (len & 0x7) {
80  len /= 8;
81  len *= 8;
82  len += 8;
83  }
84 
85  BYTE* work = new(__FILE__,__LINE__) BYTE[len];
86  ZeroMemory(work, len);
87  CopyMemory(work, block.data(), block.length());
88 
89  long* v = (long*) work;
90  for (int i = 0; i < len/8; i++) {
91  encypher(v);
92  v += 2;
93  }
94 
95  Text cypher((const char*) work, len);
96  delete [] work;
97  return cypher;
98 }
99 
100 // +-------------------------------------------------------------------+
101 
102 Text
104 {
105  int len = block.length();
106 
107  if (len & 0x7) {
108  Print("WARNING: attempt to decrypt odd length block (len=%d)\n", len);
109  return Text();
110  }
111 
112  BYTE* work = new(__FILE__,__LINE__) BYTE[len];
113  CopyMemory(work, block.data(), len);
114 
115  long* v = (long*) work;
116  for (int i = 0; i < len/8; i++) {
117  decypher(v);
118  v += 2;
119  }
120 
121  Text clear((const char*) work, len);
122  delete [] work;
123  return clear;
124 }
125 
126 // +-------------------------------------------------------------------+
127 
128 static const char* codes = "abcdefghijklmnop";
129 
130 Text
132 {
133  int len = block.length() * 2;
134  char* work = new(__FILE__,__LINE__) char[len + 1];
135 
136  for (int i = 0; i < block.length(); i++) {
137  BYTE b = (BYTE) (block.data()[i]);
138  work[2*i] = codes[b>>4 & 0xf];
139  work[2*i+1] = codes[b & 0xf];
140  }
141 
142  work[len] = 0;
143 
144  Text code(work, len);
145  delete [] work;
146  return code;
147 }
148 
149 // +-------------------------------------------------------------------+
150 
151 Text
153 {
154  int len = block.length() / 2;
155  char* work = new(__FILE__,__LINE__) char[len + 1];
156 
157  for (int i = 0; i < len; i++) {
158  char u = block[2*i];
159  char l = block[2*i + 1];
160 
161  work[i] = (u - codes[0]) << 4 |
162  (l - codes[0]);
163  }
164 
165  work[len] = 0;
166 
167  Text clear(work, len);
168  delete [] work;
169  return clear;
170 }
171