Guest User

Untitled

a guest
Dec 10th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from unicodedata import normalize
  3.  
  4. import re
  5. import sys
  6. import codecs
  7. def decode(encoding, input_file, output_file):
  8. with open(input_file, 'r', encoding=encoding) as f:
  9. lines = [line for line in f.read().splitlines()]
  10.  
  11. str_b = ''
  12. mes_dec = bytearray()
  13.  
  14. for i in range(len(lines)):
  15. space_l = lines[i][-1]
  16.  
  17. if space_l == ' ':
  18. str_b += '1'
  19. else:
  20. str_b += '0'
  21.  
  22. if len(str_b) == 8:
  23. byte = int(str_b, 2)
  24. if byte == 0:
  25. break
  26. else:
  27. mes_dec.append(byte)
  28. str_b = ''
  29. # print (mes_dec.decode(encoding))
  30.  
  31. with open(output_file, 'w', encoding=encoding) as f:
  32. f.write(mes_dec.decode(sys.stdin.encoding or locale.getpreferredencoding(True)))
  33.  
  34.  
  35. def encode(encoding, f_container, hidden_mes, output_file):
  36. with open(f_container, 'r', encoding=encoding) as f:
  37. # lines = f.read().replace(' ', '%')
  38.  
  39. # lines = list(lines.split('\n'))
  40.  
  41. lines = f.readlines()
  42.  
  43. bits_count = 0
  44. # lines[bits_count] = lines[bits_count].rstrip()
  45. bitline =''
  46. # hidden_mes = str(hidden_mes)
  47. bytes_sec_mes = bytes(hidden_mes, encoding)
  48. for byte in bytes_sec_mes:
  49.  
  50.  
  51. for bit in bin(byte)[2:].zfill(8):
  52.  
  53. if bit == '1':
  54.  
  55. lines[bits_count] = lines[bits_count][:-1] + ' \n'
  56.  
  57. else:
  58. lines[bits_count] = lines[bits_count][:-1] + '\n'
  59. bits_count += 1
  60. bitline = bitline+bit
  61. print (lines[bits_count])
  62. print (bits_count)
  63. print (bitline)
  64. with open(output_file, 'w', encoding=encoding) as f:
  65. for line in lines:
  66. f.write(line)
  67.  
  68. def main():
  69. f_hidden_mes = codecs.open('hidden_mes.txt')
  70. hidden_mes = f_hidden_mes.read().encode().decode(sys.stdin.encoding or locale.getpreferredencoding(True))
  71.  
  72. print ('1 - кодирование\n2 - декодирование')
  73. mode = input()
  74. if mode == '1':
  75. encode('utf8', 'container.txt', hidden_mes, 'enc.txt')
  76. elif mode == '2':
  77. decode('utf8', 'enc.txt', 'dec.txt')
  78. else:
  79. print('err')
  80.  
  81. if __name__ == "__main__":
  82. main()
Add Comment
Please, Sign In to add comment