Advertisement
agnishom

SteganographyUltimate.py

Aug 17th, 2012
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. def encrypt(msg,key):
  2.     txt = []
  3.     for i in range(len(msg)):
  4.         txt.append(ord(msg[i])^ord(key[i%len(key)]))
  5.     return txt
  6. def getmessage(filename):
  7.     f = open(filename, 'rb')
  8.     return f.read()
  9. def buildfile(message, filename):
  10.     f = open(filename, 'wb')
  11.     f.write(message)
  12.     f.flush()
  13. def encode(msg, f):
  14.     k = len(f[0,0])
  15.     pos = 0
  16.     for i in msg:
  17.         for j in xrange(7,-1,-1):
  18.             color = pos%k
  19.             x = (pos/k)%img.size[0]
  20.             y = (pos/k)/img.size[0]
  21.             if not ((i >> j) & 1):
  22.                 value = []
  23.                 for h in range(k):
  24.                     if h == color:
  25.                         value.append((f[x,y][color])& ~1)
  26.                     else:
  27.                         value.append((f[x,y][h]))
  28.                 f[(x,y)] = tuple(value)
  29.             else:
  30.                 value = []
  31.                 for h in range(k):
  32.                     if h == color:
  33.                         value.append((f[x,y][color])|1)
  34.                     else:
  35.                         value.append((f[x,y][h]))
  36.                 f[(x,y)] = tuple(value)
  37.             pos += 1
  38.     filename = raw_input('Enter file name to save in:')
  39.     img.save(filename)
  40. def decode(byte, f):
  41.     k = len(f[0,0])
  42.     pos = 0
  43.     currentbyte = ""
  44.     msg = ""
  45.     while (pos < byte*8):
  46.             color = pos%k
  47.             x = (pos/k)%img.size[0]
  48.             y = (pos/k)/img.size[0]
  49.             if not ((f[x,y][color])%2):
  50.                 currentbyte += '0'
  51.             else:
  52.                 currentbyte += '1'
  53.             pos += 1
  54.             if not (pos%8):
  55.                 msg += chr(int(currentbyte, 2))
  56.                 currentbyte = ""
  57.     return msg
  58.    
  59. import Image
  60. key = raw_input('Enter Password:')
  61. image = raw_input('Enter image file name:')
  62. img = Image.open(image)
  63. handle = img.load()
  64. while True:
  65.     print "Choose one of the following:\n1.Create Steganographic Data\n2.Read Steganographic Data\n3.Exit\n"
  66.     x = raw_input('Your Choice:')
  67.     if (x == '1'):
  68.         i = raw_input("1. Read from a file\n2. Enter text manually\nYour Choice:")
  69.         if (i == '2'):
  70.             message = raw_input('Enter Message:')
  71.         elif (i == '1'):
  72.             filename = raw_input('Enter data file name:')
  73.             message = getmessage(filename)
  74.         message = encrypt(message, key)
  75.         encode(message, handle)
  76.         print "The size of the message was " + str(len(message)) + " bytes"
  77.     elif (x == '2'):
  78.         pix = int(raw_input('Enter the number of bytes you want to retrieve:'))
  79.         msg = decode(pix, handle)
  80.         result = ""
  81.         for i in encrypt(msg, key):
  82.             result += chr(i)
  83.         msg = result
  84.         del result
  85.         i = raw_input("1. Write to a file\n2. Display on Console\nYour Choice:")
  86.         if (i == '2'):
  87.             print msg
  88.         elif (i == '1'):
  89.             filename = raw_input('Enter file name to write in:')
  90.             buildfile(msg, filename)
  91.     elif (x == '3'):
  92.         break
  93.     else:
  94.         print
  95.         pass
  96. raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement