Steganografi dengan Metode Substitusi LSB (Least Significant Bit)


Steganografi merupakan suatu teknik menyembunyikan sebuah file pada file lainnya. Dalam metode ini diperlukan file sebagai penampung (cover) dan file lain yang akan ditampung (message). File penampung maupun file yang akan ditampung dapat berupa citra, audio, maupun text.

Penggunaan steganografi bertujuan untuk menyembunyikan atau menyamarkan suatu data sehingga sulit untuk dideteksi (encoding). Data yang disembunyikan dapat diekstraksi kembali sama seperti keadaan aslinya (decoding).

Berikut ini merupakan contoh pemrograman matlab mengenai steganografi dengan metode substitusi LSB (Least Significant Bit) di mana file penampung berupa citra digital sedangkan file yang akan ditampung berupa text.

Langkah-langkah pemrogramannya adalah sebagai berikut:

1. Pembuatan GUI Encoding Citra

function varargout = encoding_LSB(varargin)
% ENCODING_LSB MATLAB code for encoding_LSB.fig
%      ENCODING_LSB, by itself, creates a new ENCODING_LSB or raises the existing
%      singleton*.
%
%      H = ENCODING_LSB returns the handle to a new ENCODING_LSB or the handle to
%      the existing singleton*.
%
%      ENCODING_LSB('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ENCODING_LSB.M with the given input arguments.
%
%      ENCODING_LSB('Property','Value',...) creates a new ENCODING_LSB or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before encoding_LSB_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to encoding_LSB_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 encoding_LSB

% Last Modified by GUIDE v2.5 22-Jul-2020 12:51:03

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @encoding_LSB_OpeningFcn, ...
    'gui_OutputFcn',  @encoding_LSB_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 encoding_LSB is made visible.
function encoding_LSB_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 encoding_LSB (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);
movegui(hObject,'center');

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


% --- Outputs from this function are returned to the command line.
function varargout = encoding_LSB_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)

% 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)

% menampilkan menu open file
[nama_file, nama_path] = uigetfile('*.bmp');
% jika ada file yang dipilih maka akan mengeksekusi perintah di bawahnya
if ~isequal(nama_file,0)
    % mereset button2
    axes(handles.axes1)
    cla reset
    set(gca,'XTick',[])
    set(gca,'YTick',[])
    
    axes(handles.axes2)
    cla reset
    set(gca,'XTick',[])
    set(gca,'YTick',[])
    
    set(handles.edit2,'String',[])
    
    % membaca file citra
    Img_cover = imread(fullfile(nama_path,nama_file));
    % menampilkan citra pada axes1
    axes(handles.axes1)
    imshow(Img_cover)
    
    % menampilkan nama file citra pada edit text
    set(handles.edit1,'String',nama_file)
    
    % menyimpan variabel Img_cover pada lokasi handles
    handles.Img_cover = Img_cover;
    guidata(hObject, handles)
    
    % mereset button2
    set(handles.pushbutton3,'Enable','on')
    set(handles.pushbutton4,'Enable','off')
    
else
    % jika tidak ada file yang dipilih maka akan kembali
    return
end


% --- 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)

% memanggil variabel Img_cover yang ada di lokasi handles
Img_cover = handles.Img_cover;

[row,col,~] = size(Img_cover);
R = Img_cover(:,:,1);
G = Img_cover(:,:,2);
B = Img_cover(:,:,3);

pesan = get(handles.edit2,'String');
if isempty(pesan) % cek kondisi pesan di edit text
    msgbox('Silahkan ketikkan pesan terlebih dahulu!','peringatan','warn');
    return;
end

% penentuan maksimal karakter pesan dengan maksimal 1 penyisipan / piksel
karakter_max = (row -1)*(col);
karakter_max = round((karakter_max*3)/8); % per channel warna

% perhitungan panjang pesan di edit text
panjang_pesan = length(pesan); % masih dalam hitungan desimal
if panjang_pesan < karakter_max
    pesan_biner = reshape(dec2bin(double(pesan),8).',1,[]);
else
    msgbox('Maaf,pesan terlalu panjang!','peringatan','warn');
    return;
end

% membuat kunci dari panjang pesan (dalam desimal)
baris_max = row;
kolom_max = col;
pesan_kunci = dec2bin(panjang_pesan,12);
image_pesan_r = dec2bin(R(baris_max,1),8);
image_pesan_g = dec2bin(G(baris_max,1),8);
image_pesan_b = dec2bin(B(baris_max,1),8);
image_pesan_r2 = dec2bin(R(baris_max,2),8);
image_pesan_g2 = dec2bin(G(baris_max,2),8);
image_pesan_b2 = dec2bin(B(baris_max,2),8);

image_pesan_r(7:8) = pesan_kunci(1:2);
image_pesan_g(7:8) = pesan_kunci(3:4);
image_pesan_b(7:8) = pesan_kunci(5:6);
image_pesan_r2(7:8) = pesan_kunci(7:8);
image_pesan_g2(7:8) = pesan_kunci(9:10);
image_pesan_b2(7:8) = pesan_kunci(11:12);

R(baris_max,1) = bin2dec(image_pesan_r);
G(baris_max,1) = bin2dec(image_pesan_g);
B(baris_max,1) = bin2dec(image_pesan_b);
R(baris_max,2) = bin2dec(image_pesan_r2);
G(baris_max,2) = bin2dec(image_pesan_g2);
B(baris_max,2) = bin2dec(image_pesan_b2);

% LSB
panjang_pesan = panjang_pesan*8;
for i = 1:baris_max-1
    for j = 1:kolom_max
        % untuk piksel merah
        if panjang_pesan ~= 0
            gambar_biner_r = dec2bin(R(i,j),8);
            gambar_biner_r(1,8) = pesan_biner(1,1);
            R(i,j) = bin2dec(gambar_biner_r);
            
            pesan_biner(1:1) = [];
            panjang_pesan = length(pesan_biner);
        end
        
        % untuk piksel hijau
        if panjang_pesan ~= 0
            gambar_biner_g = dec2bin(G(i,j),8);
            gambar_biner_g(1,8) = pesan_biner(1,1);
            G(i,j) = bin2dec(gambar_biner_g);
            
            pesan_biner(1:1) = [];
            panjang_pesan = length(pesan_biner);
        end
        
        % untuk piksel biru
        if panjang_pesan ~= 0
            gambar_biner_b = dec2bin(B(i,j),8);
            gambar_biner_b(1,8) = pesan_biner(1,1);
            B(i,j) = bin2dec(gambar_biner_b);
            
            pesan_biner(1:1) = [];
            panjang_pesan = length(pesan_biner);
        end
    end
end

Img_steg(:,:,1) = uint8(R);
Img_steg(:,:,2) = uint8(G);
Img_steg(:,:,3) = uint8(B);

% menampilkan citra hasil steganografi pada axes3
axes(handles.axes2)
imshow(Img_steg)

% menyimpan citra hasil steganografi pada lokasi handles
handles.Img_steg = Img_steg;
guidata(hObject, handles)

% mereset button2
set(handles.pushbutton4,'Enable','on')


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


% --- 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


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

% membaca citra hasil steganografi yang ada di lokasi handles
Img_steg = handles.Img_steg;
% menampilkan menu save file
[nama_file, nama_path] = uiputfile('*.bmp');
% jika ada file yang dipilih maka akan mengeksekusi perintah di bawahnya
if ~isequal(nama_file,0)
    % menyimpan citra hasil steganografi
    imwrite(Img_steg,fullfile(nama_path,nama_file))
else
    % jika ada file yang dipilih maka akan kembali
    return
end

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

% mereset button2
axes(handles.axes1)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])

axes(handles.axes2)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])

set(handles.edit1,'String',[])
set(handles.edit2,'String',[])

set(handles.pushbutton3,'Enable','off')
set(handles.pushbutton4,'Enable','off')


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

Tampilan GUI untuk proses encoding adalah sebagai berikut:

Tampilan Awal

Load Citra Cover (File penampung/File yang akan disisipi)

Mengisi pesan text yang akan ditampung/disisipkan

Proses Encoding

2. Pembuatan GUI Decoding Citra

function varargout = decoding_LSB(varargin)
% DECODING_LSB MATLAB code for decoding_LSB.fig
%      DECODING_LSB, by itself, creates a new DECODING_LSB or raises the existing
%      singleton*.
%
%      H = DECODING_LSB returns the handle to a new DECODING_LSB or the handle to
%      the existing singleton*.
%
%      DECODING_LSB('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DECODING_LSB.M with the given input arguments.
%
%      DECODING_LSB('Property','Value',...) creates a new DECODING_LSB or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before decoding_LSB_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to decoding_LSB_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 decoding_LSB

% Last Modified by GUIDE v2.5 18-Aug-2020 22:06:00

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @decoding_LSB_OpeningFcn, ...
    'gui_OutputFcn',  @decoding_LSB_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 decoding_LSB is made visible.
function decoding_LSB_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 decoding_LSB (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);
movegui(hObject,'center');

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


% --- Outputs from this function are returned to the command line.
function varargout = decoding_LSB_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)

% 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)

% menampilkan menu open file
[nama_file, nama_path] = uigetfile('*.bmp');
% jika ada file yang dipilih maka akan mengeksekusi perintah di bawahnya
if ~isequal(nama_file,0)
    % mereset button2
    axes(handles.axes1)
    cla reset
    set(gca,'XTick',[])
    set(gca,'YTick',[])
    
    set(handles.edit2,'String',[])
    
    % membaca file citra
    Img_steg = imread(fullfile(nama_path,nama_file));
    % menampilkan citra pada axes1
    axes(handles.axes1)
    imshow(Img_steg)
    
    % menampilkan nama file citra pada edit text
    set(handles.edit1,'String',nama_file)
    
    % menyimpan variabel Img_steg pada lokasi handles
    handles.Img_steg = Img_steg;
    guidata(hObject, handles)
    
    % mereset button2
    set(handles.pushbutton2,'Enable','on')
else
    % jika tidak ada file yang dipilih maka akan kembali
    return
end

% --- 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)

tStart = tic;

% memanggil variabel Img_cover pada lokasi handles
Img_steg = handles.Img_steg;

[row,col,~] = size(Img_steg);
R = Img_steg(:,:,1);
G = Img_steg(:,:,2);
B = Img_steg(:,:,3);
baris_max = row;
kolom_max = col;

% mengambil kunci panjang pesan (dalam desimal)
piksel_r = dec2bin(R(baris_max,1),8);
piksel_g = dec2bin(G(baris_max,1),8);
piksel_b = dec2bin(B(baris_max,1),8);
piksel_r2 = dec2bin(R(baris_max,2),8);
piksel_g2 = dec2bin(G(baris_max,2),8);
piksel_b2 = dec2bin(B(baris_max,2),8);

pesan_r = piksel_r(7:8);
pesan_g = piksel_g(7:8);
pesan_b = piksel_b(7:8);
pesan_r2 = piksel_r2(7:8);
pesan_g2 = piksel_g2(7:8);
pesan_b2 = piksel_b2(7:8);

panjang_pesan = strcat(pesan_r, pesan_g, pesan_b, pesan_r2, pesan_g2, pesan_b2);
panjang_pesan = bin2dec(reshape(panjang_pesan,12,[]).');

% ekstraksi pesan
pesan = '';
for i = 1:baris_max-1
    for j = 1:kolom_max
        % untuk piksel merah
        panjang_biner = length(pesan);
        if panjang_biner < panjang_pesan*8
            gambar_biner_r = dec2bin(R(i,j),8);
            pesan_r = gambar_biner_r(1,8);
            pesan = strcat(pesan,pesan_r);
        else
            pesan_asli = char(bin2dec(reshape(pesan,8,[]).')).';
            set(handles.edit2,'String',pesan_asli);
            return;
        end
        
        % untuk piksel hijau
        panjang_biner = length(pesan);
        if panjang_biner < panjang_pesan*8
            gambar_biner_g = dec2bin(G(i,j),8);
            pesan_g = gambar_biner_g(1,8);
            pesan = strcat(pesan,pesan_g);
        else
            pesan_asli = char(bin2dec(reshape(pesan,8,[]).')).';
            set(handles.edit2,'String',pesan_asli);
            return;
        end
        
        % untuk piksel biru
        panjang_biner = length(pesan);
        if panjang_biner < panjang_pesan*8
            gambar_biner_b = dec2bin(B(i,j),8);
            pesan_b = gambar_biner_b(1,8);
            pesan = strcat(pesan,pesan_b);
        else
            pesan_asli = char(bin2dec(reshape(pesan,8,[]).')).';
            set(handles.edit2,'String',pesan_asli);
            return;
        end
    end
end


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


% --- 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


% --- 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)

% mereset button2
axes(handles.axes1)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])

set(handles.edit1,'String',[])
set(handles.edit2,'String',[])

set(handles.pushbutton2,'Enable','off')

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

Tampilan GUI untuk proses decoding adalah sebagai berikut:

Tampilan Awal

Load Citra Steganografi (File yang telah disisipi)

Proses decoding

Hasil ekstraksi pada proses decoding menunjukkan text yang sama seperti text yang disisipkan pada proses encoding.

File source code lengkap beserta citra yang ada pada pemrograman di atas dapat diperoleh melalui halaman berikut ini: Source Code

Posted on September 5, 2020, in Pengolahan Citra and tagged , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: