|
| 1 | +from copy import copy |
| 2 | +from tables import * |
| 3 | + |
| 4 | +def subBytes(block): |
| 5 | + for i in range(len(block)): |
| 6 | + block[i] = sbox[block[i]] |
| 7 | + return block |
| 8 | + |
| 9 | +def subBytesInv(block): |
| 10 | + for i in range(len(block)): |
| 11 | + block[i] = sboxInv[block[i]] |
| 12 | + return block |
| 13 | + |
| 14 | +def keyScheduleCore(word, i): |
| 15 | + newWord = word[1:]+word[:1] |
| 16 | + subBytes(newWord) |
| 17 | + newWord[0] = newWord[0] ^ rCon[i] |
| 18 | + return newWord |
| 19 | + |
| 20 | +def expandKey(cipherKey): |
| 21 | + cipherKeySize = len(cipherKey) |
| 22 | + assert cipherKeySize == 32 |
| 23 | + expandedKey = [] |
| 24 | + currentSize,rconIter = 0,1 |
| 25 | + |
| 26 | + t = [0,0,0,0] |
| 27 | + |
| 28 | + for i in range(cipherKeySize): |
| 29 | + expandedKey.append(cipherKey[i]) |
| 30 | + currentSize += cipherKeySize |
| 31 | + |
| 32 | + while currentSize < 240: |
| 33 | + for i in range(4): |
| 34 | + t[i] = expandedKey[(currentSize - 4) + i] |
| 35 | + if currentSize % cipherKeySize == 0: |
| 36 | + t = keyScheduleCore(t, rconIter) |
| 37 | + rconIter += 1 |
| 38 | + if currentSize % cipherKeySize == 16: |
| 39 | + for i in range(4): |
| 40 | + t[i] = sbox[t[i]] |
| 41 | + for i in range(4): |
| 42 | + expandedKey.append(((expandedKey[currentSize - cipherKeySize]) ^ (t[i]))) |
| 43 | + currentSize += 1 |
| 44 | + return expandedKey |
| 45 | + |
| 46 | +def createRoundKey(expandedKey,i): |
| 47 | + return expandedKey[i*16:i*16+16] |
| 48 | + |
| 49 | +def addroundkey(block,roundKey): |
| 50 | + for i in range(len(block)): |
| 51 | + block[i] = block[i] ^ roundKey[i] |
| 52 | + return block |
| 53 | + |
| 54 | +def blockfileColumn(filename): |
| 55 | + test2 = filename |
| 56 | + testarr = [] |
| 57 | + for row in range(0,4): |
| 58 | + for column in range(0,len(test2),4): |
| 59 | + testarr.append(test2[row+column]) |
| 60 | + return testarr |
| 61 | + |
| 62 | +def rotate(rotate, n): |
| 63 | + return rotate[n:]+rotate[0:n] |
| 64 | + |
| 65 | +def shiftRow(blockfile): |
| 66 | + block = blockfileColumn(blockfile) |
| 67 | + for i in range(4): |
| 68 | + block[i*4:i*4+4] = rotate(block[i*4:i*4+4], i) |
| 69 | + blockDone = blockfileColumn(block) |
| 70 | + return blockDone |
| 71 | + |
| 72 | +def shiftRowInv(blockfile): |
| 73 | + block = blockfileColumn(blockfile) |
| 74 | + for i in range(4): |
| 75 | + block[i*4:i*4+4] = rotate(block[i*4:i*4+4], -i) |
| 76 | + block = blockfileColumn(block) |
| 77 | + return block |
| 78 | + |
| 79 | +def mixColumn(column): |
| 80 | + col = copy(column) |
| 81 | + column[0] = table_2[col[0]] ^ table_3[col[1]] ^ col[2] ^ col[3] |
| 82 | + column[1] = col[0] ^ table_2[col[1]] ^ table_3[col[2]] ^ col[3] |
| 83 | + column[2] = col[0] ^ col[1] ^ table_2[col[2]] ^ table_3[col[3]] |
| 84 | + column[3] = table_3[col[0]] ^ col[1] ^ col[2] ^ table_2[col[3]] |
| 85 | + return column |
| 86 | + |
| 87 | + |
| 88 | +def mixColumns(col): |
| 89 | + outCol = [] |
| 90 | + for i in range(0,len(col),4): |
| 91 | + outCol.append(mixColumn(col[i:i+4])) |
| 92 | + return listoflist2singlelist(outCol) |
| 93 | + |
| 94 | +def mixColumnInv(column): |
| 95 | + col = copy(column) |
| 96 | + column[0] = table_14[col[0]] ^ table_11[col[1]] ^ table_13[col[2]] ^ table_9[col[3]] |
| 97 | + column[1] = table_9[col[0]] ^ table_14[col[1]] ^ table_11[col[2]] ^ table_13[col[3]] |
| 98 | + column[2] = table_13[col[0]] ^ table_9[col[1]] ^ table_14[col[2]] ^ table_11[col[3]] |
| 99 | + column[3] = table_11[col[0]] ^ table_13[col[1]] ^ table_9[col[2]] ^ table_14[col[3]] |
| 100 | + return column |
| 101 | + |
| 102 | + |
| 103 | +def mixColumnsInv(col): |
| 104 | + intList = [] |
| 105 | + for i in range(0,len(col),4): |
| 106 | + intList.append(mixColumnInv(col[i:i+4])) |
| 107 | + return listoflist2singlelist(intList) |
| 108 | + |
| 109 | +def listoflist2singlelist(column): |
| 110 | + columnList = [] |
| 111 | + for row in column: |
| 112 | + for i in range(0,len(row)): |
| 113 | + columnList.append(row[i]) |
| 114 | + return columnList |
| 115 | + |
| 116 | +def encrypt(block,key): |
| 117 | + expandedKey = expandKey(key) |
| 118 | + roundKey = createRoundKey(expandedKey,0) |
| 119 | + block = addroundkey(block,roundKey) |
| 120 | + |
| 121 | + for i in range(1,14): |
| 122 | + roundKey = createRoundKey(expandedKey,i) |
| 123 | + block = subBytes(block) |
| 124 | + block = shiftRow(block) |
| 125 | + block = mixColumns(block) |
| 126 | + block = addroundkey(block,roundKey) |
| 127 | + |
| 128 | + roundKey = createRoundKey(expandedKey,14) |
| 129 | + block = subBytes(block) |
| 130 | + block = shiftRow(block) |
| 131 | + block = addroundkey(block,roundKey) |
| 132 | + return block |
| 133 | + |
| 134 | +def decrypt(block,key): |
| 135 | + expandedKey = expandKey(key) |
| 136 | + roundKey = createRoundKey(expandedKey,14) |
| 137 | + block = addroundkey(block,roundKey) |
| 138 | + block = shiftRowInv(block) |
| 139 | + block = subBytesInv(block) |
| 140 | + |
| 141 | + for i in range(13,0,-1): |
| 142 | + roundKey = createRoundKey(expandedKey,i) |
| 143 | + block = addroundkey(block,roundKey) |
| 144 | + block = mixColumnsInv(block) |
| 145 | + block = shiftRowInv(block) |
| 146 | + block = subBytesInv(block) |
| 147 | + |
| 148 | + roundKey = createRoundKey(expandedKey,0) |
| 149 | + block = addroundkey(block,roundKey) |
| 150 | + return block |
0 commit comments