123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- #include <stdlib.h>
- #include <limits.h>
- #include "RSPiX.h"
- #include "game.h"
- #include "encrypt.h"
- #define NUM_KEYS 10
- #define KEY_LENGTH 128
- #define MAX_STRING_LENGTH 256
- #define CRCPOLY 0xA001
-
- #define UPDATE_CRC(c) \
- ms_usCRC = ms_usCRCtable[(ms_usCRC ^ (c)) & 0xFF] ^ (ms_usCRC >> CHAR_BIT)
- static char *ms_aszSeed[NUM_KEYS]= { "S3D5Lf6klfdsjiureLJKHLKmnblkjshgwieourLKHLKJHSDA0432175094SJAKLFK7348LSDLKJDHFOIUhjgdskgfeiurytowelxcmnvbdfhgeoriyfdslkhgfdgyuie",
- "KlkjILKJ879IHKJ8kj67kjh7IUKJHjkh98769872vYUTui6JHG87jhg78JHg787JHGUYuytuyjg876RHCVBnvbAIUT678787igJGFHGFTy67uhHJKJHUYTjhg6HGHG76",
- "4LKJH5Euhysdiouyfjlkw45879345kjshfdgcvxJSDHFR8734hdskmfv7y8KJLSDGFAVBMZXBV789kMNADFYUYjhkgkjhdsgfowe787243KJHGDSAGJKkjgdfuye3sd7",
- "l45kjhdfuihlIUYSLKHi87ylKJHYOIUmsagKJLHgY9802734KJHLKiuoymNJHTmnb4iU7687nkmbojyTD64654HJG765KHKJhg87jKHKJ76dcbnmbDBDSJFDHJ6IHGHJ",
- "fdlkhgljslfkdfhvcxzb 902374jklhfdgoIUOYLSKFDJH98798ysdlk9870OLHL987sdxmncosidHGKJ876kjhtdg786JHG87kj76Kjhgkjhi8K8HG98kgdnb mvzms",
- "lsdhflkasjhLKJHSLKHAFD987435LRKJHL34UY5L34J64MNB7IU4Yiuoyt87sdfjshg87ahjkjhgds87JHGKJHT8kjhgdfsvbmyt4eu678JHGkjyt6jKH78k0kjt02KJ",
- "lkjhUIYkljhlUYOI6yt9876HJMNBVuyt97JKHfMvhjytrfiyIUHGKVBMNBuyi9872548JKN76kj9fsd8KHG8JKg7872732jK8JgkjhKJHGKJHgkjhg7jkhgkjhgkjhgk",
- "dsdfLKJH;LKjKJFLdJGmhpi657oU6Y76fhgf5dyfh879346nmzmbvc56erter654gvcbmzwsdfdsmxcngfvRDFrEDfg76bDewdfvdfgwqsdfdgfcrRDfhgfderfdfteP",
- "lkhfdsRE546457djh6g65l67KJHLkhmnbvxcbDF09KLJliuyfdsgxcvbFDGERsdasdfgTEdhFDtYrt8765DFRrfgfhvhghretwRF67654DFhDGFDVctTRTtrytrRyiTY",
- "28734jsdfhg873jksgUIYTIUkhgiuT7YIGVBKjg8796KHG8o76HJG876tKJHG8976GFd6584H6G67RT987kJHG65978JKHG8JHGKJHGkhjgkjhgidsytf87439649283"};
- static unsigned short ms_usCRCtable[UCHAR_MAX + 1];
- static unsigned short ms_usCRC;
- short Verify(char *szSource,short sLength,unsigned short usCRC);
- void MakeCRCTable();
- short Encrypt(char* szInputString,char* szOutputString,short sSourceLength)
- {
- short rc=0,
- sIndex=0,
- sStartIndex=GetRandom()%KEY_LENGTH,
- sCurrentKey=GetRandom()%NUM_KEYS,
- sSeedIndex=sStartIndex;
-
- szOutputString[0]=(char)sCurrentKey;
- szOutputString[1]=(char)sStartIndex;
-
-
- while(1)
- {
-
- if(sIndex==sSourceLength)
- {
- szOutputString[sIndex+2]='\0';
- break;
- }
-
- szOutputString[sIndex+2]=szInputString[sIndex]^(~ms_aszSeed[sCurrentKey][sSeedIndex]);
-
- sIndex++;
- sSeedIndex++;
-
- if(sSeedIndex==KEY_LENGTH)
- sSeedIndex=0;
- }
- sIndex=2;
-
-
- while(1)
- {
-
- if(sIndex==sSourceLength+1)
- {
- break;
- }
-
- szOutputString[sIndex]^=szOutputString[sIndex-1];
- sIndex++;
- }
- return sIndex+1;
- }
- short Decrypt(char* szInputString,char* szOutputString,short sSourceLength)
- {
- sSourceLength -= 2;
- short rc=0,
- sStartIndex=szInputString[1],
- sIndex=sSourceLength,
- sCurrentKey=szInputString[0],
- sSeedIndex=sStartIndex;
-
- if((sStartIndex<KEY_LENGTH)&&(sStartIndex>=0))
- {
- if((sCurrentKey<NUM_KEYS)&&(sCurrentKey>=0))
- {
-
- while(1)
- {
-
- if(sIndex==1)
- {
- break;
- }
-
- szInputString[sIndex]^=szInputString[sIndex-1];
- sIndex--;
- }
- sIndex=2;
-
- while(1)
- {
-
- if(sIndex==sSourceLength+2)
- {
- szOutputString[sSourceLength+2]='\0';
- break;
- }
-
- szOutputString[sIndex-2]=szInputString[sIndex]^(~ms_aszSeed[sCurrentKey][sSeedIndex]);
-
- sIndex++;
- sSeedIndex++;
-
- if(sSeedIndex==KEY_LENGTH)
- sSeedIndex=0;
- }
- }
- else
- {
- TRACE("ERROR: Invalid key.\n");
- rc=-1;
- }
- }
- else
- {
- TRACE("ERROR: Invalid start position.");
- rc=-2;
- }
- return rc;
- }
- short Encrypt(char* szFileName,char* szInputString)
- {
- short rc=-1,
- sLength=strlen(szInputString);
- RFile cnFile;
- char szEncrypted[MAX_STRING_LENGTH];
- ms_usCRC=0;
-
- if(cnFile.Open(szFileName,"wb",RFile::LittleEndian)==0)
- {
- if(Encrypt(szInputString,szEncrypted,sLength)==0)
- {
- MakeCRCTable();
-
- for (short i=0;i<sLength;i++)
- UPDATE_CRC(szEncrypted[i]);
-
- cnFile.Write((S16*)&sLength);
- cnFile.Write((S8*) szEncrypted,sLength+2);
- cnFile.Write((U16*)&ms_usCRC);
-
- rc=0;
- }
-
- cnFile.Close();
- }
- return rc;
- }
- short Decrypt(char* szFileName,char* szOutputString)
- {
- short rc=-1,
- sLength;
- RFile cnFile;
- char szDecrypted[MAX_STRING_LENGTH];
- unsigned short usCRC=0;
- if(cnFile.Open(szFileName,"rb",RFile::LittleEndian)==0)
- {
- MakeCRCTable();
-
- cnFile.Read((S16*)&sLength);
- cnFile.Read((S8*) szDecrypted,sLength+2);
- cnFile.Read((U16*)&usCRC);
-
- if(Verify(szDecrypted,sLength,usCRC)==0)
- rc=Decrypt(szDecrypted,szOutputString,sLength);
-
- cnFile.Close();
- }
- return rc;
- }
- short Verify(char *szSource,short sLength,unsigned short usCRC)
- {
- short rc=0;
-
- ms_usCRC=0;
- for (short i=0;i<sLength;i++)
- UPDATE_CRC(szSource[i]);
- if(ms_usCRC!=usCRC)
- {
- TRACE("Error: CRC failure !!! Old CRC: %hu New CRC: %hu\n");
- rc=-1;
- }
- return rc;
- }
- void MakeCRCTable()
- {
- unsigned short i, j, r;
- for (i = 0; i <= UCHAR_MAX; i++)
- {
- r = i;
- for (j = 0; j < CHAR_BIT; j++)
- if (r & 1) r = (r >> 1) ^ CRCPOLY;
- else r >>= 1;
- ms_usCRCtable[i] = r;
- }
-
- return;
- }
|