/****************************************************************************
* [S]imulated [M]edieval [A]dventure multi[U]ser [G]ame | *
* -----------------------------------------------------------| \\._.// *
* SmaugWiz (C) 1998 by Russ Pillsbury (Windows NT version) | (0...0) *
* -----------------------------------------------------------| ).:.( *
* SMAUG (C) 1994, 1995, 1996 by Derek Snider | {o o} *
* -----------------------------------------------------------| / ' ' \ *
* SMAUG code team: Thoric, Altrag, Blodkai, Narn, Haus, |~'~.VxvxV.~'~*
* Scryn, Swordbearer, Rennard, Tricops, and Gorog. | *
* ------------------------------------------------------------------------ *
* Merc 2.1 Diku Mud improvments copyright (C) 1992, 1993 by Michael *
* Chastain, Michael Quan, and Mitchell Tse. *
* Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer, *
* Michael Seifert, Hans Henrik Staerfeldt, Tom Madsen, and Katja Nyboe. *
****************************************************************************/
// Crypt.cpp
#include "stdafx.h"
//#include <wincrypt.h>
#include "smaug.h"
#include "SmaugWizDoc.h"
//#define NOCRYPT
CString Crypt (const char* pwd, const char* name)
{
#ifdef NOCRYPT
return pwd;
#endif
#ifdef XXXX
HCRYPTPROV hProv = 0;
HCRYPTKEY hKey = 0;
HCRYPTHASH hHash = 0;
DWORD Length;
CString Msg;
// Get handle to user default provider.
if (! CryptAcquireContext (&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
Msg.Format ("Error %x during CryptAcquireContext!\n", GetLastError ());
goto done;
}
// Create hash object.
if (! CryptCreateHash (hProv, CALG_MD5, 0, 0, &hHash)) {
Msg.Format ("Error %x during CryptCreateHash!\n", GetLastError ());
goto done;
}
// Hash name string.
Length = strlen (name);
if (! CryptHashData (hHash, (BYTE*) name, Length, 0)) {
Msg.Format ("Error %x during CryptHashData!\n", GetLastError ());
goto done;
}
// Create block cipher session key based on hash of the name.
if (! CryptDeriveKey (hProv, CALG_RC4, hHash, CRYPT_EXPORTABLE, &hKey)) {
Msg.Format ("Error %x during CryptDeriveKey!\n", GetLastError ());
goto done;
}
// Use 'hKey' to do something.
// Now encrypt the password
char buf [32];
strcpy (buf, pwd);
Length = strlen (pwd);
if (! CryptEncrypt (hKey, hHash, TRUE, 0, (BYTE*)buf, &Length, sizeof (buf))) {
Msg.Format ("Error %x during CryptEncrypt!\n", GetLastError ());
goto done;
}
done:
// Destroy hash object.
if (hHash) CryptDestroyHash (hHash);
// Destroy session key.
if (hKey) CryptDestroyKey (hKey);
// Release provider handle.
if (hProv) CryptReleaseContext (hProv, 0);
if (Msg.IsEmpty ()) then return buf;
gpDoc->LogString (Msg);
return pwd;
#endif
// Ah hell, I can't make the above work. How about for now a very simple
// encryption just to make the passwords look encrypted. We'll add 12 to
// the ascii value of each even parity letter, 14 to the ascii value of each
// odd parity letter, and wrap to 32+remainder if over 126! Since it doesn't
// matter if the passwords can be decrypted or not, we'll also 'round up'
// any result characters between ' ' and '0', by doubling them.
char buf [64];
CString s = name;
s += pwd;
memset (buf, 0, sizeof (buf));
for (int i=0; i < s.GetLength (); ++i) {
UINT c = s.GetAt (i);
c += (c % 2) ? 12 : 14;
if (c > 122) then
c = 32 + (c-122);
if (c < 48) then c *= 2;
buf [i] = c;
}
return buf;
}