資源簡介
AES-FINAL.rar

代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
“““
Created?on?Tue?Nov?21?16:05:52?2017
@author:?oyjiy
@topic?:?AES_128
“““
import?data
import?s2?as?mul
#?print(data.sbox[1])
nb?=?4??????#?number?of?the?coloum?of?states
nr?=?10?????#?number?of?rounds?of?ciphering
nk?=?4??????#?length?of?the?key?(words)
def?encry(input_byte?key):
????“““
????1.?subbyte
????2.?shift?rows
????3.?mix?coloums(except?the?last?round)
????4.?add?key?for?each?round
????5.?turn?state?to?1-D?array
????“““
????state?=?[[]?for?j?in?range(4)]
????for?r?in?range(4):
????????for?c?in?range(nb):
????????????state[r].append(input_byte[r+4*c])
????
????key_schedule?=?key_expan(key)
????state?=?addkey(state?key_schedule)
????
????for?rnd?in?range(1nr):
????????#?with?mix?coloums
????????state?=?SubByte(state)
????????state?=?shift_row(state)
????????state?=?mix_col(state)
????????state?=?addkey(statekey_schedulernd)
????#?The?last?:?without?mix?coloums
????state?=?SubByte(state)
????state?=?shift_row(state)
????state?=?addkey(state?key_schedule?rnd?+?1)
????output?=?[None?for?i?in?range(4?*?nb)]
????for?r?in?range(4):
????????for?c?in?range(nb):
????????????output[r?+?4?*?c]?=?state[r][c]
????return?output
def?decrypt(cipher?key):
????state?=?[[]?for?i?in?range(nb)]
????for?r?in?range(4):
????????for?c?in?range(nb):
????????????state[r].append(cipher[r?+?4?*?c])
????
????key_schedule?=?key_expan(key)
????state?=?addkey(state?key_schedule?nr)
????
????rnd?=?nr?-?1?
????while?rnd?>=?1:
????????state?=?shift_row(state?inv=True)
????????state?=?SubByte(state?inv=True)
????????state?=?addkey(state?key_schedule?rnd)
????????state?=?mix_col(state?inv=True)
????????
????????rnd?=?rnd?-?1
????
????state?=?shift_row(state?inv=True)
????state?=?SubByte(state?inv=True)
????state?=?addkey(state?key_schedule?rnd)
????
????output?=?[None?for?i?in?range(4?*?nb)]
????for?r?in?range(4):
????????for?c?in?range(nb):
????????????output[r?+?4?*?c]?=?state[r][c]
????
????return?output
def?key_expan(key):
????key_sym?=?[ord(sym)?for?sym?in?key]
????#?key?padding?to?128?bits
????if?len(key_sym)?4?*?nk:
????????for?i?in?range(4?*?nk?-?len(key_sym)):
????????????key_sym.append(0x01)
????#?4?bytes->1?coloum->1?key_schedule
????#?init?key_scedule:?4?coloums?0~3
????key_schedule?=?[[]?for?i?in?range(4)]
????for?r?in?range(4):
????????for?c?in?range(nk):
????????????key_schedule[r].append(key_sym[r?+?4?*?c])
????
????#?expand?key_scedule:?4~43
????for?col?in?range(nk?nb?*?(nr?+?1)):
????????#?if?col?is?K4
????????if?col?%?nk?==?0:
????????????#?tmp?=?K4
????????????tmp?=?[key_schedule[row][col-1]?for?row?in?range(1?4)]
????????????tmp.append(key_schedule[0][col-1])
????????????
????????????#?byte?substitution?using?sbox
????????????for?j?in?range(len(tmp)):???????????#?K4[0]=0xa4
????????????????sbox_row?=?tmp[j]?//?0x10???????#?sbox_row?=?a????整除16
????????????????sbox_col?=?tmp[j]?%?0x10??
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????10429??2017-12-27?14:10??2015301500011-歐陽彤-密碼學課程設計\AES_128.py
?????文件???????3642??2017-11-21?16:18??2015301500011-歐陽彤-密碼學課程設計\data.py
?????文件????????535??2017-12-27?14:48??2015301500011-歐陽彤-密碼學課程設計\README.txt
?????文件??????10573??2017-12-27?14:04??2015301500011-歐陽彤-密碼學課程設計\s2.py
?????文件???????4167??2017-12-19?14:42??2015301500011-歐陽彤-密碼學課程設計\test.py
?????文件?????????16??2017-11-30?17:08??2015301500011-歐陽彤-密碼學課程設計\zcon.txt
?????文件?????474839??2017-12-27?14:45??2015301500011-歐陽彤-密碼學課程設計\2015301500011-歐陽彤-密碼學課程設計.docx
?????目錄??????????0??2017-12-27?14:47??2015301500011-歐陽彤-密碼學課程設計
?????文件?????149076??2017-10-01?11:24??2015301500011-歐陽彤-密碼學課程設計\測試例子\cat.jpg
?????文件?????149076??2017-12-27?14:11??2015301500011-歐陽彤-密碼學課程設計\測試例子\crypted_cat.jpg
?????文件?????149076??2017-12-27?14:11??2015301500011-歐陽彤-密碼學課程設計\測試例子\decrypted_crypted_cat.jpg
?????目錄??????????0??2017-12-27?14:45??2015301500011-歐陽彤-密碼學課程設計\測試例子
-----------?---------??----------?-----??----
???????????????951429????????????????????12
- 上一篇:簡易步進電機S加減速
- 下一篇:AC轉DC電源的電路設計
評論
共有 條評論