Advertisement
opexxx

pyBinHexEncoder.py

Jul 12th, 2014
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. # -*- encoding: utf-8 -*-
  2. #
  3. #  FECT/pyBinHexEncoder.py
  4. #  
  5. #  Author: Jean-Philippe Teissier ( @Jipe_ )
  6. #    
  7. #  This work is licensed under the GNU General Public License
  8. #
  9.  
  10. import binascii
  11. import argparse
  12. import sys
  13.  
  14. __version__ = '0.1'
  15.  
  16. def Main():
  17.     ''' main '''
  18.  
  19.     parser = argparse.ArgumentParser(description='Create a hex encoded file from a binary file')
  20.     parser.add_argument('-i', '--input', help='Input filename')
  21.     parser.add_argument('-o', '--output', help='Output filename')
  22.     args = parser.parse_args()
  23.  
  24.     if args.input and args.output:
  25.         bytes = None
  26.  
  27.         print '[*] ' + args.input + ' -> ' + args.output
  28.  
  29.         try:
  30.             with open(args.input, 'rb') as f:
  31.                 bytes = f.read()
  32.         except IOError as e:
  33.             print '[!] Input Error: ' + e.strerror
  34.         except:
  35.             print '[!] Unexpected input error:', sys.exc_info()[0] 
  36.  
  37.         if bytes:
  38.             hexdata = binascii.hexlify(bytes)
  39.  
  40.         try:
  41.             with open(args.output, 'w+') as f2:
  42.                 f2.write(hexdata)
  43.         except IOError as e:
  44.             print '[!] Output Error: ' + e.strerror
  45.         except:
  46.             print '[!] Unexpected output error:', sys.exc_info()[0]
  47.     else:
  48.         print '[!] Error: missing parameter (-i or -o)'
  49.  
  50. if __name__ == '__main__':
  51.     Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement