Schluessel.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "Schluessel.h"
  2. using namespace Framework::Verschlüsselung;
  3. // Inhalt der Bytes Klasse aus Schlüssel.h
  4. // Konstruktor
  5. Bytes::Bytes()
  6. : bytes( 0 ),
  7. del( 1 ),
  8. län( 0 ),
  9. ref( 1 )
  10. {}
  11. Bytes::Bytes( int län )
  12. : bytes( new char[ län ] ),
  13. del( 1 ),
  14. län( län ),
  15. ref( 1 )
  16. {}
  17. Bytes::Bytes( const char *daten, int län )
  18. : bytes( new char[ län ] ),
  19. del( 1 ),
  20. län( län ),
  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 + län;
  37. for( char *c = bytes; c < end; c++, ++daten )
  38. *c = *daten;
  39. }
  40. void Bytes::setBytes( const char *daten, int län )
  41. {
  42. if( !daten || !län )
  43. return;
  44. if( del )
  45. delete[] bytes;
  46. del = 1;
  47. bytes = new char[ län ];
  48. this->län = län;
  49. setBytes( daten );
  50. }
  51. void Bytes::setBytesZ( char *daten, int län )
  52. {
  53. if( del )
  54. delete[] bytes;
  55. del = 0;
  56. bytes = daten;
  57. this->län = län;
  58. }
  59. void Bytes::füll( const char c )
  60. {
  61. if( !bytes )
  62. return;
  63. char *end = bytes + län;
  64. for( char *b = bytes; b < end; ++b )
  65. *b = c;
  66. }
  67. void Bytes::füll( const char c, int län )
  68. {
  69. if( !bytes )
  70. bytes = new char[ län ];
  71. län = län > this->län ? this->län : län;
  72. char *end = bytes + län;
  73. for( char *b = bytes; b < end; ++b )
  74. *b = c;
  75. }
  76. void Bytes::füll( const char c, int beg, int end )
  77. {
  78. if( beg >= län )
  79. return;
  80. if( !bytes )
  81. bytes = new char[ end - beg ];
  82. end = end > this->län ? this->län : end;
  83. char *endB = bytes + end;
  84. for( char *b = bytes + beg; b < endB; ++b )
  85. *b = c;
  86. }
  87. void Bytes::füll( const char *c, int cLän )
  88. {
  89. if( !c || !cLän || !bytes )
  90. return;
  91. char *endB = bytes + län;
  92. const char *endC = c + cLän;
  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 >= län )
  100. return;
  101. bytes[ pos ] = c;
  102. }
  103. // constant
  104. int Bytes::getLänge() const
  105. {
  106. return län;
  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. Schlüssel::Schlüssel()
  128. : schlüssel( 0 ),
  129. län( 0 ),
  130. pos( 0 ),
  131. ref( 1 )
  132. {}
  133. Schlüssel::Schlüssel( const char *s, int län )
  134. : schlüssel( new unsigned char[ län ] ),
  135. län( län ),
  136. pos( 0 ),
  137. ref( 1 )
  138. {
  139. for( int i = 0; i < län; ++i )
  140. schlüssel[ i ] = s[ i ];
  141. }
  142. // Destruktor
  143. Schlüssel::~Schlüssel()
  144. {
  145. delete[] schlüssel;
  146. }
  147. // nicht constant
  148. void Schlüssel::setPos( int p )
  149. {
  150. if( p < 0 )
  151. p = 0;
  152. pos = p > län ? 0 : p;
  153. }
  154. void Schlüssel::setSchlüssel( const char *s, int län )
  155. {
  156. delete[] schlüssel;
  157. schlüssel = new unsigned char[ län ];
  158. for( int i = 0; i < län; ++i )
  159. schlüssel[ i ] = s[ i ];
  160. pos = 0;
  161. this->län = län;
  162. }
  163. void Schlüssel::codieren( Bytes *daten )
  164. {
  165. if( !schlüssel || !län )
  166. {
  167. daten->release();
  168. return;
  169. }
  170. int dLän = daten->getLänge();
  171. char *bytes = daten->getBytes();
  172. char *bEnd = bytes + dLän;
  173. for( char *c = bytes; c < bEnd; ++c )
  174. {
  175. *c = *c + schlüssel[ pos ];
  176. ++pos;
  177. if( pos >= län )
  178. pos = 0;
  179. }
  180. daten->release();
  181. }
  182. void Schlüssel::decodieren( Bytes *daten )
  183. {
  184. if( !schlüssel || !län )
  185. {
  186. daten->release();
  187. return;
  188. }
  189. int dLän = daten->getLänge();
  190. char *bytes = daten->getBytes();
  191. char *bEnd = bytes + dLän;
  192. for( char *c = bytes; c < bEnd; ++c )
  193. {
  194. *c = *c - schlüssel[ pos ];
  195. ++pos;
  196. if( pos >= län )
  197. pos = 0;
  198. }
  199. daten->release();
  200. }
  201. // Reference Counting
  202. Schlüssel *Schlüssel::getThis()
  203. {
  204. ++ref;
  205. return this;
  206. }
  207. Schlüssel *Schlüssel::release()
  208. {
  209. --ref;
  210. if( !ref )
  211. delete this;
  212. return 0;
  213. }