编者按:作业选登暂时告一段落,有太多的好作业,目前才选登到2016下半年(还有5个学期的作业),每学期60人选课,来自20个学院,一个学院选一个,一学期的作业也要20天。本学期课程已结束,作业都已完成,下一学期对新班级再接着发。今天选登本学期新收到的作业。作为压轴,今天的作业具有东大水平,课程成绩为优加星。作为18级东大新生,才上了11周的大学课程,应用能力可见一斑。

零、感想&附加说明

一学期的Matlab课程学习结束了,从计算机语言角度学习Matlab其实并不算太难(不过对于刚进入大一的我还是有点挑战性的),而实践永远是能力提升的好方法,所以Matlab最后的大作业确实是让我收益颇多的。

我是一名网络空间安全学院的学生,本学期学生研讨课上,老师们介绍了许多相关的概论知识,其中多次提到了密码安全。信息时代账户密码是不可缺少的,我们在许多场景都会设置密码、使用密码,因而安全的密码对于信息安全是十分必要的。这也正是我设计这个程序的出发点。如何起一个安全的密码?首先要保障其长度、复杂度,同时为了方便记忆,一定的规律性也是必要的。上研讨课时老师就介绍过可以用一句自己喜欢的英语句子的首字母拼成密码,这点被我记了下来。而这个程序中使用的截取诗文首字母的方法,其实道理是相似的,莎翁的十四行诗又是格式规范统一的14行句子,所以截取也相对方便。另一方面,密码需要复杂度,只是单纯的截取诗文首字母,得到的则只是一堆大写字母,不够复杂。于是,引入了字符替换机制,既要有小写字母,又要有其他符号,最后四位用数字记录日期,这样就足够复杂而无序了!但是在熟悉密码的人、生成密码的人看来,这又是很容易写出来的(即使忘记了,只要根据助记语句和替换规律,也可以从相应诗篇中还原密码)。另外,在东大这样一所以工科见长的高校,多读读诗增加些人文气息也是不错的~

信息的隐藏与还原部分,其实也是新生研讨课带来的灵感(信息隐藏与数字水印)。LSB算法的部分其实算是借鉴了网上的一些开源版本,再做一些调整修改的。这部分主要是希望实现密码的备份与还原,可以以相对安全又比较稳定的方式保存密码。

最后,整个程序的编写其实还是花了不少时间的,因为这个过程需要不断补充一些必要的知识,但是它并不枯燥,反而是充实有挑战性的,因而编写的时候更多是乐此不疲地学习实践,编完了则是很有成就感的一件事。或许程序还有可以改进的地方,但是时间有限,也很难尽善尽美。不过对我而言,这样一个过程,已经很满足了。

一、简单说明

如题,这个程序主要有三部分:诗文密码生成、信息隐藏、信息还原。

密码生成:从有格式的诗文中截取密码(这里选用莎士比亚的十四行诗,因为格式比较整齐统一),并按一定原则进行字符替换增加复杂度。

信息隐藏:将密码信息写入图片(肉眼无法识别),以便备份。

信息还原:从隐藏信息的图片中还原信息(生成的密码)。

二、程序原理

密码生成:读取莎翁的十四行诗文件,从中选一篇,截取首字母,再按一定原则进行字符替换(详见《替换规则.txt》)。

信息隐藏:利用LSB算法,将信息写入图片。

信息提取:LSB算法提取图中信息。

三、使用说明

Part I 密码生成与信息隐藏

1、用matlab运行gui.m文件

2、设置-手动选择诗文

1)这一步默认应该是可以缺省的(前提:文件《Shakespeare.txt》在文件夹中)。

2)如果跳过此步报错,则可以手动设置一下试试。

此步用于获取诗文,并将自动生成密码与助记提示语。

PS:如果需要,可以点击下方的保存诗文,则会生成相应的txt文件,如前如示。

5、选择【信息写入】

6、选择【保存图片】

生成相应的已被写入密码信息的图片。

Part II 还原保存的密码

1、菜单栏中【工具-信息还原器】

Ps:直接运行gui_decryptor.m亦可

2、击【选择图片】

选择相应的被写入信息的图片,然后会呈现在程序中。

将隐藏的密码提取出来。

补充:文件的退出

方式相同,都是点击菜单栏【文件】-【退出】

四、主程序设计界面(gui.fig)

五、主程序代码(gui.m)

function varargout = gui(varargin)

% GUI MATLAB code for gui.fig

%      GUI, by itself, creates a new GUI or raises the existing

%      singleton*.

%      H = GUI returns the handle to a new GUI or the handle to

%      the existing singleton*.

%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in GUI.M with the given input arguments.

%      GUI('Property','Value',...) creates a new GUI or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before gui_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to gui_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 gui

% Last Modified by GUIDE v2.5 09-Dec-2018 11:39:56

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

'gui_Singleton',  gui_Singleton, ...

'gui_OpeningFcn', @gui_OpeningFcn, ...

'gui_OutputFcn',  @gui_OutputFcn, ...

'gui_LayoutFcn',  [] , ...

'gui_Callback',   []);

if nargin && ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1});

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

gui_mainfcn(gui_State, varargin{:});

% End initialization code - DO NOT EDIT

% --- Executes just before gui is made visible.

function gui_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 gui (see VARARGIN)

% Choose default command line output for gui

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

%隐藏控件

set(handles.axes1,'visible','off');

set(handles.axes2,'visible','off');

setappdata(gcf,'textName','Shakespeare.txt');

setappdata(gcf,'if_original_image_file',0);

% UIWAIT makes gui wait for user response (see UIRESUME)

% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.

function varargout = gui_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)

[filename,pathname,filter] = uigetfile({'*.bmp;*.png'},'Please select the picture used to be hidden the code.');

if filter == 0

str = fullfile(pathname,filename);

setappdata(gcf,'original_image_file',str);

setappdata(gcf,'if_original_image_file',1);

axes(handles.axes1);

I=imread(str);

% set I to a globally available variable, and hold it with function setappdata

setappdata(0,'I',I);

%set(handles.axes1,'visible','on');

%会产生坐标,先注释掉,如果图片不可见,可打开

function edit1_Callback(hObject, eventdata, handles)

% hObject    handle to text1 (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 text1 as text

%        str2double(get(hObject,'String')) returns contents of text1 as a double

% --- Executes during object creation, after setting all properties.

function text1_CreateFcn(hObject, eventdata, handles)

% hObject    handle to text1 (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');

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

textName=getappdata(gcf,'textName');

num=randi([1,50]);

[password,hint,poem,poem2,poemSav]=Password_Generator(textName,num);

setappdata(gcf,'password',password);

setappdata(gcf,'hint',hint);

setappdata(gcf,'poem',poem);

setappdata(gcf,'poem2',poem2);

setappdata(gcf,'poemSav',poemSav);

set(handles.text1,'string',poem);

set(handles.edit2,'string',hint);

set(handles.edit3,'string',password);

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

password=getappdata(gcf,'password');

imageOpen=getappdata(gcf,'if_original_image_file');

if(imageOpen==1)

name = getappdata(gcf,'original_image_file'); %已经打开图片,则直接写入

[filename,pathname,filter] = uigetfile({'*.bmp;*.png'},'Please select the picture used to be hidden the code.'); %未选择图片,先打开图片

if filter == 0

name = fullfile(pathname,filename);

origin_image=imread(name);

axes(handles.axes1);

imshow(origin_image);

setappdata(gcf,'if_original_image_file',1); %已打开图片

setappdata(gcf,'original_image_file',name);

[picName, image_result] = LSB_embed(name, password, 2, 3);

axes(handles.axes2);

imshow(image_result);

%set(handles.axes2,'visible','on');

%会产生坐标,先注释掉,如果图片不可见,可打开

setappdata(gcf,'picName',picName);

setappdata(gcf,'image_result',image_result);

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');

function edit3_Callback(hObject, eventdata, handles)

% hObject    handle to edit3 (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 edit3 as text

%        str2double(get(hObject,'String')) returns contents of edit3 as a double

% --- Executes during object creation, after setting all properties.

function edit3_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit3 (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');

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

outputName=getappdata(gcf,'picName');

image_result=getappdata(gcf,'image_result');

imwrite(image_result, outputName);

% --- Executes on button press in pushbutton6.

function pushbutton6_Callback(hObject, eventdata, handles)

% hObject    handle to pushbutton6 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

poemSav=getappdata(gcf,'poemSav');

A=getappdata(gcf,'poem2');

fid=fopen(poemSav,'wt');

for k = 1:size(A,1)

fprintf(fid,'%s %s %s %s %s %s %s %s %s %s %s\n',A(k,1),A(k,2),A(k,3),A(k,4),A(k,5),A(k,6),A(k,7),A(k,8),A(k,9),A(k,10),A(k,11));

fclose(fid);

% --------------------------------------------------------------------

function menu_1_Callback(hObject, eventdata, handles)

% hObject    handle to menu_1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------

function menu_2_Callback(hObject, eventdata, handles)

% hObject    handle to menu_2 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------

function submenu_2_1_Callback(hObject, eventdata, handles)

% hObject    handle to submenu_2_1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------

function submenu_2_2_Callback(hObject, eventdata, handles)

% hObject    handle to submenu_2_2 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

run('gui_decryptor.m');

%set(hObject,'checked','on');

% --------------------------------------------------------------------

function Untitled_1_Callback(hObject, eventdata, handles)

% hObject    handle to Untitled_1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------

function menu_3_Callback(hObject, eventdata, handles)

% hObject    handle to menu_3 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------

function submenu_3_1_Callback(hObject, eventdata, handles)

% hObject    handle to submenu_3_1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

[FileName,PathName,FilterIndex] = uigetfile('*.txt','Open the textfile','Shakespeare.txt');

textName = fullfile(PathName,FileName);

if FilterIndex == 0

setappdata(gcf,'textName',textName);

% --------------------------------------------------------------------

function submenu_1_1_Callback(hObject, eventdata, handles)

% hObject    handle to submenu_1_1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

button=questdlg('确定要退出吗?','退出确认','是','否','是');

if strcmp(button,'是')

六、密码还原程序界面(gui_decryptor.fig)

七、密码还原程序代码(gui_decryptor.m)

function varargout = gui_decryptor(varargin)

% GUI_DECRYPTOR MATLAB code for gui_decryptor.fig

%      GUI_DECRYPTOR, by itself, creates a new GUI_DECRYPTOR or raises the existing

%      singleton*.

%      H = GUI_DECRYPTOR returns the handle to a new GUI_DECRYPTOR or the handle to

%      the existing singleton*.

%      GUI_DECRYPTOR('CALLBACK',hObject,eventData,handles,...) calls the local

%      function named CALLBACK in GUI_DECRYPTOR.M with the given input arguments.

%      GUI_DECRYPTOR('Property','Value',...) creates a new GUI_DECRYPTOR or raises the

%      existing singleton*.  Starting from the left, property value pairs are

%      applied to the GUI before gui_decryptor_OpeningFcn gets called.  An

%      unrecognized property name or invalid value makes property application

%      stop.  All inputs are passed to gui_decryptor_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 gui_decryptor

% Last Modified by GUIDE v2.5 09-Dec-2018 11:44:46

% Begin initialization code - DO NOT EDIT

gui_Singleton = 1;

gui_State = struct('gui_Name',       mfilename, ...

'gui_Singleton',  gui_Singleton, ...

'gui_OpeningFcn', @gui_decryptor_OpeningFcn, ...

'gui_OutputFcn',  @gui_decryptor_OutputFcn, ...

'gui_LayoutFcn',  [] , ...

'gui_Callback',   []);

if nargin && ischar(varargin{1})

gui_State.gui_Callback = str2func(varargin{1});

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

gui_mainfcn(gui_State, varargin{:});

% End initialization code - DO NOT EDIT

% --- Executes just before gui_decryptor is made visible.

function gui_decryptor_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 gui_decryptor (see VARARGIN)

% Choose default command line output for gui_decryptor

handles.output = hObject;

% Update handles structure

guidata(hObject, handles);

set(handles.axes1,'visible','off');

setappdata(gcf,'if_original_image_file',0);

% UIWAIT makes gui_decryptor wait for user response (see UIRESUME)

% uiwait(handles.figure1);

% --- Outputs from this function are returned to the command line.

function varargout = gui_decryptor_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)

[filename,pathname,filter] = uigetfile({'*.bmp;*.png'},'Please select the picture used to be hidden the code.');

if filter == 0

str = fullfile(pathname,filename);

setappdata(gcf,'original_image_file',str);

setappdata(gcf,'if_original_image_file',1);

axes(handles.axes1);

I=imread(str);

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

imageOpen=getappdata(gcf,'if_original_image_file');

if(imageOpen==1)

name = getappdata(gcf,'original_image_file'); %已经打开图片,则直接写入

[filename,pathname,filter] = uigetfile({'*.bmp;*.png'},'Please select the picture used to be hidden the code.'); %未选择图片,先打开图片

if filter == 0

name = fullfile(pathname,filename);

origin_image=imread(name);

axes(handles.axes1);

imshow(origin_image);

setappdata(gcf,'if_original_image_file',1); %已打开图片

setappdata(gcf,'original_image_file',name);

password = LSB_extract(name, 2, 3);

set(handles.edit1,'string',password);

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');

% --------------------------------------------------------------------

function Menu_1_Callback(hObject, eventdata, handles)

% hObject    handle to Menu_1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

% --------------------------------------------------------------------

function submenu_1_1_Callback(hObject, eventdata, handles)

% hObject    handle to submenu_1_1 (see GCBO)

% eventdata  reserved - to be defined in a future version of MATLAB

% handles    structure with handles and user data (see GUIDATA)

button=questdlg('是否确认退出','退出确认','是','否','是');

if strcmp(button,'是')

八、子函数(LSB_embed.m)

function [picName, image_result] = LSB_embed(name, message, lsb, color)

% LSBembed(name, message, lsb)  LSB in steganography (embed)

% name: the picture's path and name

% message: the data you want to hide in the picture

% lsb: lsb-rightmost LSBs

% color: 1-red, 2-green, 3-blue

% PS:Based on Moming's version

outputName='result';

outputName=strcat(outputName,'.png');

image = imread(name);

msg_origin = unicode2native(strcat(message, char(4)), 'UTF-8');  % UTF-8 encode, 'EOT' is the end tag

msg_bin = dec2bin(msg_origin, 8);  % convert to binary

msg = strjoin(cellstr(msg_bin)','');

len = length(msg) / lsb;

while len ~= fix(len)

strcat(msg, char(4));

len = length(msg) / lsb;

tmp = blanks(len);

for i = 1 : len

tmp(i) = char(bin2dec(msg((i - 1) * lsb + 1 : i * lsb)) + '0');  % '0' is a kind of placeholder

% use Red, Green or Blue

layer = image(:, :, color);

for i = 1 : len

layer(i) = layer(i) - mod(layer(i), 2^lsb) + double(tmp(i) - '0');  % only to be consistent with front

% save the picture

image_result = image;

image_result(:, :, color) = layer;

%imshow(image_result);

%imwrite(image_result, outputName);  % jpg would lose some information

picName=outputName;

九、子函数(LSB_extract.m)

function [msg_origin] = LSB_extract(name, lsb, color)

% LSB_extract(name, lsb)  LSB in steganography (extract)

% name: the picture's path and name

% lsb: lsb-rightmost LSBs

% color: 1-red, 2-green, 3-blue

% PS:Based on Moming's version

image = imread(name);

layer = image(:, :, color);

tmp = blanks(0);

n = numel(layer);

% if lsb ~= 2, then you need to change something below

for i = 1 : n * lsb / 8

tmp((i - 1) * 4 + 1 : i * 4) = mod(layer((i - 1) * 4 + 1 : i * 4), 2^lsb);

msg((i - 1) * 8 + 1 : i * 8) = dec2bin(tmp((i - 1) * 4 + 1 : i * 4), lsb)';

msg_origin(i) = bin2dec(msg((i - 1) * 8 + 1 : i * 8));

if msg_origin(i) == 4  % EOT is the end tag

msg_origin = native2unicode(msg_origin,'UTF-8');

msg_origin = msg_origin(1:end-1);

十、子函数(Password_Generator.m)

function[password,hint,poem,poem2,poemSav]=Password_Generator(textname,num)

%确定生成密码的时间

t=datetime;

year=num2str(t.Year);

month=num2str(t.Month);

if t.Month<10

month=[num2str(0),num2str(t.Month)];

day=num2str(t.Day);

if t.Day<10

day=[num2str(0),num2str(t.Day)];

date=strcat(year,"年",month,"月",day,"日");

%找到诗所在的行数

beginLine=(num-1)*15+1;

endLine=num*15;

%从诗集中截取诗并生成密码

poemSav="the poem";

poemSav=strcat(poemSav,year,month,day,".txt");

phns1=textname;

fileID=fopen(phns1);

C = textscan(fileID,'%s %s %s %s %s %s %s %s %s %s %s');

fclose(fileID);

r=0; %从0开始的计数器,而密码从r>=1的行数开始截取

for i =beginLine:1:endLine

for j =1:1:11

at=string(C{1,j}{i,1});

At=[At,at];

pwt=char(At(1,1));

new_str=[];

switch r %按一定规则替换字符,提高密码安全性

case {2,4,7,10}

new_str=lower(a);

new_str='!';

new_str='@';

new_str='#';

new_str=month(1);

new_str=month(2);

new_str=day(1);

new_str=day(2);

pw=strcat(pw,new_str);

password=pw;

%生成密码助记提示语

hintStr1="在";hintStr2=",你读了莎士比亚十四行诗的第";hintStr3="首诗。";