khbr

hideMessage_textstego4.py

Nov 19th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. #-*- coding: windows-1251 -*-
  2. # Todo: пока работает только с маленькими o
  3.  
  4. import sys
  5. import os
  6.  
  7. def to_binary_string(s):
  8.     binary_s = ''
  9.     for c in s:
  10.         binary_s += '{:08b}'.format(ord(c))
  11.     return binary_s
  12.  
  13. def main():
  14.     if len(sys.argv) != 4:
  15.         print 'Usage: python %s <input-file> <message> <output-file>' % sys.argv[0]
  16.         exit(1)
  17.        
  18.     input_file   = sys.argv[1]
  19.     message      = sys.argv[2]
  20.     output_file  = sys.argv[3]
  21.        
  22.     if not os.path.exists(input_file):
  23.         print 'File "%s" does not exist' % input_file
  24.         exit(1)
  25.        
  26.     message_binary = to_binary_string(message)
  27.     print 'message_binary', message_binary
  28.  
  29.     with open(input_file) as f:
  30.         container = f.read()
  31.    
  32.     assert(container.count('o') >= len(message_binary))
  33.    
  34.     indices = []
  35.     for idx, c in enumerate(container):
  36.         if c == 'o':
  37.             indices.append(idx)
  38.            
  39.     container_lst = list(container)
  40.     for idx, bit in enumerate(message_binary):
  41.         if bit == '1':
  42.             container_lst[indices[idx]] = ' # russian
  43.        
  44.        
  45.            
  46.    with open(output_file, 'w') as f:
  47.        f.write(''.join(container_lst))
  48.    
  49. if __name__ == '__main__':
  50.    main()
Advertisement
Add Comment
Please, Sign In to add comment