Embedding a string into an image can pick pixels sequentially or randomly. Let’s implement each algorithm in turn.

1. Sequential embedding

Sequential embedding is simple, iterating through each pixel and embedding the binary into the last digit, the eighth digit.

To write the report, I also drew a flow chart, which is posted here for easy understanding:

Python version 1.1

First I used Python to implement a, mainly is not familiar with Matlab, using Python3 PIL library is very simple to write. Let’s just look at the code

# coding: utF-8 # python 3.6.6 from PIL import Image import time def str_convert_bin(s): result = '' for c in s: b = bin(ord(c)).replace('0b', ") b = '0'*(7-len(b))+b result = result+b return result # def bin_convert_str(b): B1 = [b[I: I +7] for I in range(0, len(b), 7)] for I in range(len(b1)): B2 = CHR (int(b1[I],2)) STR = STR +b2 return STR # insert(im,bin1): size = im.size length = len(bin1) k=0 flag=0 for i in range(size[0]): for j in range(size[1]): # im. Getpixel ((I, j)) read pixel pixel_b of pixels (I, j) = bin (im) getpixel ((I, j)) [2]). The replace (' 0 b ', ') if pixel_b [1:] < bin1 [k]. # im. Putpixel ((I, j), (x, y, z)) set the pixel (I, j) RGB values for the (x, y, z) im.putpixel((i,j),(im.getpixel((i,j))[0],im.getpixel((i,j))[1],im.getpixel((i,j))[2]+1)) if pixel_b[-1:]>bin1[k]: im.putpixel((i,j),(im.getpixel((i,j))[0],im.getpixel((i,j))[1],im.getpixel((i,j))[2]-1)) k=k+1 if k==length: Flag = 1 break the if flag = = 1: break the print (" string embedded \ n \ n ") # extract string im: Image (), length: binary string length def extract (im, length) : size = im.size k=0 result='' flag=0 for i in range(size[0]): for j in range(size[1]): pixel_b=bin(im.getpixel((i,j))[2]).replace('0b', '') result=result+pixel_b[-1:] k=k+1 if k==length: flag=1 break if flag==1: \n%s"%result) STR = bin_convert_str(result) print(" \n%s"% STR) def main(): Test_str =input(" please enter string :\n") result = str_convert_bin(test_str) print(" to be embedded string converted to binary :\n%s"%result) print(" start embedding...." Im = image.open ("2.bmp") insert(im, result) time.sleep(5) print(" ") extract(im, len(result)) if __name__=='__main__': main()Copy the code

Results:

Matlab version 1.2

Subsequent more complex steganography or need to use MATLAB, so it is still matlab.

Matlab will not be introduced, I also learn to use first, the code is rough, barely pasted code:

% By gengyanqing % LSB hide (sequentially hide) % can hide numbers, letters, English characters ex: hello,world.111 % JPG distortion! Using PNG/BMP clear all; clc; data=imread('1.png'); % read image STR =input(' Please enter the string to be sneaked into :','s'); % Receive string str_bin_mat=dec2bin(STR); L_str_bin_mat =size(str_bin_mat); % binary matrix str_bin= "; for i=1:l_str_bin_mat(1) for j=1:l_str_bin_mat(2) str_bin=[str_bin,str_bin_mat(i,j)]; End end disp(' the binary form of the string to be embedded is '); disp(str_bin); % check whether it can be fully embedded [l,w,h]=size(data); If length(str_bin)>=l*w*h error(' length exceeds!! '); End % data1=data; Disp (' start embedding '); flag1=1; Flag2 =1; flag3=1; for i=1:l if flag3==0 break end for j=1:w if flag2==0 flag3=0; break end for k=1:h if flag1>length(str_bin) disp('over'); flag2=0; break end a=dec2bin(data1(i,j,k),8); % number in binary data1 (I, j, k) = bin2dec ([a (1:7), str_bin (flag1)]); Flag1 =flag1+1; Imwrite (data1,'1-2. PNG ') disp(' embedded complete, save as 1-2. PNG '); % below is the extraction program disp(' Start extraction... ') data2 = imread('1-2.png'); [l,w,h]=size(data2); str_bin1=''; % Extracted binary string locationx=[]; locationy=[]; locationxy=[]; m=length(str_bin); flag1=1; flag2=1; flag3=1; for i=1:l if flag3==0 break end for j=1:w if flag2==0 flag3=0; break end for k=1:h if flag1>length(str_bin) flag2=0; break end a=dec2bin(data2(i,j,k),8); Str_bin1 =[str_bin1,a(8)]; Flag1 =flag1+1; End end end disp(' Extract complete! '); Disp (' Extracted binary string: '); disp(str_bin1); Disp (' start conversion... ') % binary string str2= "; for q=1:length(str_bin1)/l_str_bin_mat(2) w=str_bin1((q-1)*l_str_bin_mat(2)+1:q*l_str_bin_mat(2)); %w is every seven bits a=bin2dec(w); If a>9 str2=[str2,char(a)]; end if a<9 str2=[str2,a]; End end disp(' conversion completed '); Disp (' final result: '); disp(str2);Copy the code

Can be embedded with characters, numbers, letters, the result is as follows:

2. Random LSB steganography

It’s pretty much the same order, it’s just changing (I,j) to random points as we traverse pixels, we can write a random function that randomly generates lists X and Y, and when we embed (I,j) we change it to (X(I +j), Y(I +j)), Why not (X(I), Y(j))? You can think about that.

I have also drawn a flow chart for this, but there seems to be a problem, so let’s leave it at that.

Next look at the function that generates the random list randomxy.m:

Function [x,y]=randxy(l,w,len_str_bin,key) % set the random seed, Generate a string of random numbers rand('seed',key); disp('hhhhhhhhh'); x=randperm(l,len_str_bin); y=randperm(w,len_str_bin); %x = unique(x); %y = unique(y); % to reprocess endCopy the code

Then look at the main code:

% By gengyanqing % LSB hide (random hide) % Can hide numbers, letters, English characters ex: hello,world.111 % JPG distortion! Using PNG/BMP clear all; clc; data=imread('1.png'); % read image STR =input(' Please enter the string to be sneaked into :','s'); % Receive string str_bin_mat=dec2bin(STR); L_str_bin_mat =size(str_bin_mat); % binary matrix str_bin= "; for i=1:l_str_bin_mat(1) for j=1:l_str_bin_mat(2) str_bin=[str_bin,str_bin_mat(i,j)]; End end disp(' the binary form of the string to be embedded is '); disp(str_bin); % check whether it can be fully embedded [l,w,h]=size(data); If length(str_bin)>=l*w*h error(' length exceeds!! '); End % data1=data; Disp (' start embedding '); flag1=1; Flag2 =1; flag3=1; [x,y]=randxy(l,w,length(str_bin),88); for i=1:l if flag3==0 break end for j=1:w if flag2==0 flag3=0; break end for k=1:h if flag1>length(str_bin) disp('over'); flag2=0; break end a=dec2bin(data1(x(i+j),y(i+j),k),8); % number in binary data1 (x (I + j), y (I + j), k) = bin2dec ([a (1:7), str_bin (flag1)]); Flag1 =flag1+1; Imwrite (data1,'1-2. PNG ') disp(' embedded complete, save as 1-2. PNG '); X and y are provided to extract binary string bits disp(' Start extracting... ') data2 = imread('1-2.png'); [l,w,h]=size(data2); str_bin1=''; % Extracted binary string locationx=[]; locationy=[]; locationxy=[]; m=length(str_bin); flag1=1; flag2=1; flag3=1; for i=1:l if flag3==0 break end for j=1:w if flag2==0 flag3=0; break end for k=1:h if flag1>length(str_bin) flag2=0; break end a=dec2bin(data2(x(i+j),y(i+j),k),8); % decimal to binary locationx=[locationx,x(I +j)]; % random point x locationy=[locationy,y(I +j)]; % locationxy=[locationxy;x(I +j),y(I +j),k]; str_bin1=[str_bin1,a(8)]; Flag1 =flag1+1; End end end disp(' Extract complete! '); Disp (' Extracted binary string: '); disp(str_bin1); Disp (' start conversion... ') % binary string str2= "; for q=1:length(str_bin1)/l_str_bin_mat(2) w=str_bin1((q-1)*l_str_bin_mat(2)+1:q*l_str_bin_mat(2)); %w is every seven bits a=bin2dec(w); If a>9 str2=[str2,char(a)]; end if a<9 str2=[str2,a]; End end disp(' conversion completed '); Disp (' final result: '); disp(str2); Disp (' random positions are '); disp(locationxy); plot(locationx,locationy);Copy the code

Results:

The following is a diagram of the hidden points (as you can see, they are really random)