Advertisement
Guest User

python 2.7.14 - encoding user input to ASCII (anonymised)

a guest
Jan 24th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import numpy
  2.  
  3. input_string = raw_input("")
  4. grouped_characters = []
  5. compound_list_of_bytes = []
  6. list_of_datachunks = []
  7. list_of_decimals = []
  8. compound_list_of_decomps = []
  9. final_list_of_characters = []
  10.  
  11. '''[redacted for privacy], 24/01/18
  12. this was written with the express intent of being as verbose as possible. as much as possible, every step of logic is laid out as methodically and descriptively as possible, even at the cost of efficiency and redundant code.
  13. '''
  14.  
  15. while input_string:
  16. grouped_characters.append(input_string[:4])
  17. input_string = input_string[4:]
  18. '''splits input string into list of 4-character items
  19. '''
  20. pad_string = grouped_characters[-1]
  21. grouped_characters[-1] = '{0: <4}'.format(pad_string)
  22. '''ads trailing spaces to pad out the final 4-character string if it is too short
  23. '''
  24.  
  25. first_loop_length = len(grouped_characters)
  26.  
  27. for i in range(-1, first_loop_length -1):
  28. i += 1
  29. temp_string = grouped_characters[i]
  30. temp_char_list = list(temp_string)
  31. for t in range(-1, 3):
  32. t += 1
  33. temp_ord = ord(temp_char_list[t])
  34. temp_binary = numpy.binary_repr(temp_ord).zfill(8)
  35. temp_char_list[t] = temp_binary
  36. compound_list_of_bytes.append(temp_char_list)
  37. '''1) ord() returns the ASCII ordinal of a given character
  38. 2) numpy.binary_repr() returns the equivalent value of an integer in binary (big-endian)
  39. 3) .zfill() pads the binary number with leading zeroes to meet the specified number of digits.
  40. '''
  41.  
  42. second_loop_length = len(compound_list_of_bytes)
  43.  
  44. for i in range(-1, second_loop_length -1):
  45. i += 1
  46. temp_4bytes = ''.join(compound_list_of_bytes[i])
  47. list_of_datachunks.append(temp_4bytes)
  48. '''1) ''.join() takes a given list or sublist, and returns it as a string. the items are separated within the string by the character specified in the quote marks.
  49. '''
  50.  
  51. third_loop_length = len(list_of_datachunks)
  52.  
  53. for i in range(-1, third_loop_length -1):
  54. i += 1
  55. temp_datachunk = list_of_datachunks[i]
  56. temp_decimal = int(temp_datachunk, 2)
  57. list_of_decimals.append(temp_decimal)
  58. '''1) int(number, base) converts the given number to decimal from the specified numerical base (typically binary)
  59. '''
  60.  
  61. fourth_loop_length = len(list_of_decimals)
  62.  
  63. for i in range(-1, fourth_loop_length -1):
  64. i += 1
  65. temp_list_of_decomps = []
  66. temp_decimal = list_of_decimals[i]
  67. for t in range (-1, 3):
  68. t += 1
  69. t_inverse = 4 - t
  70. temp_decomp, temp_decimal = divmod(temp_decimal, (85 ** t_inverse))
  71. temp_decomp += 33
  72. temp_list_of_decomps.append(temp_decomp)
  73. temp_decimal += 33
  74. temp_list_of_decomps.append(temp_decimal)
  75. compound_list_of_decomps.append(temp_list_of_decomps)
  76. '''1) divmod(arg, arg2) returns both the quotient and the remainder (modulo) of performing the given division
  77. '''
  78. print(temp_list_of_decomps)
  79.  
  80. final_loop_length = len(compound_list_of_decomps)
  81.  
  82. for i in range(-1, final_loop_length -1):
  83. i += 1
  84. temp_list_of_decomps = compound_list_of_decomps[i]
  85. for t in range(-1, 4):
  86. t += 1
  87. temp_decomp = temp_list_of_decomps[t]
  88. temp_list_of_decomps[t] = chr(temp_decomp)
  89. final_list_of_characters.append(temp_list_of_decomps)
  90. '''1) chr() returns the character represented by a given ASCII ordinal number
  91. '''
  92. final_string = ''.join([str(item) for sublist in final_list_of_characters for item in sublist])
  93. '''crazy nonsense string manipulation, turns the fully encoded ASCII from a compound list into a nice, pretty, printable string
  94. '''
  95. print(final_list_of_characters)
  96. print("grouped characters ", grouped_characters)
  97. print("compound binary list ", compound_list_of_bytes)
  98. print("first data chunk pre-concat ", compound_list_of_bytes[0])
  99. print("first concatenated data chunk ", list_of_datachunks[0])
  100. print("first 32 bit decimal ", list_of_decimals[0])
  101. print("compound list of decomps ", compound_list_of_decomps)
  102. print("ASCII: ", final_string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement