艿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)
密码学上机实验二报告(Matlab) 来自淘豆网m.daumloan.com转载请标明出处.