本次AES算法实现,我将AES算法拆成5个组成部分:轮密钥加、字节替换、行位移、列混淆、密钥扩展5个部分,本次使用的示例以书应用密码学(第四版)第128面的例子进行试验。
1.轮密钥加
def ADDROUNDKEY(p,k):#输入数组
ARK = [] # 轮密钥加
for i in range(4):
x = p[i]
y = k[i]
z = eval("0b"+x) ^ eval("0b"+y)
xor = format(z,'b').zfill(32)
ARK.append(xor)
return ARK#返回轮密钥加数组
############################################################
plaintext = "32 43 f6 a8 88 5a 30 8d 31 31 98 a2 e0 37 07 34".replace(" ","")
key = "2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c".replace(" ","")
p = []#存储明文二进制
k = []#存储密钥二进制
for i in range(0,len(plaintext),8):#将每4个字节存储为1列
p.append(bin(int("0x" + plaintext[i:i+8],16))[2:].zfill(32))#获得明文二进制
for i in range(0,len(key),8):#将每4个字节存储为1列
k.append(bin(int("0x" + key[i:i+8],16))[2:].zfill(32))#获得密钥二进制
print(p)
print(k)
#print(f'轮密钥加结果:{ADDROUNDKEY(p,k)}')
print(ADDROUNDKEY(p,k))
temp = hex(int("0b"+"".join(ADDROUNDKEY(p,k)),2))[2:]
ark = []#存储初始轮密钥加结果
for i in range(0,len(temp),2):
ark.append(temp[i:i+2])
print(f"初始轮密钥加结果为:{' '.join(ark).upper()}")
'''
初始轮密钥加结果为:19 3D E3 BE A0 F4 E2 2B 9A C6 8D 2A E9 F8 48 08
'''
2.字节替换
字节替换需要自己建立一个替换表格,如下面Subchart
def SubBytes(plaintext):#字节替换,输入数组
Subchart = [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]
temp = []
for i in plaintext:#输入十六进制,将列表的16个十六进制数分别转化二进制数
temp.append(bin(int("0x" + i,16))[2:].zfill(8))
table = []#存储字节替换的二进制
for i in temp:
x = int(eval("0b" + i[:4]))#行值,前四位作为行数
y = int(eval("0b" + i[4:]))#列值,后四位作为列数
#table.append(hex(int("0b"+bin(Subchart[x*16+y])[2:].zfill(8),2))[2:].zfill(2).upper())
table.append(format(int(Subchart[x*16+y]),'x').upper())
return table
plaintext = "19 3D E3 BE A0 F4 E2 2B 9A C6 8D 2A E9 F8 48 08".split(" ")
print(f'字节替换的结果为:{" ".join(SubBytes(plaintext))}')
'''
字节替换的结果为:D4 27 11 AE E0 BF 98 F1 B8 B4 5D E5 1E 41 52 30
'''
3.行位移
行位移在AES算法里应该是最简单的一个步骤啦,如下面shiftrow1函数,输入一个列表,直接通过索引来自行变化每个元素的位置,或者按照下方的函数,通过列表ShiftROW_chart的每个数字再减去1作为新位置的索引。
def shiftrow1(s):
return [s[0],s[5],s[10],s[15],
s[4],s[9],s[14],s[3],
s[8],s[13],s[2],s[7],
s[12],s[1],s[6],s[11]]
plaintext = "D4 27 11 AE E0 BF 98 F1 B8 B4 5D E5 1E 41 52 30".split(" ")
print(" ".join(shiftrow1(plaintext)))
def ShiftRows(plaintext):#行位移,输入数组
ShiftROW_chart = [1, 6, 11, 16,
5, 10, 15, 4,
9, 14, 3, 8,
13, 2, 7, 12]#构建位移表
temp = [i for i in range(16)]#用于存储换位的表
for i in range(len(ShiftROW_chart)):
temp[i] = plaintext[ShiftROW_chart[i] - 1]
return temp
plaintext = "D4 27 11 AE E0 BF 98 F1 B8 B4 5D E5 1E 41 52 30".split(" ")
print(" ".join(ShiftRows(plaintext)))
'''
结果都是一样:D4 BF 5D 30 E0 B4 52 AE