Advertisement
FlyFar

Steganography - Python

Jul 7th, 2023
953
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | Cybersecurity | 0 0
  1. from PIL import image
  2. import binascii
  3. import optparse
  4.  
  5. def rgb2hex(r,g,b):
  6.     return '#{:02x}{:02x}{:02x}'.format(r,g,b)
  7.  
  8. def hex2rgb(hexcode):
  9.     return tuple(map(ord,hexcode[1:],decode('hex')))
  10.  
  11. def str2bin(message):
  12.     binary=bin(int(binascii.hexify(message),16))
  13.     return binary[2:]
  14.  
  15. def binary2str(binary):
  16.     message=binascii.unhexify('%x' % (int('0b'+binary,2)))
  17.     return message
  18.  
  19. def encode(hexcode,digit):
  20.     if hexcode[-1] in ('0','1','2','3','4','5'):
  21.         hexcode=hexcode[:-1]+digit
  22.         return hexcode
  23.     else:
  24.         return None
  25.  
  26. def decode(hexcode):
  27.     if hexcode[-1] in ('0','1'):
  28.         return hexcode[-1]
  29.     else:
  30.         None       
  31.  
  32. def hide(filename, message):
  33.     img=Image.open(filename)
  34.     binary=str2bin(message)+'1111111111111110'
  35.     if img.mode in ('RGBA'):
  36.         img=img.convert('RGBA')
  37.         datas=image.getdata()
  38.         newData=[]
  39.         digit=0
  40.         temp=''
  41.         for item in datas:
  42.             if (digit<len(binary)):
  43.                 newpix=encode(rgb2hex(item[0],item[1],item[2]),binayu[digit])
  44.                 if newpix==None:
  45.                     newData.append(item)
  46.                 else:
  47.                     r,g,b=hex2rgb(newpix)
  48.                     newData.append((r,g,b,255))
  49.                     digit+=1
  50.             else:
  51.                 newData.append(item)
  52.         img.putdata(newData)
  53.         img.save(filename,"PNG")
  54.         return "Completed!"
  55.     return "Incorrect Image Mode!!"
  56.  
  57. def retrieve(filename):
  58.     img=Image.open(filename)
  59.     binary=''
  60.  
  61.     if img.mode in ('RGBA'):
  62.         img=img.convert('RGBA')
  63.         datas=img.getdata()
  64.  
  65.         for item in datas:
  66.             digit=decode(rgb2hex(item[0],item[1],item[2]))
  67.             if digit==None:
  68.                 pass
  69.             else:
  70.                 binary=binary+digit
  71.                 if(binary[-16:]=="1111111111111110"):
  72.                     print "Success"
  73.                     return bin2str(binary[:-16])
  74.         return bin2str(binary)
  75.     return "Incorrect Image Mode!!"
  76.  
  77. def Main():
  78.     parser=optparse.OptionParser('Usage: %prog '+'-e/d <target file>')
  79.     parser.add_option('-e',dest='hide',type="string",help="target picture path to hide text")
  80.     parser.add_option('-d',dest='retrieve',type="string",help="target picture path to retrieve text")
  81.     (options.args)=parser.parse.args()
  82.     if (options.hide != None):
  83.         text=raw_input('Enter message:')
  84.         print hide(options.hide,text)
  85.     elif (options.retreive != None):
  86.         print retrieve(options.retrieve)
  87.     else:
  88.         print parser.usage
  89.         exit(0)
  90.  
  91. if __name__=="__main__":
  92.     Main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement