Key.cpp 4.0 KB

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