python2不导入任何外部包实现AES加密
实现的是AES加密的ECB模式,目前只支持16位的明文和16位的密匙进行加密
参考文章 AES算法原理参考: https://blog.csdn.net/qq_28205153/article/details/55798628 AES 列混淆 GF(2^8) 有限域上的乘法: https://blog.csdn.net/qq_43694952/article/details/110522437 python水平不高,代码比较烂,请谅解
S_table1=[[0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76],[0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0],[0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15],[0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75],[0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84],[0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf],[0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8],[0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2],[0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73],[0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb],[0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79],[0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08],[0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a],[0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e],[0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf],[0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16]]
S_table2={'00':'63' , '01':'7c' , '02':'77' , '03':'7b' , '04':'f2' , '05':'6b' , '06':'6f' , '07':'c5' , '08':'30' , '09':'01' , '0a':'67' , '0b':'2b' , '0c':'fe' , '0d':'d7' , '0e':'ab' , '0f':'76' , '10':'ca' , '11':'82' , '12':'c9' , '13':'7d' , '14':'fa' , '15':'59' , '16':'47' , '17':'f0' , '18':'ad' , '19':'d4' , '1a':'a2' , '1b':'af' , '1c':'9c' , '1d':'a4' , '1e':'72' , '1f':'c0' , '20':'b7' , '21':'fd' , '22':'93' , '23':'26' , '24':'36' , '25':'3f' , '26':'f7' , '27':'cc' , '28':'34' , '29':'a5' , '2a':'e5' , '2b':'f1' , '2c':'71' , '2d':'d8' , '2e':'31' , '2f':'15' , '30':'04' , '31':'c7' , '32':'23' , '33':'c3' , '34':'18' , '35':'96' , '36':'05' , '37':'9a' , '38':'07' , '39':'12' , '3a':'80' , '3b':'e2' , '3c':'eb' , '3d':'27' , '3e':'b2' , '3f':'75' , '40':'09' , '41':'83' , '42':'2c' , '43':'1a' , '44':'1b' , '45':'6e' , '46':'5a' , '47':'a0' , '48':'52' , '49':'3b' , '4a':'d6' , '4b':'b3' , '4c':'29' , '4d':'e3' , '4e':'2f' , '4f':'84' , '50':'53' , '51':'d1' , '52':'00' , '53':'ed' , '54':'20' , '55':'fc' , '56':'b1' , '57':'5b' , '58':'6a' , '59':'cb' , '5a':'be' , '5b':'39' , '5c':'4a' , '5d':'4c' , '5e':'58' , '5f':'cf' , '60':'d0' , '61':'ef' , '62':'aa' , '63':'fb' , '64':'43' , '65':'4d' , '66':'33' , '67':'85' , '68':'45' , '69':'f9' , '6a':'02' , '6b':'7f' , '6c':'50' , '6d':'3c' , '6e':'9f' , '6f':'a8' , '70':'51' , '71':'a3' , '72':'40' , '73':'8f' , '74':'92' , '75':'9d' , '76':'38' , '77':'f5' , '78':'bc' , '79':'b6' , '7a':'da' , '7b':'21' , '7c':'10' , '7d':'ff' , '7e':'f3' , '7f':'d2' , '80':'cd' , '81':'0c' , '82':'13' , '83':'ec' , '84':'5f' , '85':'97' , '86':'44' , '87':'17' , '88':'c4' , '89':'a7' , '8a':'7e' , '8b':'3d' , '8c':'64' , '8d':'5d' , '8e':'19' , '8f':'73' , '90':'60' , '91':'81' , '92':'4f' , '93':'dc' , '94':'22' , '95':'2a' , '96':'90' , '97':'88' , '98':'46' , '99':'ee' , '9a':'b8' , '9b':'14' , '9c':'de' , '9d':'5e' , '9e':'0b' , '9f':'db' , 'a0':'e0' , 'a1':'32' , 'a2':'3a' , 'a3':'0a' , 'a4':'49' , 'a5':'06' , 'a6':'24' , 'a7':'5c' , 'a8':'c2' , 'a9':'d3' , 'aa':'ac' , 'ab':'62' , 'ac':'91' , 'ad':'95' , 'ae':'e4' , 'af':'79' , 'b0':'e7' , 'b1':'c8' , 'b2':'37' , 'b3':'6d' , 'b4':'8d' , 'b5':'d5' , 'b6':'4e' , 'b7':'a9' , 'b8':'6c' , 'b9':'56' , 'ba':'f4' , 'bb':'ea' , 'bc':'65' , 'bd':'7a' , 'be':'ae' , 'bf':'08' , 'c0':'ba' , 'c1':'78' , 'c2':'25' , 'c3':'2e' , 'c4':'1c' , 'c5':'a6' , 'c6':'b4' , 'c7':'c6' , 'c8':'e8' , 'c9':'dd' , 'ca':'74' , 'cb':'1f' , 'cc':'4b' , 'cd':'bd' , 'ce':'8b' , 'cf':'8a' , 'd0':'70' , 'd1':'3e' , 'd2':'b5' , 'd3':'66' , 'd4':'48' , 'd5':'03' , 'd6':'f6' , 'd7':'0e' , 'd8':'61' , 'd9':'35' , 'da':'57' , 'db':'b9' , 'dc':'86' , 'dd':'c1' , 'de':'1d' , 'df':'9e' , 'e0':'e1' , 'e1':'f8' , 'e2':'98' , 'e3':'11' , 'e4':'69' , 'e5':'d9' , 'e6':'8e' , 'e7':'94' , 'e8':'9b' , 'e9':'1e' , 'ea':'87' , 'eb':'e9' , 'ec':'ce' , 'ed':'55' , 'ee':'28' , 'ef':'df' , 'f0':'8c' , 'f1':'a1' , 'f2':'89' , 'f3':'0d' , 'f4':'bf' , 'f5':'e6' , 'f6':'42' , 'f7':'68' , 'f8':'41' , 'f9':'99' , 'fa':'2d' , 'fb':'0f' , 'fc':'b0' , 'fd':'54' , 'fe':'bb' , 'ff':'16'}
Rcon=[0x01000000,0x02000000,0x04000000,0x08000000,0x10000000,0x20000000,0x40000000,0x80000000,0x1b000000,0x36000000]
def T(code,i):
to_S_talbe={}
to_S_talbe[i]=""
code=code[2:]+code[:2]
for x in range(4):
to_S_talbe[i]+=S_table2[code[2*x:2*x+2]]
s=int(to_S_talbe[i],16)^Rcon[(i-4)/4]
if "L" in hex(s):
return hex(s)[2:-1]
else:
return hex(s)[2:]
def extkey(key_hex):
W={}
key_result=[]
for x in range(4):
W[x]=key_hex[8*x:8*x+8]
for i in range(4,44):
if i%4==0:
W[i]=hex(int(W[i-4],16)^int(T(W[i-1],i),16))[2:].rjust(8,"0")
if "L" in W[i]:
W[i]=hex(int(W[i-4],16)^int(T(W[i-1],i),16))[2:-1].rjust(8,"0")
else:
W[i]=hex(int(W[i-4],16)^int(W[i-1],16))[2:].rjust(8,"0")
if "L" in W[i]:
W[i]=hex(int(W[i-4],16)^int(W[i-1],16))[2:-1].rjust(8,"0")
for x in range(11):
key_result.append(W[4*x]+W[4*x+1]+W[4*x+2]+W[4*x+3])
return key_result
def exbyte(data_hex):
new_data=''
for x in range(0,len(data_hex),2):
new_data+=S_table2[data_hex[x:x+2]]
return new_data
def h_move(data_hex):
matrix_list=[0,5,10,15,4,9,14,3,8,13,2,7,12,1,6,11]
new_data=""
for x in matrix_list:
new_data+=data_hex[2*x:2*x+2]
return new_data
def xtime(a,count):
for i in range(0,count):
if a>>7==1:
a= (0xff&(a<<1))^0x1b
else:
a= a<<1
return a
def GF2multi(b,a):
if isinstance(a,int)==False:
a=int(a,16)
index_of_1_list=[]
count=0
while b!=0:
if b&0x1==1:
index_of_1_list.append(count)
count+=1
b=b>>1
xtime_result_list=[]
for i in index_of_1_list:
xtime_result_list.append(xtime(a,i))
res = xtime_result_list[0]
for i in range(1,len(xtime_result_list)):
res=res^xtime_result_list[i]
return res
def column_mix(data_hex):
matrix_t=[[2,1,1,3],[3,2,1,1],[1,3,2,1],[1,1,3,2]]
matrix_list=[]
new_matrix=[[],[],[],[]]
for x in range(4):
column_list=[]
for i in range(4):
column_list.append(data_hex[8*x+2*i:8*x+2*i+2])
matrix_list.append(column_list)
for x in range(4):
new_matrix[x].append(hex(GF2multi(2,matrix_list[x][0])^GF2multi(3,matrix_list[x][1])^GF2multi(1,matrix_list[x][2])^GF2multi(1,matrix_list[x][3]))[2:].rjust(2,"0"))
new_matrix[x].append(hex(GF2multi(1,matrix_list[x][0])^GF2multi(2,matrix_list[x][1])^GF2multi(3,matrix_list[x][2])^GF2multi(1,matrix_list[x][3]))[2:].rjust(2,"0"))
new_matrix[x].append(hex(GF2multi(1,matrix_list[x][0])^GF2multi(1,matrix_list[x][1])^GF2multi(2,matrix_list[x][2])^GF2multi(3,matrix_list[x][3]))[2:].rjust(2,"0"))
new_matrix[x].append(hex(GF2multi(3,matrix_list[x][0])^GF2multi(1,matrix_list[x][1])^GF2multi(1,matrix_list[x][2])^GF2multi(2,matrix_list[x][3]))[2:].rjust(2,"0"))
return ("".join(new_matrix[0])+"".join(new_matrix[1])+"".join(new_matrix[2])+"".join(new_matrix[3])).upper()
def round_plus(data_hex,round_num):
result=''
key_hex=key_list[round_num]
for x in range(16):
result+=hex(int(data_hex[2*x:2*x+2],16)^int(key_hex[2*x:2*x+2],16))[2:].rjust(2,"0")
return result
key="1234567890123456"
key_hex_0=key.encode('hex')
key_list=extkey(key_hex_0)
data="1234567890123456"
code=data.encode('hex')
code=round_plus(code,0)
for i in range(1,10):
code=exbyte(code)
code=h_move(code)
code=column_mix(code)
code=round_plus(code,i)
code=exbyte(code)
code=h_move(code)
code=round_plus(code,10)
print code
|