Key.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "Key.h"
  2. using namespace Framework::Encryption;
  3. // Inhalt der Bytes Klasse aus Schlüssel.h
  4. // Konstruktor
  5. Bytes::Bytes()
  6. : ReferenceCounter(),
  7. bytes(0),
  8. del(1),
  9. length(0)
  10. {}
  11. Bytes::Bytes(int len)
  12. : ReferenceCounter(),
  13. bytes(new char[len]),
  14. del(1),
  15. length(len)
  16. {}
  17. Bytes::Bytes(const char* daten, int len)
  18. : ReferenceCounter(),
  19. bytes(new char[len]),
  20. del(1),
  21. length(len)
  22. {
  23. setBytes(daten);
  24. }
  25. // Destruktor
  26. Bytes::~Bytes()
  27. {
  28. if (del) delete[] bytes;
  29. }
  30. // nicht constant
  31. void Bytes::setBytes(const char* daten)
  32. {
  33. if (!bytes || !daten) return;
  34. char* end = bytes + length;
  35. for (char* c = bytes; c < end; c++, ++daten)
  36. *c = *daten;
  37. }
  38. void Bytes::setBytes(const char* daten, int len)
  39. {
  40. if (!daten || !len) return;
  41. if (del) delete[] bytes;
  42. del = 1;
  43. bytes = new char[len];
  44. this->length = len;
  45. setBytes(daten);
  46. }
  47. void Bytes::setBytesZ(char* daten, int len)
  48. {
  49. if (del) delete[] bytes;
  50. del = 0;
  51. bytes = daten;
  52. this->length = len;
  53. }
  54. void Bytes::fill(const char c)
  55. {
  56. if (!bytes) return;
  57. char* end = bytes + length;
  58. for (char* b = bytes; b < end; ++b)
  59. *b = c;
  60. }
  61. void Bytes::fill(const char c, int len)
  62. {
  63. if (!bytes) bytes = new char[len];
  64. len = len > this->length ? this->length : len;
  65. char* end = bytes + len;
  66. for (char* b = bytes; b < end; ++b)
  67. *b = c;
  68. }
  69. void Bytes::fill(const char c, int beg, int end)
  70. {
  71. if (beg >= length) return;
  72. if (!bytes) bytes = new char[end - beg];
  73. end = end > this->length ? this->length : end;
  74. char* endB = bytes + end;
  75. for (char* b = bytes + beg; b < endB; ++b)
  76. *b = c;
  77. }
  78. void Bytes::fill(const char* c, int cLen)
  79. {
  80. if (!c || !cLen || !bytes) return;
  81. char* endB = bytes + length;
  82. const char* endC = c + cLen;
  83. const char* d = c;
  84. for (char* b = bytes; b < endB; b++, d = d < endC - 1 ? d + 1 : c)
  85. *b = *d;
  86. }
  87. void Bytes::set(const char c, int pos)
  88. {
  89. if (!bytes || pos >= length) return;
  90. bytes[pos] = c;
  91. }
  92. // constant
  93. int Bytes::getLength() const
  94. {
  95. return length;
  96. }
  97. char* Bytes::getBytes() const
  98. {
  99. return bytes;
  100. }
  101. // Inhalt der Schlüssel Klasse aus Schlüssel.h
  102. // Konstruktor
  103. Key::Key()
  104. : ReferenceCounter(),
  105. key(0),
  106. length(0),
  107. pos(0)
  108. {}
  109. Key::Key(const char* s, int len)
  110. : ReferenceCounter(),
  111. key(new unsigned char[len]),
  112. length(len),
  113. pos(0)
  114. {
  115. for (int i = 0; i < len; ++i)
  116. key[i] = s[i];
  117. }
  118. // Destruktor
  119. Key::~Key()
  120. {
  121. delete[] key;
  122. }
  123. // nicht constant
  124. void Key::setPos(__int64 p)
  125. {
  126. if (p < 0) p = 0;
  127. pos = (int)(p % length);
  128. }
  129. void Key::setKey(const char* s, int len)
  130. {
  131. delete[] key;
  132. key = new unsigned char[len];
  133. for (int i = 0; i < len; ++i)
  134. key[i] = s[i];
  135. pos = 0;
  136. this->length = len;
  137. }
  138. void Key::codieren(Bytes* daten)
  139. {
  140. if (!key || !length)
  141. {
  142. daten->release();
  143. return;
  144. }
  145. int dLen = daten->getLength();
  146. char* bytes = daten->getBytes();
  147. char* bEnd = bytes + dLen;
  148. for (char* c = bytes; c < bEnd; ++c)
  149. {
  150. *c = (char)(*c + key[pos]);
  151. ++pos;
  152. if (pos >= length) pos = 0;
  153. }
  154. daten->release();
  155. }
  156. void Key::decodieren(Bytes* daten)
  157. {
  158. if (!key || !length)
  159. {
  160. daten->release();
  161. return;
  162. }
  163. int dLen = daten->getLength();
  164. char* bytes = daten->getBytes();
  165. char* bEnd = bytes + dLen;
  166. for (char* c = bytes; c < bEnd; ++c)
  167. {
  168. *c = (char)(*c - key[pos]);
  169. ++pos;
  170. if (pos >= length) pos = 0;
  171. }
  172. daten->release();
  173. }