Forpersonaluseonlyinstudyandresearch;:00-21:00羂实验楼102薁指导教师:候书会芀实验名称:芅1、上机实验二蚄实验内容:荿实验四现代对称密码学实验(验证性实验,4学时)莀AES蚅 A、实验内容:熟悉AES算法,并实现AES;膂 B、测试数据:任意,并验证加/:肆程序代码如下所示:袄膁functionplaintext=inv_cipher(ciphertext,w,inv_s_box,inv_poly_mat,vargin)薀蒇ifnargin>0节袀%Switchtheverbosemodeflagon虿verbose_mode=1;袈肄%Ifthereisnooptional"verbosemode"argument羃else蝿肅%Switchtheverbosemodeflagoff螆verbose_mode=0;蚂蝿end蒆膄%Iftheinputvectorisacellarrayordoesnothave16elements蒁ifiscell(ciphertext)|prod(size(ciphertext))~=16衿袇%Informuserandabort袆error('Ciphertexthastobeavector(notacellarray)with16elements.')芀罿end芈莃%Ifanyelementoftheinputvectorcannotberepresentedby8bits节ifany(ciphertext<0|ciphertext>255)聿莄%Informuserandabort肅error('Elementsofciphertextvectorhavetobebytes(0<=ciphertext(i)<=255).')肁腿end螅薃%Iftheexpandedkeyarrayisacellarrrayordoesnothavethecorrectsize螀ifiscell(w)|any(size(w)~=[44,4])艿膆%Informuserandabort芅error('whastobeanarray(notacellarray)with[44x4]elements.')袃莈end薇蚃ifany(w<0|w>255)薂莈%Informuserandabort羈error('Elementsofkeyarraywhavetobebytes(0<=w(i,j)<=255).')蒅莁end蒈肅state=reshape(ciphertext,4,4);袃膀%Displayintermediateresultifrequested薈ifverbose_mode蒆disp_hex('Initialstate:',state)薅end膃蚈round_key=(w(41:44,:))';袇肂%Displayintermediateresultifrequested羁ifverbose_mode螈disp_hex('Initialroundkey:',round_key)芇end螄螀%Add(xor)thecurrentroundkey(matrix)tothestate(matrix)袈state=add_round_key(state,round_key);蚈膂%Loopover9roundsbackwards螃fori_round=9:-1:1袈袅%Displayintermediateresultifrequested羄ifverbose_mode薂disp_hex(['Stateatstartofround',num2str(i_round),':'],state)羇end芆蚆%Cyclicallyshiftthelastthreerowsofthestatematrix芁state=inv_shift_rows(state);肇蚇%Displayintermediateresultifrequested肄ifverbose_mode肀disp_hex('Afterinv_shift_rows:',state)膇end肈state=sub_bytes(state,inv_s_box);袆肃%Displayintermediateresultifrequested芇ifverbose_mode膅disp_hex('Afterinv_sub_bytes:',state)芄end袂莇%Extractthecurrentroundkey(4x4matrix)fr
密码学上机实验二报告(Matlab) 来自淘豆网m.daumloan.com转载请标明出处.