encrypt.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. #include <stdio.h>
  19. #include <string.h>
  20. #include "encrypt.h"
  21. #define MAX_CHARS 65
  22. static char Base_String[MAX_CHARS] =
  23. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
  24. static char Return_Buffer[MAX_ENCRYPTED_STRING + 1];
  25. static char Temp_Buffer [MAX_ENCRYPTED_STRING + 1];
  26. /*******************************************************/
  27. /* This function is a simple one-way encryption that */
  28. /* is portable across many platforms */
  29. /* */
  30. /* The 'String' to encrypt MUST be up to 8 chars long */
  31. /* and should be no shorter than 4 characters. */
  32. /* It can contain letters and numbers and '.' and '/' */
  33. /*******************************************************/
  34. /* String is the original string to encrypt */
  35. /* Seed is the string to encrypt */
  36. //char *encrypt(char *String, char *Seed)
  37. char *do_encrypt(char *String)
  38. {
  39. /* We need a 56 bit key, so use two 32 bit values */
  40. /* and we'll strip off the high order 8 bits */
  41. //unsigned long Random_Seed_Value_high = 0; /* 32 bit seed value */
  42. //unsigned long Random_Seed_Value_low = 0; /* 32 bit seed value */
  43. //unsigned long Temp_high = 0; /* 32 bit storage value */
  44. //unsigned long Temp_low = 0; /* 32 bit storage value */
  45. unsigned int UpCnt = 0, DnCnt = 0, Cnt = 0;
  46. unsigned int Length = strlen(String);
  47. /* Ok, here is the algorithm: */
  48. /* */
  49. if (Length > MAX_ENCRYPTED_STRING)
  50. Length = MAX_ENCRYPTED_STRING;
  51. for (UpCnt = 0, DnCnt = Length; UpCnt < Length; UpCnt++, DnCnt--)
  52. if (String[UpCnt] & 0x01)
  53. Temp_Buffer[UpCnt] = (String[UpCnt] << (String[UpCnt] & 0x01)) &
  54. String[DnCnt];
  55. else
  56. Temp_Buffer[UpCnt] = (String[UpCnt] << (String[UpCnt] & 0x01)) ^
  57. String[DnCnt];
  58. for (Cnt = 0; Cnt < MAX_ENCRYPTED_STRING; Cnt++)
  59. Return_Buffer[Cnt] = Base_String[Temp_Buffer[Cnt] & 0x3F];
  60. Return_Buffer[Cnt] = NULL;
  61. return (Return_Buffer);
  62. }
  63. #ifdef UNIT_TEST
  64. void main(void)
  65. {
  66. char Input_String[9];
  67. char *new_string;
  68. while (1)
  69. {
  70. printf ("Enter a string to encrypt:");
  71. gets(Input_String);
  72. printf("\nString enterred was: %s", Input_String);
  73. new_string = encrypt(Input_String, "ab");
  74. printf("\nEncrypted string is: %s", new_string);
  75. }
  76. }
  77. #endif