Security.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Security.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. //---------------------------------------------------------------------------
  8. AnsiString SimpleEncryptChar(unsigned char Ch)
  9. {
  10. Ch = (unsigned char)((~Ch) ^ PWALG_SIMPLE_MAGIC);
  11. return
  12. PWALG_SIMPLE_STRING.SubString(((Ch & 0xF0) >> 4) + 1, 1) +
  13. PWALG_SIMPLE_STRING.SubString(((Ch & 0x0F) >> 0) + 1, 1);
  14. }
  15. //---------------------------------------------------------------------------
  16. unsigned char SimpleDecryptNextChar(AnsiString &Str)
  17. {
  18. if (Str.Length() > 0)
  19. {
  20. unsigned char Result = (unsigned char)
  21. ~((((PWALG_SIMPLE_STRING.Pos(Str.c_str()[0])-1) << 4) +
  22. ((PWALG_SIMPLE_STRING.Pos(Str.c_str()[1])-1) << 0)) ^ PWALG_SIMPLE_MAGIC);
  23. Str.Delete(1, 2);
  24. return Result;
  25. }
  26. else return 0x00;
  27. }
  28. //---------------------------------------------------------------------------
  29. AnsiString EncryptPassword(AnsiString Password, AnsiString Key, Integer /* Algorithm */)
  30. {
  31. AnsiString Result("");
  32. int Shift, Index;
  33. if (!RandSeed) Randomize();
  34. Password = Key + Password;
  35. Shift = (Password.Length() < PWALG_SIMPLE_MAXLEN) ?
  36. (unsigned char)random(PWALG_SIMPLE_MAXLEN - Password.Length()) : 0;
  37. Result += SimpleEncryptChar((Char)PWALG_SIMPLE_FLAG); // Flag
  38. Result += SimpleEncryptChar((Char)0); // Dummy
  39. Result += SimpleEncryptChar((Char)Password.Length());
  40. Result += SimpleEncryptChar((Char)Shift);
  41. for (Index = 0; Index < Shift; Index++)
  42. Result += SimpleEncryptChar((unsigned char)random(256));
  43. for (Index = 0; Index < Password.Length(); Index++)
  44. Result += SimpleEncryptChar(Password.c_str()[Index]);
  45. while (Result.Length() < PWALG_SIMPLE_MAXLEN * 2)
  46. Result += SimpleEncryptChar((unsigned char)random(256));
  47. return Result;
  48. }
  49. //---------------------------------------------------------------------------
  50. AnsiString DecryptPassword(AnsiString Password, AnsiString Key, Integer /* Algorithm */)
  51. {
  52. AnsiString Result("");
  53. Integer Index;
  54. unsigned char Length, Flag;
  55. Flag = SimpleDecryptNextChar(Password);
  56. if (Flag == PWALG_SIMPLE_FLAG)
  57. {
  58. /* Dummy = */ SimpleDecryptNextChar(Password);
  59. Length = SimpleDecryptNextChar(Password);
  60. }
  61. else Length = Flag;
  62. Password.Delete(1, ((Integer)SimpleDecryptNextChar(Password))*2);
  63. for (Index = 0; Index < Length; Index++)
  64. Result += (char)SimpleDecryptNextChar(Password);
  65. if (Flag == PWALG_SIMPLE_FLAG)
  66. {
  67. if (Result.SubString(1, Key.Length()) != Key) Result = "";
  68. else Result.Delete(1, Key.Length());
  69. }
  70. return Result;
  71. }
  72. //---------------------------------------------------------------------------