Security.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "Common.h"
  5. #include "Security.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. //---------------------------------------------------------------------------
  9. #define PWALG_SIMPLE_INTERNAL 0x00
  10. #define PWALG_SIMPLE_EXTERNAL 0x01
  11. //---------------------------------------------------------------------------
  12. AnsiString SimpleEncryptChar(unsigned char Ch)
  13. {
  14. Ch = (unsigned char)((~Ch) ^ PWALG_SIMPLE_MAGIC);
  15. return
  16. PWALG_SIMPLE_STRING.SubString(((Ch & 0xF0) >> 4) + 1, 1) +
  17. PWALG_SIMPLE_STRING.SubString(((Ch & 0x0F) >> 0) + 1, 1);
  18. }
  19. //---------------------------------------------------------------------------
  20. unsigned char SimpleDecryptNextChar(AnsiString &Str)
  21. {
  22. if (Str.Length() > 0)
  23. {
  24. unsigned char Result = (unsigned char)
  25. ~((((PWALG_SIMPLE_STRING.Pos(Str.c_str()[0])-1) << 4) +
  26. ((PWALG_SIMPLE_STRING.Pos(Str.c_str()[1])-1) << 0)) ^ PWALG_SIMPLE_MAGIC);
  27. Str.Delete(1, 2);
  28. return Result;
  29. }
  30. else return 0x00;
  31. }
  32. //---------------------------------------------------------------------------
  33. AnsiString EncryptPassword(AnsiString Password, AnsiString Key, Integer /* Algorithm */)
  34. {
  35. AnsiString Result("");
  36. int Shift, Index;
  37. if (!RandSeed) Randomize();
  38. Password = Key + Password;
  39. Shift = (Password.Length() < PWALG_SIMPLE_MAXLEN) ?
  40. (unsigned char)random(PWALG_SIMPLE_MAXLEN - Password.Length()) : 0;
  41. Result += SimpleEncryptChar((Char)PWALG_SIMPLE_FLAG); // Flag
  42. Result += SimpleEncryptChar((Char)PWALG_SIMPLE_INTERNAL); // Dummy
  43. Result += SimpleEncryptChar((Char)Password.Length());
  44. Result += SimpleEncryptChar((Char)Shift);
  45. for (Index = 0; Index < Shift; Index++)
  46. Result += SimpleEncryptChar((unsigned char)random(256));
  47. for (Index = 0; Index < Password.Length(); Index++)
  48. Result += SimpleEncryptChar(Password.c_str()[Index]);
  49. while (Result.Length() < PWALG_SIMPLE_MAXLEN * 2)
  50. Result += SimpleEncryptChar((unsigned char)random(256));
  51. return Result;
  52. }
  53. //---------------------------------------------------------------------------
  54. AnsiString DecryptPassword(AnsiString Password, AnsiString Key, Integer /* Algorithm */)
  55. {
  56. AnsiString Result("");
  57. Integer Index;
  58. unsigned char Length, Flag;
  59. Flag = SimpleDecryptNextChar(Password);
  60. if (Flag == PWALG_SIMPLE_FLAG)
  61. {
  62. /* Dummy = */ SimpleDecryptNextChar(Password);
  63. Length = SimpleDecryptNextChar(Password);
  64. }
  65. else Length = Flag;
  66. Password.Delete(1, ((Integer)SimpleDecryptNextChar(Password))*2);
  67. for (Index = 0; Index < Length; Index++)
  68. Result += (char)SimpleDecryptNextChar(Password);
  69. if (Flag == PWALG_SIMPLE_FLAG)
  70. {
  71. if (Result.SubString(1, Key.Length()) != Key) Result = "";
  72. else Result.Delete(1, Key.Length());
  73. }
  74. return Result;
  75. }
  76. //---------------------------------------------------------------------------
  77. AnsiString SetExternalEncryptedPassword(AnsiString Password)
  78. {
  79. AnsiString Result;
  80. Result += SimpleEncryptChar((Char)PWALG_SIMPLE_FLAG);
  81. Result += SimpleEncryptChar((Char)PWALG_SIMPLE_EXTERNAL);
  82. Result += StrToHex(Password);
  83. return Result;
  84. }
  85. //---------------------------------------------------------------------------
  86. bool GetExternalEncryptedPassword(AnsiString Encrypted, AnsiString & Password)
  87. {
  88. bool Result =
  89. (SimpleDecryptNextChar(Encrypted) == PWALG_SIMPLE_FLAG) &&
  90. (SimpleDecryptNextChar(Encrypted) == PWALG_SIMPLE_EXTERNAL);
  91. if (Result)
  92. {
  93. Password = HexToStr(Encrypted);
  94. }
  95. return Result;
  96. }
  97. //---------------------------------------------------------------------------