Schluessel.cpp 3.7 KB

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