Upload
conanconan5599
View
213
Download
0
Embed Size (px)
Citation preview
7/27/2019 codeBT2
1/10
function varargout = TH02(varargin)% TH02 M-file for TH02.fig% TH02, by itself, creates a new TH02 or raises the existing% singleton*.%% H = TH02 returns the handle to a new TH02 or the handle to% the existing singleton*.%% TH02('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in TH02.M with the given input arguments.%% TH02('Property','Value',...) creates a new TH02 or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before TH02_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to TH02_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 TH02
% Last Modified by GUIDE v2.5 24-Oct-2013 07:24:26
% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @TH02_OpeningFcn, ...'gui_OutputFcn', @TH02_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{:});
elsegui_mainfcn(gui_State, varargin{:});
end% End initialization code - DO NOT EDIT
% --- Executes just before TH02 is made visible.function TH02_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 TH02 (see VARARGIN)
% Choose default command line output for TH02
7/27/2019 codeBT2
2/10
handles.output = hObject;
% Update handles structureguidata(hObject, handles);
% UIWAIT makes TH02 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.function varargout = TH02_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 structurevarargout{1} = handles.output;
function pathBox_Callback(hObject, eventdata, handles)% hObject handle to pathBox (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 pathBox as text% str2double(get(hObject,'String')) returns contents of pathBox as adouble
% --- Executes during object creation, after setting all properties.function pathBox_CreateFcn(hObject, eventdata, handles)% hObject handle to pathBox (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 Browse.function Browse_Callback(hObject, eventdata, handles)% hObject handle to Browse (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('*.txt', 'Pick an txt-file');fullpathname= strcat(pathname,filename);whos fullpathname;disp(fullpathname);
7/27/2019 codeBT2
3/10
set(handles.pathBox,'String',fullpathname);
% --- Executes on button press in Text2Bin.function Text2Bin_Callback(hObject, eventdata, handles)% hObject handle to Text2Bin (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)InpText=get(handles.InputText, 'String');str= ConvertToBinary(InpText);set(handles.OutputText,'String', [InpText ': ' str]);disp(str);
% --- 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)
function InputText_Callback(hObject, eventdata, handles)% hObject handle to InputText (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 InputText as text% str2double(get(hObject,'String')) returns contents of InputText as adouble
% --- Executes during object creation, after setting all properties.function InputText_CreateFcn(hObject, eventdata, handles)% hObject handle to InputText (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 OutputText_Callback(hObject, eventdata, handles)% hObject handle to OutputText (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 OutputText as text% str2double(get(hObject,'String')) returns contents of OutputText% as a double
7/27/2019 codeBT2
4/10
% --- Executes during object creation, after setting all properties.function OutputText_CreateFcn(hObject, eventdata, handles)% hObject handle to OutputText (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 Nhung.function Nhung_Callback(hObject, eventdata, handles)% hObject handle to Nhung (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)InpText=get(handles.InputText, 'String');str= ConvertToBinary(InpText);
fname=get(handles.pathBox,'String');fstream= fileread(fname)
fout =get(handles.pathBox2,'String');EncodeStream=Nhung(fstream,str);
filewrite(fout,EncodeStream);msgbox('Encode Xong');
% --- Executes on button press in Browse2.function Browse2_Callback(hObject, eventdata, handles)% hObject handle to Browse2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)[filename, pathname] = uiputfile('*.txt', 'Pick an txt-file');fullpathname= strcat(pathname,filename);whos fullpathname;disp(fullpathname);set(handles.pathBox2,'String',fullpathname);
function pathBox2_Callback(hObject, eventdata, handles)% hObject handle to pathBox2 (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 pathBox2 as text% str2double(get(hObject,'String')) returns contents of pathBox2 as adouble
7/27/2019 codeBT2
5/10
% --- Executes during object creation, after setting all properties.function pathBox2_CreateFcn(hObject, eventdata, handles)% hObject handle to pathBox2 (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 Decode.function Decode_Callback(hObject, eventdata, handles)% hObject handle to Decode (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)Mess=get(handles.InputText,'String');fname=get(handles.pathBox2,'String'); % chon file da encode trong pathBox2fstream= fileread(fname)Bin=RutTrich(fstream);Message=ConvertToText(Bin);set(handles.OutputText,'String',Message);msgbox('Encode Xong');
%set(handles.OutputText,'String',Mess);
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)% hObject handle to pushbutton7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)
ConvertToBinary
function [ binStr ] = ConvertToBinary( str )
binStr = '';
for i = 1: length(str)
disp(dec2bin(str(i)));
binStr = strcat(binStr, dec2bin(str(i),8));
end
7/27/2019 codeBT2
6/10
end
ConvertToText
function [ str ] = ConvertToText( binStr )
str = '';
len = length(binStr);
len = len / 8;
for i = 1: len
t = '';
for j = (i-1)*8+1: i*8
t = strcat(t, binStr(j));
end
t = char(bin2dec(t));
if t == ' '
t = '-';
end
str = strcat(str, t);
end
for i = 1: length(str)
if str(i) == '-'
str(i) = ' ';
end
end
end
7/27/2019 codeBT2
7/10
Nhung
function [ embeded ] = Nhung( cover , secret )
embeded = 'e';
remain=cover;
k=1;
while true
[token1, remain] = strtok(remain, ' ')
[token2, remain] = strtok(remain, ' ')
if( strcmp(secret(k),'0'))
temp=[token1 ' ' token2 ' e']
embeded = [embeded(1:end-1), temp]
end
if( strcmp(secret(k),'1'))
temp = [token1 ' ' token2 ' e'] % matlab k cho php 1 string c blank sau ?t nn thm e vo, lt g? e
ra!
embeded = [embeded(1:end-1), temp] % dng strcat n b? kho?ng tr?ng sau embeded(1:end-1)
xong ghp temp c?ng nh? k
end
k= k + 1;
if(k>length(secret)) % nhng h?t n?i dung r?i!
7/27/2019 codeBT2
8/10
break;
end;
end; %ket thuc while
embeded=[embeded(1:end-1), remain]
end %function
filewrite
function filewrite(myfile, mytext)
fid = fopen(myfile,'w');
fprintf(fid, '%s', mytext);
fclose(fid);
RutTrich
function [secret] = RutTrich( embeded )
secret='';
a=0; % 3 khoang trang a b c
b=0;
i=1;
while true
ei=embeded(i);
ek=embeded(i+1);
if strcmp(ei,' ') && strcmp(ek,' ')
if (a==0) a=a+2; end;
7/27/2019 codeBT2
9/10
if (a==1) b=b+2; end ;
i=i+1;
end
if strcmp(ei,' ') && not(strcmp(ek,' '))
if(a==1) % vua doc 1 blank rieng truoc do roi, them nay nua la 2 blank ddown- ket thuc
break;
end;
if(a==0) a=a+1; end;
if(a==2) b=b+1; end;
end
if a+b==3;
if (a==1)
secret=[secret '0']
else
secret=[secret '1']
end;
a=0;
b=0;
end
i=i+1; % BREAK n?a
end
7/27/2019 codeBT2
10/10
end