1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| class aes: Round_num=10 S_Box=[0x65, 0x88, 0x7A, 0x86, 0x0C, 0x36, 0x3C, 0xD9, 0x78, 0xF4, 0x72, 0x01, 0x5F, 0xFD, 0xFA, 0x58, 0xD4, 0x9D, 0x92, 0xB7, 0xF7, 0xBC, 0xCA, 0xD3, 0x9F, 0x0F, 0xEF, 0x20, 0xAF, 0x6E, 0xE4, 0x35, 0x8C, 0xBF, 0xCF, 0xE9, 0x57, 0xB9, 0xDE, 0xA3, 0x3F, 0x54, 0xC2, 0x43, 0xD1, 0xA0, 0x0E, 0xDC, 0x85, 0x8B, 0x97, 0x96, 0x93, 0x16, 0x2A, 0x27, 0xA7, 0x9A, 0xB8, 0x70, 0xC5, 0x59, 0x65, 0x47, 0xE1, 0xE7, 0xB3, 0x69, 0x4E, 0xAA, 0x7F, 0x0B, 0xAB, 0x8F, 0xCD, 0xB5, 0x3B, 0x98, 0x83, 0xBE, 0x5B, 0x6F, 0xB0, 0xDF, 0x15, 0x26, 0x04, 0x07, 0xC0, 0x12, 0xF6, 0xA5, 0xEC, 0xC1, 0xE8, 0x99, 0xC7, 0x91, 0xF8, 0x37, 0xF5, 0x45, 0xD2, 0x17, 0xF9, 0x18, 0x75, 0x02, 0xD7, 0x40, 0xE5, 0xCB, 0xF2, 0x42, 0xFC, 0x77, 0x44, 0xC6, 0x34, 0x60, 0x6A, 0x41, 0x6B, 0xFB, 0x63, 0x19, 0x6C, 0x4A, 0x14, 0xBB, 0x39, 0x8D, 0x80, 0xED, 0xD8, 0x28, 0x5D, 0xB1, 0x1D, 0x5A, 0x3D, 0xCC, 0x7C, 0x4C, 0x74, 0x06, 0xE2, 0x66, 0x10, 0x9B, 0x90, 0x13, 0x89, 0x2F, 0xF1, 0x48, 0x21, 0xD6, 0x25, 0x30, 0x3A, 0xA4, 0x8A, 0x0D, 0x52, 0x8E, 0x62, 0x1C, 0xF0, 0xE3, 0xB4, 0x73, 0x50, 0x2B, 0x79, 0xAC, 0x1A, 0x23, 0xDD, 0x9C, 0xA2, 0x82, 0x9E, 0x51, 0x1B, 0xEB, 0x67, 0x3E, 0x71, 0x03, 0xBD, 0xFF, 0xAD, 0x22, 0xB2, 0x1E, 0x81, 0x49, 0xA8, 0x4F, 0x61, 0xA6, 0x38, 0xDB, 0xC8, 0x33, 0x0A, 0x29, 0xEA, 0x05, 0x94, 0xFE, 0x2C, 0xEE, 0x2D, 0x5C, 0x5E, 0x7B, 0x87, 0x55, 0x11, 0x4D, 0x56, 0x09, 0xCE, 0x46, 0x2E, 0xD0, 0x31, 0x24, 0x1F, 0xA1, 0xAE, 0xA9, 0xD5, 0x53, 0xE0, 0x08, 0xC9, 0x7E, 0xE6, 0x84, 0x64, 0x76, 0xBA, 0xF3, 0xDA, 0x32, 0xC4, 0x4B, 0x7D, 0xC3, 0x68, 0x95, 0x6D, 0xB6] Rcon=[0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36] En_Status=[[2,3,1,1], [1,2,3,1], [1,1,2,3], [3,1,1,2]] De_Status=[[0x0E, 0x0B, 0x0D, 0x09], [0x09, 0x0E, 0x0B, 0x0D], [0x0D, 0x09, 0x0E, 0x0B], [0x0B, 0x0D, 0x09, 0x0E]] def __init__(self,Key): self.D_Box=[0]*256 for i,v in enumerate(self.S_Box): self.D_Box[v]=i self.Round_num=10 self.Key=Key pass def Rot_Word(self,Raw_data,Rot_Num=1): Raw_len=len(Raw_data) New_data=[] i=Rot_Num New_len=0 while New_len<Raw_len: New_data.append(Raw_data[i%Raw_len]) i=i+1 New_len+=1 return New_data def SubBytes(self,Raw_data,Box): New_data=[] for i in Raw_data: New_Row=[] for j in i: New_Row.append(Box[j]) New_data.append(New_Row) return New_data def T(self,Raw_data,Round_N): New_data=self.SubBytes([self.Rot_Word(Raw_data)],self.S_Box)[0] New_data[0]^=self.Rcon[Round_N] return New_data def ExtentKey(self,Key_data : list): Ext_Key=Key_data.copy() i=len(Key_data) Step=4 while i<self.Round_num*4+4: New_k=[] if i%Step != 0: for j,v in enumerate(Ext_Key[i-Step]): New_k.append(Ext_Key[i-1][j]^v) else: T_Key=self.T(Ext_Key[i-1],i//Step-1) for j,v in enumerate(Ext_Key[i-Step]): New_k.append(T_Key[j]^v) Ext_Key.append(New_k) i+=1 return Ext_Key def ShiftRow(self,Row_data,dire=True): New_data=[] Row_len=len(Row_data) Col_len=len(Row_data[0]) i=0 while i<Row_len: New_data.append([]) j=0 while j<Col_len: if dire: New_data[i].append(Row_data[ (j+i)%Row_len ][j]) else: New_data[i].append(Row_data[ (Row_len-j+i)%Row_len ][ j ] ) j+=1 i+=1 return New_data def Safe_Out2(self,Raw_data): Result=Raw_data Result=Result*2 if Result>=0x100: Result=(Result&0xff)^0x1b return Result def Safe_Out4(self,Raw_data): return self.Safe_Out2(self.Safe_Out2(Raw_data)) def Safe_Out8(self,Raw_data): return self.Safe_Out4(self.Safe_Out2(Raw_data)) def Safe_Mix(self,Raw_data,Num): Result=Raw_data if Num==3: Result=self.Safe_Out2(Result)^Raw_data elif Num==2: Result=self.Safe_Out2(Result) elif Num==9: Result=self.Safe_Out8(Result)^Raw_data elif Num==11: Result=self.Safe_Out8(Result)^self.Safe_Out2(Result)^Raw_data elif Num==13: Result=self.Safe_Out8(Result)^self.Safe_Out4(Result)^Raw_data elif Num==14: Result=self.Safe_Out8(Result)^self.Safe_Out4(Result)^self.Safe_Out2(Result) return Result def Column_Mix(self,Row_data,Mul_Mat): New_data=[] for i in Row_data: New_Row=[0,0,0,0] for j,v in enumerate(i): New_Row[0]^=self.Safe_Mix(v,Mul_Mat[0][j]) New_Row[1]^=self.Safe_Mix(v,Mul_Mat[1][j]) New_Row[2]^=self.Safe_Mix(v,Mul_Mat[2][j]) New_Row[3]^=self.Safe_Mix(v,Mul_Mat[3][j]) New_data.append(New_Row) return New_data def xorMat(self,Data_Mat,Key_Mat): New_Mat=[] for i,v in enumerate(Data_Mat): New_Row=[] for j,v1 in enumerate(Data_Mat[i]): New_Row.append(v1^Key_Mat[i][j]) New_Mat.append(New_Row) return New_Mat def toMatrix(self,data,Mat_W=4,Mat_H=4): Matrixs=[] w_i=0 h_i=0 Matrix=[] Matrix_Row=[] for i in data: Matrix_Row.append(i) w_i+=1 if w_i >= Mat_W: Matrix.append(Matrix_Row) Matrix_Row=[] w_i=0 h_i+=1 if h_i>=Mat_H: Matrixs.append(Matrix) h_i=0 return Matrixs def toList(self,Raw_Mat): Out_data=[] for i in Raw_Mat: for j in i: Out_data.append(j) return Out_data def decrypt(self,Raw_data): Key_Mat=self.toMatrix(self.Key)[0] Exten_K=self.ExtentKey(Key_Mat) Key_Mat=Exten_K[self.Round_num*4:self.Round_num*4+4] Plain=[] This_data=0 De_len=len(Raw_data) while This_data < De_len: T_de_data=self.toMatrix(Raw_data[This_data:This_data+16])[0] T_de_data=self.xorMat(T_de_data,Key_Mat) This_Round=0 This_Key=self.Round_num*4-4 while This_Round < self.Round_num: T_Key=Exten_K[This_Key:This_Key+4] T_de_data=self.SubBytes(T_de_data,self.D_Box) T_de_data=self.ShiftRow(T_de_data,dire=False) if This_Round!=self.Round_num-1: T_de_data=self.Column_Mix(T_de_data,self.De_Status) T_Key=self.Column_Mix(T_Key,self.De_Status) T_de_data=self.xorMat(T_de_data,T_Key) This_Key-=4 This_Round+=1 Plain=Plain+self.toList(T_de_data) This_data+=16 return bytes(Plain)
key=b'Th3_K4y_R3vers3!' a=aes(key) b=bytes([0x5A, 0xC0, 0x94, 0x69, 0x5C, 0x42, 0xFA, 0xCD, 0x49, 0x93, 0x60, 0xEE, 0x6E, 0x14, 0x87, 0x3F]) print(a.decrypt(b))
|