fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class self_synchronizing_scrambler
  5. {
  6. unsigned char key;
  7. unsigned char shift_register;
  8.  
  9. bool pickbit(const unsigned char val, const short index)
  10. {
  11. return ((val >> index)%2);
  12. }
  13.  
  14. public:
  15. self_synchronizing_scrambler() {}
  16.  
  17. self_synchronizing_scrambler(unsigned char key, unsigned char shift_register)
  18. {
  19. this->key = key;
  20. this->shift_register = shift_register;
  21. }
  22.  
  23. void assign_key(unsigned char key) {this->key = key;}
  24. void assign_register(unsigned char shift_register) {this->shift_register = shift_register;}
  25.  
  26. unsigned char key_value() {return key;}
  27. unsigned char register_value() {return shift_register;}
  28.  
  29. void scramble(unsigned char &letter)
  30. {
  31. unsigned char output = 0;
  32. for (unsigned char i = 0; i < 8; ++i)
  33. {
  34. bool newbit = pickbit(letter, 7-i);
  35. for (unsigned char j = 0; j < 8; ++j)
  36. {
  37. newbit ^= pickbit(this->key, 7-j) & pickbit(this->shift_register, 7-j);
  38. }
  39. output ^= (newbit << (7-i));
  40.  
  41. key = (key >> 1) + (newbit << 7);
  42. }
  43. letter = output;
  44. }
  45.  
  46. void descramble(unsigned char &letter)
  47. {
  48. unsigned char output = 0;
  49. for(unsigned char i = 0; i < 8; ++i)
  50. {
  51. bool oldbit = pickbit(letter, 7-i), scrambit = 0;
  52. for(unsigned char j = 0; j < 8; ++j)
  53. {
  54. scrambit ^= pickbit(this->key, 7-j) & pickbit(this->shift_register, 7-j);
  55. }
  56. output ^= ((scrambit^oldbit) << (7-i));
  57.  
  58. key = (key >> 1) + (oldbit << 7);
  59. }
  60. letter = output;
  61. }
  62. };
  63.  
  64. int main()
  65. {
  66. self_synchronizing_scrambler A(5,17);
  67. unsigned short scrambled;
  68. unsigned char output;
  69.  
  70. while (cin >> hex >> scrambled)
  71. {
  72. output = scrambled;
  73. A.descramble(output);
  74. cout << output;
  75. }
  76.  
  77. return 0;
  78. }
Success #stdin #stdout 0s 4264KB
stdin
ec 49 a0 c9 72 75 43 13 55 66 28 80 e7 ed d2 75 b7 bf 69 93 c7 df 4e d0 be 3f b1 de 5c f6 ea 6c 94 f5 8d 1f 86 80 aa 74 5e c7 9e 17 2 47 41 76 7c d4 a1 
stdout
Base, base, it’s cheeseburger 1. Can you hear me?