Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

mangler.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // MANGLER.CPP
  2. //
  3. // (C) 1984-2009 MicroNeil Research Corporation
  4. // Derived from Version 1 of Mangler Encryption Algorythm, 1984.
  5. // Derived from Version 2 of Mangler Encryption Algorythm, 1998.
  6. //
  7. // 20021008 _M
  8. // Found and corrected range bug in ChaosDriver(void) where
  9. // ~Position might access a location outside the fill. Replaced
  10. // ~Position with Position^0xff which has the intended effect.
  11. // 20020119 _M Version 3.0
  12. //
  13. // Mangler encryption engine object.
  14. // Using new optimized chaos driver for uniformity experiments.
  15. // Important in this experiment is proof of highest possible entropy.
  16. #include "CodeDweller/mangler.hpp"
  17. namespace CodeDweller {
  18. unsigned char MANGLER::ChaosDriver(void) { // Return the current
  19. return Fill[Fill[Position]^Fill[Position^0xff]]; // chaos engine output
  20. } // value.
  21. // As of version 3 the output of the chaos driver was strengthened for
  22. // cryptography and to increase the sensitivity of the output for use
  23. // as a random number generator. In version 2, the software would simply
  24. // return the fill value at the engine's current position. In the new
  25. // version two distinct fill values are involved in abstracting the
  26. // value of Position and determining the final output value and the Position
  27. // value itself is used to add complexity to the output.
  28. unsigned char MANGLER::Rotate(unsigned char i) { // Bitwise rotates i
  29. return (
  30. (i & 0x80)? // This operation is
  31. (i<<1)+1: // described without
  32. (i<<1) // using asm.
  33. );
  34. }
  35. void MANGLER::ChaosDriver(unsigned char i) { // Drives chaos engine.
  36. // First we move our mixing position in the fill buffer forward.
  37. Position=( // Move mixing position.
  38. Position+1+ // Move at least 1, then
  39. (Fill[Position]&0x0f) // maybe a few more.
  40. )%256; // But stay within the fill.
  41. // The fill position in version 2 was simply incremented. This allowed
  42. // for an attacker to predict something important about the state of
  43. // the chaos engine. The new method above uses abstraction through the
  44. // fill buffer to introduce "jitter" when setting a new position based
  45. // on data that is hidden from the outside.
  46. // Next we abstract the incoming character through the fill buffer and
  47. // use it to select fill data to rotate and swap.
  48. unsigned char Swap = ((Fill[Position]^Fill[i])+Position+i)%256;
  49. unsigned char Tmp;
  50. Tmp = Fill[Swap];
  51. Fill[Swap]=Fill[Position];
  52. Fill[Position]=Rotate(Tmp);
  53. // Whenever the Swap and Path positions are the same, the result is
  54. // that no data is swapped in the chaos field. We resolve that by
  55. // recalling the ChaosDriver. This has the added effect of increasing
  56. // the complexity and making it more difficult to predict the state
  57. // of the engine... particularly because the engine evloves to a new
  58. // state under these conditions without having exposed that change
  59. // to the outside world.
  60. if(Position==Swap) ChaosDriver(Tmp); // If we didn't swap, recurse.
  61. }
  62. // The encryption / decryption scheme works by modulating an input data
  63. // stream with a chaotic system and allowing the encrypted stream to drive
  64. // the chaotic system of both the transmitter and receiver. This will
  65. // synchronize the two chaotic systems and allow the receiving system to
  66. // "predict" the state of the transmiting system so that it can properly
  67. // demodulate the encrypted stream. Both chaotic systems must start in the
  68. // same state with the same fill data characteristics or else the two
  69. // chaotic systems evolve to further divergent states.
  70. unsigned char MANGLER::Encrypt(unsigned char i) {
  71. unsigned char g = ChaosDriver() ^ i; // Take the output of the
  72. ChaosDriver(g); // chaos engine and use it
  73. return g; // to moduleate the input.
  74. } // Then drive the engine
  75. // with the encrypted data.
  76. unsigned char MANGLER::Decrypt(unsigned char i) {
  77. unsigned char g = ChaosDriver() ^ i; // Take the output of the
  78. ChaosDriver(i); // chaos engine and use it
  79. return g; // to demodulate the input.
  80. } // then drive the engine
  81. // with the original input.
  82. MANGLER::MANGLER(void) : Position(0) { // The default constructor sets
  83. for(unsigned int c = 0;c<256;c++) // the key to the root primary
  84. Fill[c]=(unsigned char) c; // value and Position to 0.
  85. }
  86. }