Loading [MathJax]/extensions/TeX/AMSsymbols.js

November 1, 2012

Code Steganography

This is my code I used to hide the message in the images. The algorithm I made it thanks to my classmate Alejandro Avendaño thats why is very similar, well here is the code


import Image
import sys, os
from numpy import *
message=list()
msgDec=list()
cont=0
rgb=0#to know which pixel was used
#convert message into decimal
def msg_dec(file):
for words in file.readlines():
if len(words)==0:
continue
#ignore "\n"
words = words.rstrip("\n")
for letter in words:
message.append(letter)
#convert to ascii
for letter in msg:
number = ord(letter)
msgDec.append(number)
def algorithm(width,height,pixels):
area = width * height
decSize = int(len(msgDec))
range = area/decSize
condition = 0
aux = 0
z = range/4
char = 0
for i in range(width):
for j in range (height):
(p1,p2,p3)=pixels[i,j]#get pixels of the image
#just one time
if condition == 0:
mod = p3 % 7
if mod == 1:
hide = size
pixels[i,j] = (hide,p2,p3) #hide in the image
condition = condition + 1 #do it just one time
else:
if aux < range:
if cont > 0: #one more character?
#red pixel
if rgb == 0:
hide = conver[char]
pixels[i,j] = (hide,p2,p3)
char = char + 1
cont = cont - 1 #total letters - 1
aux = range
#green pixel
if rgb == 1:
hide = conver[char]
pixels[i,j] = (p1,hide,p3)
char = char + 1
cont = cont - 1 #total letters - 1
aux = range
#blue pixel
if rgb == 2:
hide = conver[char]
pixels[i,j] = (hide,p2,hide)
char = char + 1
cont = cont - 1 #total letters - 1
aux = range
if manage >= range: #next pixel
range = range + z/2
aux = 0
manage = manage +1# to control the jumps to the next pixel
if rgbb == 0:
rgbb = 1
elif rgbb == 1:
rgbb = 2
else:
rgbb = 0
def main(image,text):
file = open(archivo,"r")
msg_dec(file)
img = Image.open(image)
width,height = img.size()
pixels = img.load()
algorithm(width,height,pixels)
img = save("cpu.jpg")
#arg1 = image, arg2 = .txt
main(sys.argv[1],sys.argv[2])

And this was my first attempt but but I got stuck :C in this attempt I was manipulating binary


import Image
import sys, os
from numpy import *
def msg_bits(text):
file = open(text,'r')
msg = list()
ascii = list()
bitsMsg = list()
for words in file.readlines():
if len(words)==0:
continue
#ignore "\n"
words = words.rstrip("\n")
for letter in words:
msg.append(letter)
#convert to ascii
for letter in msg:
number = ord(letter)
ascii.append(number)
#convert to binary
for number in ascii:
bits = bin(number)
bits = bits[2:]
if number == 32:
bits = '0'+bits
bitsMsg.append(bits)
return bitsMsg
def bits_img(image,width,height):
bitsImg = list()
for i in range(width):
for j in range(height):
(r,g,b) = image.getpixel((i,j))
bitsR=bin(r)
bitsR = bitsR[2:]
bitsG=bin(g)
bitsG = bitsG[2:]
bitsB=bin(b)
bitsB = bitsB[2:]
bitsImg.append(bitsR)
bitsImg.append(bitsG)
bitsImg.append(bitsB)
return bitsImg
def cypher(img, msg):
for i in range(len(msg)):
byte = msg[i]
for j in range(len(byte)):
change = list(img.pop(0))
bit = byte[j]
change.pop()
change.append(bit)
img.insert(j,change)
return img
def main(image,text):
img=Image.open(image)
rgb_im=img.convert('RGB')
width,height=rgb_im.size
bitsMsg= msg_bits(text)
bitsImg= bits_img(rgb_im, width,height)
cy= cypher(bitsImg, bitsMsg)
#arg1 = image, arg2 = .txt
main(sys.argv[1],sys.argv[2])
#main(sys.argv[1])
view raw img.py hosted with ❤ by GitHub

No comments:

Post a Comment