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. unsigned char Shift, Index;
  33. if (!RandSeed) Randomize();
  34. Password = Key + Password;
  35. Shift = (unsigned char)random(PWALG_SIMPLE_MAXLEN - Password.Length());
  36. Result += SimpleEncryptChar((Char)PWALG_SIMPLE_FLAG); // Flag
  37. Result += SimpleEncryptChar((Char)0); // Dummy
  38. Result += SimpleEncryptChar((Char)Password.Length());
  39. Result += SimpleEncryptChar((Char)Shift);
  40. for (Index = 0; Index < Shift; Index++)
  41. Result += SimpleEncryptChar((unsigned char)random(256));
  42. for (Index = 0; Index < Password.Length(); Index++)
  43. Result += SimpleEncryptChar(Password.c_str()[Index]);
  44. while (Result.Length() < PWALG_SIMPLE_MAXLEN * 2)
  45. Result += SimpleEncryptChar((unsigned char)random(256));
  46. return Result;
  47. }
  48. //---------------------------------------------------------------------------
  49. AnsiString DecryptPassword(AnsiString Password, AnsiString Key, Integer /* Algorithm */)
  50. {
  51. AnsiString Result("");
  52. Integer Index;
  53. unsigned char Length, Flag;
  54. Flag = SimpleDecryptNextChar(Password);
  55. if (Flag == PWALG_SIMPLE_FLAG)
  56. {
  57. /* Dummy = */ SimpleDecryptNextChar(Password);
  58. Length = SimpleDecryptNextChar(Password);
  59. }
  60. else Length = Flag;
  61. Password.Delete(1, ((Integer)SimpleDecryptNextChar(Password))*2);
  62. for (Index = 0; Index < Length; Index++)
  63. Result += (char)SimpleDecryptNextChar(Password);
  64. if (Flag == PWALG_SIMPLE_FLAG)
  65. {
  66. if (Result.SubString(1, Key.Length()) != Key) Result = "";
  67. else Result.Delete(1, Key.Length());
  68. }
  69. return Result;
  70. }
  71. //---------------------------------------------------------------------------