encrypt.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Encrypt.cpp //////////////////////////////////////////////////////
  24. // Ancient Westwood Online password encryption (obfuscation?) code
  25. // Author: Anonymous
  26. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "Common/encrypt.h"
  30. #define MAX_CHARS 65
  31. static char Base_String[MAX_CHARS] =
  32. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
  33. static char Return_Buffer[MAX_ENCRYPTED_STRING + 1];
  34. static char Temp_Buffer [MAX_ENCRYPTED_STRING + 1];
  35. /*******************************************************/
  36. /* This function is a simple one-way encryption that */
  37. /* is portable across many platforms */
  38. /* */
  39. /* The 'String' to encrypt MUST be up to 8 chars long */
  40. /* and should be no shorter than 4 characters. */
  41. /* It can contain letters and numbers and '.' and '/' */
  42. /*******************************************************/
  43. /* String is the original string to encrypt */
  44. /* Seed is the string to encrypt */
  45. //char *encrypt(char *String, char *Seed)
  46. const char *EncryptString(const char *String)
  47. {
  48. /* We need a 56 bit key, so use two 32 bit values */
  49. /* and we'll strip off the high order 8 bits */
  50. //unsigned long Random_Seed_Value_high = 0; /* 32 bit seed value */
  51. //unsigned long Random_Seed_Value_low = 0; /* 32 bit seed value */
  52. //unsigned long Temp_high = 0; /* 32 bit storage value */
  53. //unsigned long Temp_low = 0; /* 32 bit storage value */
  54. unsigned int UpCnt = 0, DnCnt = 0, Cnt = 0;
  55. unsigned int Length = strlen(String);
  56. /* Ok, here is the algorithm: */
  57. /* */
  58. if (Length > MAX_ENCRYPTED_STRING)
  59. Length = MAX_ENCRYPTED_STRING;
  60. for (UpCnt = 0, DnCnt = Length; UpCnt < Length; UpCnt++, DnCnt--)
  61. if (String[UpCnt] & 0x01)
  62. Temp_Buffer[UpCnt] = (String[UpCnt] << (String[UpCnt] & 0x01)) &
  63. String[DnCnt];
  64. else
  65. Temp_Buffer[UpCnt] = (String[UpCnt] << (String[UpCnt] & 0x01)) ^
  66. String[DnCnt];
  67. for (Cnt = 0; Cnt < MAX_ENCRYPTED_STRING; Cnt++)
  68. Return_Buffer[Cnt] = Base_String[Temp_Buffer[Cnt] & 0x3F];
  69. Return_Buffer[Cnt] = NULL;
  70. return (Return_Buffer);
  71. }
  72. #ifdef UNIT_TEST
  73. void main(void)
  74. {
  75. char Input_String[9];
  76. char *new_string;
  77. while (1)
  78. {
  79. printf ("Enter a string to encrypt:");
  80. gets(Input_String);
  81. printf("\nString enterred was: %s", Input_String);
  82. new_string = encrypt(Input_String, "ab");
  83. printf("\nEncrypted string is: %s", new_string);
  84. }
  85. }
  86. #endif