fork download
  1. # Decode the two ciphertexts from the Instructors Box below,
  2. # or the C1, C2 variables - which are the same
  3. #
  4. # We highly recommend that you run your decoding code in the
  5. # programming language of your choice outside of the
  6. # this environment, as this system does not provide enough
  7. # computational resources to successfully decode
  8. #
  9. # After decoding the two ciphertexts,
  10. # replace the plaintext1 and plaintext2 variables below
  11. # with the decoded ciphertexts
  12.  
  13. # C1 and C2 are messages in english,
  14. # encoded using string_to_bits, with 7bit ASCII
  15. # and then XOR'd with a secret key
  16. #
  17. # In pseudo-code:
  18. # C1 = XOR(string_to_bits(plaintext1), secret_key)
  19. # C2 = XOR(string_to_bits(plaintext2), secret_key)
  20.  
  21. C1 = "1010110010011110011111101110011001101100111010001111011101101011101000110010011000000101001110111010010111100100111101001010000011000001010001001001010000000010101001000011100100010011011011011011010111010011000101010111111110010011010111001001010101110001111101010000001011110100000000010010111001111010110000001101010010110101100010011111111011101101001011111001101111101111000100100001000111101111011011001011110011000100011111100001000101111000011101110101110010010100010111101111110011011011001101110111011101100110010100010001100011001010100110001000111100011011001000010101100001110011000000001110001011101111010100101110101000100100010111011000001111001110000011111111111110010111111000011011001010010011100011100001011001101110110001011101011101111110100001111011011000110001011111111101110110101101101001011110110010111101000111011001111"
  22.  
  23. C2 = "1011110110100110000001101000010111001000110010000110110001101001111101010000101000110100111010000010011001100100111001101010001001010001000011011001010100001100111011010011111100100101000001001001011001110010010100101011111010001110010010101111110001100010100001110000110001111111001000100001001010100011100100001101010101111000100001111101111110111001000101111111101011001010000100100000001011001001010000101001110101110100001111100001011101100100011000110111110001000100010111110110111010010010011101011111111001011011001010010110100100011001010110110001001000100011011001110111010010010010110100110100000111100001111101111010011000100100110011111011001010101000100000011111010010110111001100011100001111100100110010010001111010111011110110001000111101010110101001110111001110111010011111111010100111000100111001011000111101111101100111011001111"
  24.  
  25. #####
  26. # CHANGE THESE VARIABLES
  27.  
  28. plaintext1 = "decoded message"
  29. plaintext2 = "the other decoded message"
  30.  
  31. # END
  32. #############
  33.  
  34. #############
  35. # Below is some code that might be useful
  36. #
  37.  
  38. BITS = ('0', '1')
  39. ASCII_BITS = 7
  40.  
  41. def display_bits(b):
  42. """converts list of {0, 1}* to string"""
  43. return ''.join([BITS[e] for e in b])
  44.  
  45. def seq_to_bits(seq):
  46. return [0 if b == '0' else 1 for b in seq]
  47.  
  48. def pad_bits(bits, pad):
  49. """pads seq with leading 0s up to length pad"""
  50. assert len(bits) <= pad
  51. return [0] * (pad - len(bits)) + bits
  52.  
  53. def convert_to_bits(n):
  54. """converts an integer `n` to bit array"""
  55. result = []
  56. if n == 0:
  57. return [0]
  58. while n > 0:
  59. result = [(n % 2)] + result
  60. n = n / 2
  61. return result
  62.  
  63. def string_to_bits(s):
  64. def chr_to_bit(c):
  65. return pad_bits(convert_to_bits(ord(c)), ASCII_BITS)
  66. return [b for group in
  67. map(chr_to_bit, s)
  68. for b in group]
  69.  
  70. def bits_to_char(b):
  71. assert len(b) == ASCII_BITS
  72. value = 0
  73. for e in b:
  74. value = (value * 2) + e
  75. return chr(value)
  76.  
  77. def list_to_string(p):
  78. return ''.join(p)
  79.  
  80. def bits_to_string(b):
  81. return ''.join([bits_to_char(b[i:i + ASCII_BITS])
  82. for i in range(0, len(b), ASCII_BITS)])
  83.  
  84. b1 = string_to_bits(C1)
  85. print bits_to_string(b1)
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.5/py_compile.py", line 125, in compile
    _optimize=optimize)
  File "<frozen importlib._bootstrap_external>", line 735, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "./prog.py", line 85
    print bits_to_string(b1)
                       ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.5/py_compile.py", line 129, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 85
    print bits_to_string(b1)
                       ^
SyntaxError: invalid syntax

stdout
Standard output is empty