Learning

# How to do Steganography in python

By ayed_amira , on 02/05/2020 , updated on 09/10/2020 , 1 comment - 4 minutes to read How to do Steganography in python : You can hide text in a digital image in a way that is completely invisible to the naked eye. This technique is called watermarking. It’s actually steganography pure and simple.

##### How does it work?

A colour image can be described in computer terms as a triplet matrix. Each triplet gives the colour of a pixel in the RGB system. Today, an image is composed of several million pixels. This allows us to hide a large number of characters in these images.

#### Integration of the message in the image :

Each character in the text to be hidden will be represented by its extended ASCII code. For example, the ASCII code of “A” is 64, which gives in binary, on one byte (8 bits): 01000001 . The complete text will therefore be a sequence of 0 and 1, each character using 8 bits and implicitly 8 pixels.

In the python code below we will work only on the red component of the pixel (we can of course use all components). The hidden message will be: “AMIRA DATA – All the data you need“. For the first 8 pixels of the image, we will hide the length of the string. It is 34 characters long (each space must be counted so that the original message is not altered), i.e. 00100010 in binary value. The next step is to convert our string to binary. We saw above that the ASCII code of the letter “A” was 64 which gives in binary 01000001.

 pixel (red component) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Image originale 231 12 102 202 131 37 45 18 17 154 167 193 7 101 40 38 Reduction 230 12 102 202 130 36 44 18 16 154 166 192 6 100 40 38 Binary 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 1 Modified image 230 12 103 202 130 36 45 18 16 155 166 192 6 100 40 39

In the table below, the second row is given the red component of the first 16 pixels. In a first step, in the reduction line, the values are reduced to the largest even number less than or equal to the value . Then, we will add to these even numbers 0 and 1 and it is these 0 and 1, grouped by 8, which will transmit the hidden information.

```#-*- coding:Latin-1 -*-
from PIL import Image

# function to hide the message in the image
def encode_message(directory,saved_image,message):
im = Image.open(directory)
w,h=im.size # we retrieve the dimensions of the image
r,g,b=im.split() #Let's split the image in three (red green blue)
r=list(r.getdata())  #we turn the image into a list

u=len(message)
v=bin(len(message))[2:].rjust(8,"0")    #we note the length of the string and transform it to binary
ascii=[bin(ord(x))[2:].rjust(8,"0") for x in message]  #we transform the string into a list of 0 and 1
a=''.join(ascii) #transformation of the list into a chain

#the length of the list is encoded in the first 8 red pixels.
for j in range(8):
r[j]=2*int(r[j]//2)+int(v[j])

#we code the string in the following pixels
for i in range(8*u):
r[i+8]=2*int(r[i+8]//2)+int(a[i])

# we recreate the red image
nr = Image.new("L",(w,h))
nr.putdata(r)
# merging the three new images
imgnew = Image.merge('RGB',(nr,g,b))
imgnew.save(saved_image)
```

#### Retrieving text from the image

:

Recovery is a five-step process::

1. Retrieve the matrix describing the image.
2. Replace an even number by 0, an odd number by 1.
3. Group the bits in groups of 8.
4. Convert each byte to a decimal number.
5. Write the characters corresponding to the ASCII codes obtained.
```# function to retrieve the hidden message
def decode_message(directory):
im = Image.open(directory)
r,g,b=im.split()
r=list(r.getdata())
# Reading the length of the string
p=[str(x%2) for x in r[0:8]]
q="".join(p)
q=int(q,2)
n=[str(x%2) for x in r[8:8*(q+1)]]
b="".join(n)
message=""

for k in range(0,q):
l=b[8*k:8*k+8]
message=message+chr(int(l,2))
print (message)
```

I hope you were interested in How to do Steganography in python. Let us know what you think! 🙂 On 02/06/2020 at 12 h 53 min, Marouane Benz said:

Amazing !