Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from unicodedata import normalize
- import re
- import sys
- import codecs
- def decode(encoding, input_file, output_file):
- with open(input_file, 'r', encoding=encoding) as f:
- lines = [line for line in f.read().splitlines()]
- str_b = ''
- mes_dec = bytearray()
- for i in range(len(lines)):
- space_l = lines[i][-1]
- if space_l == ' ':
- str_b += '1'
- else:
- str_b += '0'
- if len(str_b) == 8:
- byte = int(str_b, 2)
- if byte == 0:
- break
- else:
- mes_dec.append(byte)
- str_b = ''
- # print (mes_dec.decode(encoding))
- with open(output_file, 'w', encoding=encoding) as f:
- f.write(mes_dec.decode(sys.stdin.encoding or locale.getpreferredencoding(True)))
- def encode(encoding, f_container, hidden_mes, output_file):
- with open(f_container, 'r', encoding=encoding) as f:
- # lines = f.read().replace(' ', '%')
- # lines = list(lines.split('\n'))
- lines = f.readlines()
- bits_count = 0
- # lines[bits_count] = lines[bits_count].rstrip()
- bitline =''
- # hidden_mes = str(hidden_mes)
- bytes_sec_mes = bytes(hidden_mes, encoding)
- for byte in bytes_sec_mes:
- for bit in bin(byte)[2:].zfill(8):
- if bit == '1':
- lines[bits_count] = lines[bits_count][:-1] + ' \n'
- else:
- lines[bits_count] = lines[bits_count][:-1] + '\n'
- bits_count += 1
- bitline = bitline+bit
- print (lines[bits_count])
- print (bits_count)
- print (bitline)
- with open(output_file, 'w', encoding=encoding) as f:
- for line in lines:
- f.write(line)
- def main():
- f_hidden_mes = codecs.open('hidden_mes.txt')
- hidden_mes = f_hidden_mes.read().encode().decode(sys.stdin.encoding or locale.getpreferredencoding(True))
- print ('1 - кодирование\n2 - декодирование')
- mode = input()
- if mode == '1':
- encode('utf8', 'container.txt', hidden_mes, 'enc.txt')
- elif mode == '2':
- decode('utf8', 'enc.txt', 'dec.txt')
- else:
- print('err')
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment