Key.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 )
  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. // Inhalt der Schlüssel Klasse aus Schlüssel.h
  113. // Konstruktor
  114. Key::Key()
  115. : ReferenceCounter(),
  116. key( 0 ),
  117. length( 0 ),
  118. pos( 0 )
  119. {}
  120. Key::Key( const char *s, int len )
  121. : ReferenceCounter(),
  122. key( new unsigned char[ len ] ),
  123. length( len ),
  124. pos( 0 )
  125. {
  126. for( int i = 0; i < len; ++i )
  127. key[ i ] = s[ i ];
  128. }
  129. // Destruktor
  130. Key::~Key()
  131. {
  132. delete[] key;
  133. }
  134. // nicht constant
  135. void Key::setPos( __int64 p )
  136. {
  137. if( p < 0 )
  138. p = 0;
  139. pos = (int)( p % length );
  140. }
  141. void Key::setKey( const char *s, int len )
  142. {
  143. delete[] key;
  144. key = new unsigned char[ len ];
  145. for( int i = 0; i < len; ++i )
  146. key[ i ] = s[ i ];
  147. pos = 0;
  148. this->length = len;
  149. }
  150. void Key::codieren( Bytes *daten )
  151. {
  152. if( !key || !length )
  153. {
  154. daten->release();
  155. return;
  156. }
  157. int dLen = daten->getLength();
  158. char *bytes = daten->getBytes();
  159. char *bEnd = bytes + dLen;
  160. for( char *c = bytes; c < bEnd; ++c )
  161. {
  162. *c = (char)( *c + key[ pos ] );
  163. ++pos;
  164. if( pos >= length )
  165. pos = 0;
  166. }
  167. daten->release();
  168. }
  169. void Key::decodieren( Bytes *daten )
  170. {
  171. if( !key || !length )
  172. {
  173. daten->release();
  174. return;
  175. }
  176. int dLen = daten->getLength();
  177. char *bytes = daten->getBytes();
  178. char *bEnd = bytes + dLen;
  179. for( char *c = bytes; c < bEnd; ++c )
  180. {
  181. *c = (char)( *c - key[ pos ] );
  182. ++pos;
  183. if( pos >= length )
  184. pos = 0;
  185. }
  186. daten->release();
  187. }