已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
恰好我有。能運行的,C語言的。 #include #include aes.h #include commonage.h #define byte unsigned char #define BPOLY 0x1b /! Lower 8 bits of (x8+x4+x3+x+1), ie. (x4+x3+x+1). #define BLOCKSIZE 16 /! Block size in number of bytes. #define KEYBITS 128 /! Use AES128. #define ROUNDS 10 /! Number of rounds. #define KEYLENGTH 16 /! Key length in number of bytes. byte xdata block1 256 ; /! Workspace 1. byte xdata block2 256 ; /! Worksapce 2. byte xdata * powTbl; /! Final location of exponentiation lookup table. byte xdata * logTbl; /! Final location of logarithm lookup table. byte xdata * sBox; /! Final location of s-box. byte xdata * sBoxInv; /! Final location of inverse s-box. byte xdata * expandedKey; /! Final location of expanded key. void CalcPowLog( byte * powTbl, byte * logTbl ) byte xdata i = 0; byte xdata t = 1; do / Use 0x03 as root for exponentiation and logarithms. powTbli = t; logTblt = i; i+; / Muliply t by 3 in GF(28). t = (t 1) (t & 0x80 ? BPOLY : 0); while( t != 1 ); / Cyclic properties ensure that i 0 ) temp = powTbl 255 - logTbli ; else temp = 0; / Affine transformation in GF(2). result = temp 0x63; / Start with adding a vector in GF(2). for( rot = 0; rot 4; rot+ ) / Rotate left. temp = (temp7); / Add rotated byte in GF(2). result = temp; / Put result in table. sBoxi = result; while( +i != 0 ); void CalcSBoxInv( byte * sBox, byte * sBoxInv ) byte xdata i = 0; byte xdata j = 0; / Iterate through all elements in sBoxInv using i. do / Search through sBox using j. cleardog(); do / Check if current j is the inverse of current i. if( sBox j = i ) / If so, set sBoxInc and indicate search finished. sBoxInv i = j; j = 255; while( +j != 0 ); while( +i != 0 ); void CycleLeft( byte * row ) / Cycle 4 bytes in an array left once. byte xdata temp = row0; row0 = row1; row1 = row2; row2 = row3; row3 = temp; void InvMixColumn( byte * column ) byte xdata r0, r1, r2, r3; r0 = column1 column2 column3; r1 = column0 column2 column3; r2 = column0 column1 column3; r3 = column0 column1 column2; column0 = (column0 1) (column0 & 0x80 ? BPOLY : 0); column1 = (column1 1) (column1 & 0x80 ? BPOLY : 0); column2 = (column2 1) (column2 & 0x80 ? BPOLY : 0); column3 = (column3 1) (column3 & 0x80 ? BPOLY : 0); r0 = column0 column1; r1 = column1 column2; r2 = column2 column3; r3 = column0 column3; column0 = (column0 1) (column0 & 0x80 ? BPOLY : 0); column1 = (column1 1) (column1 & 0x80 ? BPOLY : 0); column2 = (column2 1) (column2 & 0x80 ? BPOLY : 0); column3 = (column3 1) (column3 & 0x80 ? BPOLY : 0); r0 = column0 column2; r1 = column1 column3; r2 = column0 column2; r3 = column1 column3; column0 = (column0 1) (column0 & 0x80 ? BPOLY : 0); column1 = (column1 1) (column1 & 0x80 ? BPOLY : 0); column2 = (column2 1) (column2 & 0x80 ? BPOLY : 0); column3 = (column3 1) (column3 & 0x80 ? BPOLY : 0); column0 = column1 column2 column3; r0 = column0; r1 = column0; r2 = column0; r3 = column0; column0 = r0; column1 = r1; column2 = r2; column3 = r3; byte Multiply( unsigned char num, unsigned char factor ) byte mask = 1; byte result = 0; while( mask != 0 ) / Check bit of factor given by mask. if( mask & factor ) / Add current multiple of num in GF(2). result = num; / Shift mask to indicate next bit. mask = 1; / Double num. num = (num 1) (num & 0x80 ? BPOLY : 0); return result; byte DotProduct( unsigned char * vector1, unsigned char * vector2 ) byte result = 0; result = Multiply( *vector1+, *vector2+ ); result = Multiply( *vector1+, *vector2+ ); result = Multiply( *vector1+, *vector2+ ); result = Multiply( *vector1 , *vector2 ); return result; void MixColumn( byte * column ) byte xdata row8 = 0x02, 0x03, 0x01, 0x01, 0x02, 0x03, 0x01, 0x01 ; / Prepare first row of matrix twice, to eliminate need for cycling. byte xdata result4; / Take dot products of each matrix row and the column vector. result0 = DotProduct( row+0, column ); result1 = DotProduct( row+3, column ); result2 = DotProduct( row+2, column ); result3 = DotProduct( row+1, column ); / Copy temporary result to original column. column0 = result0; column1 = result1; column2 = result2; column3 = result3; void SubBytes( byte * bytes, byte count ) do *bytes = sBox *bytes ; / Substitute every byte in state. bytes+; while( -count ); void InvSubBytesAndXOR( byte * bytes, byte * key, byte count ) do / *bytes = sBoxInv *bytes *key; / Inverse substitute every byte in state and add key. *bytes = block2 *bytes *key; / Use block2 directly. Increases speed. bytes+; key+; while( -count ); void InvShiftRows( byte * state ) byte temp; / Note: State is arranged column by column. / Cycle second row right one time. temp = state 1 + 3*4 ; state 1 + 3*4 = state 1 + 2*4 ; state 1 + 2*4 = state 1 + 1*4 ; state 1 + 1*4 = state 1 + 0*4 ; state 1 + 0*4 = temp; / Cycle third row right two times. temp = state 2 + 0*4 ; state 2 + 0*4 = state 2 + 2*4 ; state 2 + 2*4 = temp; temp = state 2 + 1*4 ; state 2 + 1*4 = state 2 + 3*4 ; state 2 + 3*4 = temp; / Cycle fourth row right three times, ie. left once. temp = state 3 + 0*4 ; state 3 + 0*4 = state 3 + 1*4 ; state 3 + 1*4 = state 3 + 2*4 ; state 3 + 2*4 = state 3 + 3*4 ; state 3 + 3*4 = temp; void ShiftRows( byte * state ) byte temp; / Note: State is arranged column by column. / Cycle second row left one time. temp = state 1 + 0*4 ; state 1 + 0*4 = state 1 + 1*4 ; state 1 + 1*4 = state 1 + 2*4 ; state 1 + 2*4 = state 1 + 3*4 ; state 1 + 3*4 = temp; / Cycle third row left two times. temp = state 2 + 0*4 ; state 2 + 0*4 = state 2 + 2*4 ; state 2 + 2*4 = temp; temp = state 2 + 1*4 ; state 2 + 1*4 = state 2 + 3*4 ; state 2 + 3*4 = temp; / Cycle fourth row left three times, ie. right once. temp = state 3 + 3*4 ; state 3 + 3*4 = state 3 + 2*4 ; state 3 + 2*4 = state 3 + 1*4 ; state 3 + 1*4 = state 3 + 0*4 ; state 3 + 0*4 = temp; void InvMixColumns( byte * state ) InvMixColumn( state + 0*4 ); InvMixColumn( state + 1*4 ); InvMixColumn( state + 2*4 ); InvMixColumn( state + 3*4 ); void MixColumns( byte * state ) MixColumn( state + 0*4 ); MixColumn( state + 1*4 ); MixColumn( state + 2*4 ); MixColumn( state + 3*4 ); void XORBytes( byte * bytes1, byte * bytes2, byte count ) do *bytes1 = *bytes2; / Add in GF(2), ie. XOR. bytes1+; bytes2+; while( -count ); void CopyBytes( byte * to, byte * from, byte count ) do *to = *from; to+; from+; while( -count ); void KeyExpansion( byte * expandedKey ) byte xdata temp4; byte i; byte xdata Rcon4 = 0x01, 0x00, 0x00, 0x00 ; / Round constant. unsigned char xdata *key; unsigned char xdata a16; key=a; /以下為加解密密碼,共16字節(jié)??梢赃x擇任意值 key0=0x30; key1=0x30; key2=0x30; key3=0x30; key4=0x30; key5=0x30; key6=0x30; key7=0x30; key8=0x30; key9=0x30; key10=0x30; key11=0x30; key12=0x30; key13=0x30; key14=0x30; key15=0x30; / / Copy key to start of expanded key. i = KEYLENGTH; do *expandedKey = *key; expandedKey+; key+; while( -i ); / Prepare last 4 bytes of key in temp. expandedKey -= 4; temp0 = *(expandedKey+); temp1 = *(expandedKey+); temp2 = *(expandedKey+); temp3 = *(expandedKey+); / Expand key. i = KEYLENGTH; while( i BLOCKSIZE*(ROUNDS+1) ) / Are we at the start of a multiple of the key size? if( (i % KEYLENGTH) = 0 ) CycleLeft( temp ); / Cycle left once. SubBytes( temp, 4 ); / Substitute each byte. XORBytes( temp, Rcon, 4 ); / Add constant in GF(2). *Rcon = (*Rcon 24 / Are we right past a block size? else if( (i % KEYLENGTH) = BLOCKSIZE ) SubBytes( temp, 4 ); / Substitute each byte. #endif / Add bytes in GF(2) one KEYLENGTH away. XORBytes( temp, expandedKey - KEYLENGTH, 4 ); / Copy result to current 4 bytes. *(expandedKey+) = temp 0 ; *(expandedKey+) = temp 1 ; *(expandedKey+) = temp 2 ; *(expandedKey+) = temp 3 ; i += 4; / Next 4 bytes. void InvCipher( byte * block, byte * expandedKey ) byte round = ROUNDS-1; expandedKey += BLOCKSIZE * ROUNDS; XORBytes( block, expandedKey, 16 ); expandedKey -= BLOCKSIZE; do InvShiftRows( block ); InvSubBytesAndXOR( block, expandedKey, 16 ); expandedKey -= BLOCKSIZE; InvMixColumns( block ); while( -round ); InvShiftRows( block ); InvSubBytesAndXOR( block, expandedKey, 16 ); void Cipher( byte * block, byte * expandedKey ) /完成一個塊(16字節(jié),128bit)的加密 byte round = ROUNDS-1; XORBytes( block, expandedKey, 16 ); expandedKey += BLOCKSIZE; do SubBytes( block, 16 ); ShiftRows( block ); MixColumns( block ); XORBytes( block, expandedKey, 16 ); expandedKey += BLOCKSIZE; while( -round ); SubBytes( block, 16 ); ShiftRows( block ); XORBytes( block, expandedKey, 16 ); void aesInit( unsigned char * tempbuf ) powTbl = block1; logTbl = block2; CalcPowLog( powTbl, logTbl ); sBox = tempbuf; CalcSBox( sBox ); expandedKey = block1; /至此block1用來存貯密碼表 KeyExpansion( expandedKey ); sBoxInv = block2; / Must be block2. block2至此開始只用來存貯SBOXINV CalcSBoxInv( sBox, sBoxInv ); /對一個16字節(jié)塊解密,參數(shù)buffer是解密密緩存,chainBlock是要解密的塊 void aesDecrypt( unsigned char * buffer, unsigned char * chainBlock ) /byte xdata temp BLOCKSIZE ; /CopyBytes( temp, buffer, BLOCKSIZE ); CopyBytes(buffer,chainBlock,BLOCKSIZE); InvCipher( buffer, expandedKey ); /XORBytes( buffer, chainBlock, BLOCKSIZE ); CopyBytes( chainBlock, buffer, BLOCKSIZE ); /對一個16字節(jié)塊完成加密,參數(shù)buffer是加密緩存,chainBlock是要加密的塊 void aesEncrypt( unsigned char * buffer, unsigned char * chainBlock ) CopyBytes( buffer, chainBlock, BLOCKSIZE ); /XORBytes( buffer, chainBlock, BLOCKSIZE ); Cipher( buffer, expandedKey ); CopyBytes( chainBlock, buffer, BLOCKSIZE ); /加解密函數(shù),參數(shù)為加解密標志,要加解密的數(shù)據(jù)緩存起始指針,要加解密的數(shù)據(jù)長度(如果解密運算,必須是16的整數(shù)倍。) unsigned char aesBlockDecrypt(bit Direct,unsigned char *ChiperDataBuf,unsigned char DataLen) unsigned char xdata i; unsigned char xdata Blocks; unsigned char xdata sBoxbuf256; unsigned char xdata tempbuf16; unsigned long int xdata OrignLen=0; /未加密數(shù)據(jù)的原始長度 if(Direct=0) *(unsigned char *)&OrignLen+3)=ChiperDataBuf0; *(unsigned char *)&OrignLen+2)=ChiperDataBuf1; *(unsigned char *)&OrignLen+1)=ChiperDataBuf2; *(unsigned char *)&OrignLen)=ChiperDataBuf3; DataLen=DataLen-4; else memmove(ChiperDataBuf+4,ChiperDataBuf,DataLen); OrignLen=DataLen; ChiperDataBuf0=OrignLen; ChiperDataBuf1=OrignLen8; ChiperDataBuf2=OrignLen16; ChiperDataBuf3=Ori
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五版環(huán)保產(chǎn)業(yè)技術(shù)轉(zhuǎn)移合同范本4篇
- 年度SKI系列二甲苯異構(gòu)化催化劑產(chǎn)業(yè)分析報告
- 2024離婚導(dǎo)致的版權(quán)許可合同
- 2024年心理咨詢師題庫帶答案(b卷)
- 地下室回頂施工方案
- 滯回比較器電壓課程設(shè)計
- 《員工手冊培訓(xùn)》課件
- 二零二五年度體育賽事觀眾免責(zé)條款4篇
- 2025年度數(shù)據(jù)中心承建合同標的網(wǎng)絡(luò)安全保障3篇
- 2024銷售原油合作協(xié)議
- 中考語文非連續(xù)性文本閱讀10篇專項練習(xí)及答案
- 2022-2023學(xué)年度六年級數(shù)學(xué)(上冊)寒假作業(yè)【每日一練】
- 高中生物專題10 能量流動的過程分析及計算(原卷版)
- 法人不承擔(dān)責(zé)任協(xié)議書(3篇)
- 電工工具報價單
- 反歧視程序文件
- 油氣藏類型、典型的相圖特征和識別實例
- 流體靜力學(xué)課件
- 顧客忠誠度論文
- 實驗室安全檢查自查表
- 證券公司績效考核管理辦法
評論
0/150
提交評論