summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Encrypt.cpp
blob: 176d7d0a65940ff501f696724d1e67c0ad4ae45b (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*  Starshatter OpenSource Distribution
    Copyright (c) 1997-2004, Destroyer Studios LLC.
    All Rights Reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name "Destroyer Studios" nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.

    SUBSYSTEM:    nGenEx.lib
    FILE:         Encrypt.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Simple Encryption / Decryption class
*/


#include "MemDebug.h"
#include "Encrypt.h"

// +--------------------------------------------------------------------+

void Print(const char* fmt, ...);

static long k[4] = {
    0x3B398E26,
    0x40C29501,
    0x614D7630,
    0x7F59409A
};

static void encypher(long* v)
{
    DWORD y=v[0];
    DWORD z=v[1];
    DWORD sum=0;
    DWORD delta=0x9e3779b9;                // a key schedule constant
    DWORD n=32;                            // num iterations

    while (n-->0) {                        // basic cycle start
        sum += delta;
        y += (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1];
        z += (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3];
    }

    v[0]=y;
    v[1]=z;
}

static void decypher(long* v)
{
    DWORD y=v[0];
    DWORD z=v[1];
    DWORD sum=0;
    DWORD delta=0x9e3779b9;                // a key schedule constant
    DWORD n=32;                            // num iterations

    sum=delta<<5;

    while (n-->0) {
        z-= (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3];
        y-= (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1];
        sum-=delta;
    }

    v[0]=y;
    v[1]=z;
}

// +-------------------------------------------------------------------+

Text
Encryption::Encrypt(Text block)
{
    int len = block.length();

    if (len < 1)
    return Text();

    // pad to eight byte chunks
    if (len & 0x7) {
        len /= 8;
        len *= 8;
        len += 8;
    }

    BYTE* work = new(__FILE__,__LINE__) BYTE[len];
    ZeroMemory(work, len);
    CopyMemory(work, block.data(), block.length());

    long* v = (long*) work;
    for (int i = 0; i < len/8; i++) {
        encypher(v);
        v += 2;
    }

    Text cypher((const char*) work, len);
    delete [] work;
    return cypher;
}

// +-------------------------------------------------------------------+

Text
Encryption::Decrypt(Text block)
{
    int  len    = block.length();

    if (len & 0x7) {
        Print("WARNING: attempt to decrypt odd length block (len=%d)\n", len);
        return Text();
    }

    BYTE* work = new(__FILE__,__LINE__) BYTE[len];
    CopyMemory(work, block.data(), len);

    long* v = (long*) work;
    for (int i = 0; i < len/8; i++) {
        decypher(v);
        v += 2;
    }

    Text clear((const char*) work, len);
    delete [] work;
    return clear;
}

// +-------------------------------------------------------------------+

static const char* codes = "abcdefghijklmnop";

Text
Encryption::Encode(Text block)
{
    int   len  = block.length() * 2;
    char* work = new(__FILE__,__LINE__) char[len + 1];

    for (int i = 0; i < block.length(); i++) {
        BYTE b = (BYTE) (block.data()[i]);
        work[2*i]   = codes[b>>4 & 0xf];
        work[2*i+1] = codes[b    & 0xf];
    }

    work[len] = 0;

    Text code(work, len);
    delete [] work;
    return code;
}

// +-------------------------------------------------------------------+

Text
Encryption::Decode(Text block)
{
    int   len  = block.length() / 2;
    char* work = new(__FILE__,__LINE__) char[len + 1];

    for (int i = 0; i < len; i++) {
        char u = block[2*i];
        char l = block[2*i + 1];

        work[i] = (u - codes[0]) << 4 |
        (l - codes[0]);
    }

    work[len] = 0;

    Text clear(work, len);
    delete [] work;
    return clear;
}