1. 程式人生 > >MATLAB-LSB 資訊隱藏

MATLAB-LSB 資訊隱藏

MATLAB - LSB 資訊隱藏工具

2018-8-24 20:29:45 【原創】

1. 執行環境

最近用到 LSB 的資訊隱藏技術將資料隱藏到圖片檔案中,遂寫了一個隱藏工具,使用 MATLAB 實現起來也很容易。

另外說一下,如果 MATLAB-2010a 版本在安裝破解之後還是不能用時候,修改一下電腦的時間就好了,原理是序列號有過期時間,和本地電腦做校驗。

2. 實現的功能

  1. 支援讀取使用者要隱藏的資料
  2. 支援將資料寫入影象檔案
  3. 支援將影象檔案進行儲存
  4. 支援提取圖片中隱藏的資料,並且寫入一個txt文件在當前目下

3.程式碼

為了方便檢視,我將程式碼中的註釋刪掉了,只留了一些漢語的文字註釋。

另外,還貼了一個完整版的。

在 MATLAB 如果不會用圖形化開發框架的話,可以搜尋一下。其實就是在 matlab 的命令列寫入命令“guide”,即可呼叫開啟圖形化框架

1. 刪除註釋後的

function varargout = chenyun(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @chenyun_OpeningFcn, ...
                   'gui_OutputFcn',  @chenyun_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end


function chenyun_OpeningFcn(hObject, eventdata, handles, varargin)

handles.output = hObject;

guidata(hObject, handles);

function varargout = chenyun_OutputFcn(hObject, eventdata, handles) 

set(handles.edit1,'enable','off'); 
varargout{1} = handles.output;


function pushbutton1_Callback(hObject, eventdata, handles)

[filename, pathname] = uigetfile(...
    {'*.bmp;*.png','Image Files(*.bmp,*.png)';...
    '*.*', 'All Files (*.*)'},...
    '選擇一個載體影象:');

%讀入影象矩陣
global C;
global fpath;
axes(handles.axes1);
fpath=[pathname filename];
C=imread(fpath);%讀取影象並存放到C中
imshow(C);


function pushbutton2_Callback(hObject, eventdata, handles)

global C;
global L;
C_M=C;

[filename, pathname] = uigetfile({'*.txt','*.mat'},'請選擇待隱藏的資訊檔名...');
msgpath=[pathname filename];
fileID=fopen(msgpath,'r');%開啟檔案
[M,L]=fread(fileID,'ubit1');

%將二進位制的祕密訊息M嵌入到載密影象的LSB面
[m,n]=size(C);

p=1;%嵌入計數器
for i=1:m
    for j=1:n
        C_M(i,j)=C(i,j)-mod(C(i,j),2)+M(p,1);%嵌入方法是先將LSB置零,然後加上祕密訊息中的對應的0或1
        if L==p   %嵌入終止條件:當嵌入的個數為祕密訊息的長度時,跳出迴圈,不再嵌入
         break;
        end
       p=p+1;
 end
 if L==p    %嵌入終止條件:當嵌入的個數為祕密訊息的長度時,跳出迴圈,不再嵌入
    break;
 end
end


[filename1, pathname1] = uiputfile({'*.bmp','*.bmp'},'將隱藏影象另存為...');
imagepath=[pathname1 filename1];
fileID=fopen(imagepath,'w');%開啟檔案
imwrite(C_M,imagepath','bmp');


% imwrite(C_M,'pic70.bmp','bmp');

 %檢視嵌入資訊情況:將載密影象減去載體影象
for i=1:m
    for j=1:n
        C_C(i,j)=C_M(i,j)-C(i,j);
   end
 end

%輸出第二個影象

%imshow(cover);
axes(handles.axes2);
%fpath=[pathname filename];
%cover=imread(fpath);
%ste_cover=double(cover);
%imshow(cover);
imshow(C_M);

function pushbutton3_Callback(hObject, eventdata, handles)
global L;
global fpath;


[filename, pathname] = uigetfile(...
    {'*.bmp;*.png','Image Files(*.bmp,*.png)';...
    '*.*', 'All Files (*.*)'},...
    '請選擇待提取資訊的隱藏影象:');

%讀入影象矩陣

fpath=[pathname filename];

axes(handles.axes1);
C=imread(fpath);%讀取影象並存放到C中
imshow(C);

axes(handles.axes2);
C=imread(fpath);%讀取影象並存放到C中
imshow(C);

lsbsget(fpath,L);

fid = fopen('secret.txt','r');
i = 1;
while feof(fid) ==0
   b(i,:) = fgetl(fid);
   i = i+1;
end

set( handles.edit1,'string',b);
fclose(fid);

ra=fix(mean(double(get(handles.edit4,'string'))));
code_disp=get(handles.edit1,'String');
code=str2num(b);
text=char((code-ra)/ra);
set( handles.edit2,'string',text);


function edit1_Callback(hObject, eventdata, handles)

function edit1_CreateFcn(hObject, eventdata, handles)

function edit2_Callback(hObject, eventdata, handles)

function edit2_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function axes1_CreateFcn(hObject, eventdata, handles)

function axes2_CreateFcn(hObject, eventdata, handles)

function pushbutton_readmsg_Callback(hObject, eventdata, handles)


global code_disp;%密文

ra=fix(mean(double(get(handles.edit4,'string'))));%取整避免舍入誤差
% ra為加密運算元,是由使用者輸入且僅有使用者本人知道的字串轉換而來,這裡的轉換演算法是將字串中的字元求
%其ASCII碼的平均值,作為加密運算元ra的值。
text=get(handles.edit2,'String'); %得到要轉換的文字內容  
code=double(text); %取得文字內容的ASCII碼陣列
encode=code*ra+ra;%轉換成要顯示的編碼
code_disp=num2str(encode); %將編碼轉換成字串,便於顯示  
set(handles.edit1,'String',code_disp)%顯示轉換後的結果

function pushbutton_savesecret_Callback(hObject, eventdata, handles)

global code_disp;%密文

[filename, pathname] = uiputfile(...
    {'*.txt','txt files(*.txt)';...
    '*.*', 'All Files (*.*)'},...
    '將密文另存為...');
msgpath=[pathname filename];
fileID=fopen(msgpath,'w');
fwrite(fileID,code_disp,'char');

function edit4_Callback(hObject, eventdata, handles)

function edit4_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function S=lsbsget(C_M,L)
%讀取載密影象矩陣
C_M1=imread(C_M);
[m,n]=size(C_M1);
S=zeros(1,L);
%讀出LSB平面中0、1存入S中
for i=1:m
    for j=1:n
        if (i-1)*m+j>L%判斷嵌入長度是否大於L,若大於L,則終止迴圈
            break;
        end
        S(1,(i-1)*m+j)=mod(C_M1(i,j),2);
    end
    if (i-1)*m+j>L%判斷嵌入長度是否大於L,若大於L,則終止迴圈
         break;
    end
end

%將二進位制向量S轉化為ASCII碼並存放到文字檔案secret中
fileID=fopen('secret.txt','w');
a=L/8;%計算字元個數
SC=zeros(1,a);
for i=1:a
    b=0;
    for j=1:8
        b=b+S(1,(i-1)*8+j)*power(2,j-1);
    end
    SC(1,i)=b;
    fwrite(fileID,SC(1,i),'char');  
end
fclose(fileID);

2. 刪除註釋前的

function varargout = chenyun(varargin)
% CHENYUN M-file for chenyun.fig
%      CHENYUN, by itself, creates a new CHENYUN or raises the existing
%      singleton*.
%
%      H = CHENYUN returns the handle to a new CHENYUN or the handle to
%      the existing singleton*.
%
%      CHENYUN('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CHENYUN.M with the given input arguments.
%
%      CHENYUN('Property','Value',...) creates a new CHENYUN or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before chenyun_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to chenyun_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help chenyun

% Last Modified by GUIDE v2.5 09-May-2015 09:22:36

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @chenyun_OpeningFcn, ...
                   'gui_OutputFcn',  @chenyun_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before chenyun is made visible.
function chenyun_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to chenyun (see VARARGIN)

% Choose default command line output for chenyun
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes chenyun wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = chenyun_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.edit1,'enable','off'); 
% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[filename, pathname] = uigetfile(...
    {'*.bmp;*.png','Image Files(*.bmp,*.png)';...
    '*.*', 'All Files (*.*)'},...
    '選擇一個載體影象:');

%讀入影象矩陣
global C;
global fpath;
axes(handles.axes1);
fpath=[pathname filename];
C=imread(fpath);%讀取影象並存放到C中
imshow(C);


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global C;
global L;
C_M=C;
%imwrite(ste_cover,output);

[filename, pathname] = uigetfile({'*.txt','*.mat'},'請選擇待隱藏的資訊檔名...');
msgpath=[pathname filename];
fileID=fopen(msgpath,'r');%開啟檔案
[M,L]=fread(fileID,'ubit1');

%將二進位制的祕密訊息M嵌入到載密影象的LSB面
[m,n]=size(C);

p=1;%嵌入計數器
for i=1:m
    for j=1:n
        C_M(i,j)=C(i,j)-mod(C(i,j),2)+M(p,1);%嵌入方法是先將LSB置零,然後加上祕密訊息中的對應的0或1
        if L==p   %嵌入終止條件:當嵌入的個數為祕密訊息的長度時,跳出迴圈,不再嵌入
         break;
        end
       p=p+1;
 end
 if L==p    %嵌入終止條件:當嵌入的個數為祕密訊息的長度時,跳出迴圈,不再嵌入
    break;
 end
end


[filename1, pathname1] = uiputfile({'*.bmp','*.bmp'},'將隱藏影象另存為...');
imagepath=[pathname1 filename1];
fileID=fopen(imagepath,'w');%開啟檔案
imwrite(C_M,imagepath','bmp');


% imwrite(C_M,'pic70.bmp','bmp');

 %檢視嵌入資訊情況:將載密影象減去載體影象
for i=1:m
    for j=1:n
        C_C(i,j)=C_M(i,j)-C(i,j);
   end
 end

%輸出第二個影象

%imshow(cover);
axes(handles.axes2);
%fpath=[pathname filename];
%cover=imread(fpath);
%ste_cover=double(cover);
%imshow(cover);
imshow(C_M);

% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global L;
global fpath;


[filename, pathname] = uigetfile(...
    {'*.bmp;*.png','Image Files(*.bmp,*.png)';...
    '*.*', 'All Files (*.*)'},...
    '請選擇待提取資訊的隱藏影象:');

%讀入影象矩陣

fpath=[pathname filename];

axes(handles.axes1);
C=imread(fpath);%讀取影象並存放到C中
imshow(C);

axes(handles.axes2);
C=imread(fpath);%讀取影象並存放到C中
imshow(C);

lsbsget(fpath,L);

fid = fopen('secret.txt','r');
i = 1;
while feof(fid) ==0
   b(i,:) = fgetl(fid);
   i = i+1;
end

set( handles.edit1,'string',b);
fclose(fid);

ra=fix(mean(double(get(handles.edit4,'string'))));
code_disp=get(handles.edit1,'String');
code=str2num(b);
text=char((code-ra)/ra);
set( handles.edit2,'string',text);


function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


%a1 = str2double(get(handles.edit1,'String'));

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
%if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
%    set(hObject,'BackgroundColor','white');
%end



function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes1


% --- Executes during object creation, after setting all properties.
function axes2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes2


% --- Executes on button press in pushbutton_readmsg.
function pushbutton_readmsg_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_readmsg (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global code_disp;%密文

ra=fix(mean(double(get(handles.edit4,'string'))));%取整避免舍入誤差
% ra為加密運算元,是由使用者輸入且僅有使用者本人知道的字串轉換而來,這裡的轉換演算法是將字串中的字元求
%其ASCII碼的平均值,作為加密運算元ra的值。
text=get(handles.edit2,'String'); %得到要轉換的文字內容  
code=double(text); %取得文字內容的ASCII碼陣列
encode=code*ra+ra;%轉換成要顯示的編碼
code_disp=num2str(encode); %將編碼轉換成字串,便於顯示  
set(handles.edit1,'String',code_disp)%顯示轉換後的結果


% --- Executes on button press in pushbutton_savesecret.
function pushbutton_savesecret_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_savesecret (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global code_disp;%密文

[filename, pathname] = uiputfile(...
    {'*.txt','txt files(*.txt)';...
    '*.*', 'All Files (*.*)'},...
    '將密文另存為...');
msgpath=[pathname filename];
fileID=fopen(msgpath,'w');
fwrite(fileID,code_disp,'char');

function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function S=lsbsget(C_M,L)
%讀取載密影象矩陣
C_M1=imread(C_M);
[m,n]=size(C_M1);
S=zeros(1,L);
%讀出LSB平面中0、1存入S中
for i=1:m
    for j=1:n
        if (i-1)*m+j>L%判斷嵌入長度是否大於L,若大於L,則終止迴圈
            break;
        end
        S(1,(i-1)*m+j)=mod(C_M1(i,j),2);
    end
    if (i-1)*m+j>L%判斷嵌入長度是否大於L,若大於L,則終止迴圈
         break;
    end
end

%將二進位制向量S轉化為ASCII碼並存放到文字檔案secret中
fileID=fopen('secret.txt','w');
a=L/8;%計算字元個數
SC=zeros(1,a);
for i=1:a
    b=0;
    for j=1:8
        b=b+S(1,(i-1)*8+j)*power(2,j-1);
    end
    SC(1,i)=b;
    fwrite(fileID,SC(1,i),'char');  
end
fclose(fileID);