def encryptionCTR(key,plaintext,original_Counter): count = len(plaintext)/64 cipher = '' hexPlain = tl.b2h(plaintext) encryCounter_List = getCounter(tl.b2h(original_Counter),count,key) for i in xrange(0,count,1): cipher+= str(tl.b2h(tl.xor(tl.h2b(encryCounter_List[i]),tl.h2b(hexPlain[i*16:(i*16)+16])))) return cipher
def decryptionCTR(key,ciphertext,original_Counter,padding_size): count = len(ciphertext)/16 plain='' encryCounter_List = getCounter(tl.b2h(original_Counter),count,key) for i in xrange(0,count,1): plain+= str(tl.b2h(tl.xor(tl.h2b(encryCounter_List[i]),tl.h2b(ciphertext[i*16:(i*16)+16])))) plain = tl.unPadding(plain,padding_size) return plain
def newKey(key): key_table = key row0=[] row1=[] row2=[] row3=[] row4=[] row5=[] row6=[] row7=[] for i in range (8): result0=str(int(key_table[i]) ^ int(key_table[i+8])) result2=str(int(key_table[i+16]) ^ int(key_table[i+24])) result4=str(int(key_table[i+32]) ^ int(key_table[i+40])) result6=str(int(key_table[i+48]) ^ int(key_table[i+56])) row0.append(result0) row2.append(result2) row4.append(result4) row6.append(result6) row1_0=[] for i in xrange(8,16,1): row1_0.append(key_table[i]) Hax_row1=tl.b2h(row1_0) Sbox_Value_1 = substituted(Hax_row1) result1 = tl.h2b(Sbox_Value_1) row3_0=[] for i in xrange(24,32,1): row3_0.append(key_table[i]) Hax_row3=tl.b2h(row3_0) Sbox_Value_3 = substituted(Hax_row3) result3 = tl.h2b(Sbox_Value_3) row5_0=[] for i in xrange(40,48,1): row5_0.append(key_table[i]) Hax_row5=tl.b2h(row5_0) Sbox_Value_5 = substituted(Hax_row5) result5 = tl.h2b(Sbox_Value_5) row7_0=[] for i in xrange(56,64,1): row7_0.append(key_table[i]) Hax_row7=tl.b2h(row7_0) Sbox_Value_7 = substituted(Hax_row7) result7 = tl.h2b(Sbox_Value_7) for i in range(8): row1.append(result1[i]) row3.append(result3[i]) row5.append(result5[i]) row7.append(result7[i]) return row0+row1+row2+row3+row4+row5+row6+row7
def encryptionCFB(key,plaintext,iv): count = len(plaintext)/64 cipher = '' for i in xrange(0,count,1): key_plain = tl.h2b(aes.encryption(key,iv)) c = tl.b2h(tl.xor(key_plain,plaintext[i*64:(i*64)+64])) cipher += c iv = tl.h2b(c) return cipher
def decryptionCFB(key,ciphertext,iv): hexCipher = tl.h2b(ciphertext[0]) count = len(tl.h2b(ciphertext[0]))/64 plain = '' for i in xrange(0,count,1): key_plain = tl.h2b(aes.encryption(key,iv)) p = tl.b2h(tl.xor(key_plain,hexCipher[i*64:(i*64)+64])) iv = hexCipher[i*64:(i*64)+64] plain += p plain = tl.unPadding(plain, ciphertext[1]) return plain
def encryption(key, plaintext): keylist=[] keylist.append(key) for i in range(1,6): key = newKey(keylist[i-1]) keylist.append(key) state = tl.b2h(tl.xor(keylist[0],plaintext)) for i in range(1,5): state = substituted(state) state = permutation(tl.h2b(state)) state = tl.b2h(tl.xor(keylist[i],state)) state = substituted(state) state = tl.b2h(tl.xor(keylist[5],tl.h2b(state))) return state
def decryptionECB(key, ciphertext): cipher = tl.h2b(ciphertext[0]) result = "" for x in xrange(0, (len(cipher)/64)): result += aes.decryption(key, cipher[(x*64):(x*64+64)]) result = tl.unPadding(result, ciphertext[1]) return result
def getCounter(original_Counter,count,key): encryCounter_List =[] counter = int(original_Counter,16) for i in xrange(0,count,1): counter=counter + i counter_binary=bin(counter)[2:].zfill(len(original_Counter)*4) encryCounter = aes.encryption(key,tl.h2b(tl.b2h(counter_binary))) encryCounter_List.append(encryCounter) return encryCounter_List
def decryption(key,ciphertext): keylist=[] keylist.append(key) for i in range(1,6): key = newKey(keylist[i-1]) keylist.append(key) # Round 1 XOR KEY5 state = tl.b2h(tl.xor(keylist[5],ciphertext)) state = inverse_substituted(state) # Round 2 to 5 for i in xrange(4,0,-1): state = tl.b2h(tl.xor(keylist[i],tl.h2b(state))) state = inverse_permutation(tl.h2b(state)) state = inverse_substituted(tl.b2h(state)) # Round 6 XOR KEY0 state = tl.b2h(tl.xor(keylist[0],tl.h2b(state))) return state
def encryptionOFB(iv, key, plaintext): biPlain = plaintext count = len(plaintext)/64 cipher = [] for i in xrange(0,count,1): iv = tl.h2b(aes.encryption(key, iv)) biP = biPlain[i*64:(i*64)+64] cipher += tl.xor(iv, biP) cipherStr = tl.b2h(cipher) return cipherStr
def encryptionCBC(key,plaintext,iv): count = len(plaintext)/64 cipher = '' for i in xrange(0,count,1): plain = tl.xor(plaintext[i*64:(i*64)+64],iv) c = aes.encryption(key,plain) cipher += c iv = tl.h2b(c) return cipher
def decryptionOFB(iv, key, ciphertext,padding_size): biCipher = ciphertext count = len(biCipher)/64 plain = [] for i in xrange(0,count,1): iv = tl.h2b(aes.encryption(key, iv)) biC = ciphertext[i*64:(i*64)+64] plain += tl.xor(iv, biC) plainStr = tl.b2h(plain) plainStr = tl.unPadding(plainStr,padding_size) return plainStr
def main(): print "1: ECB" print "2: CBC" print "3: OFB" print "4: CFB" print "5: CTR" tmp = raw_input('-->') [plaintext, padding_size] = tl.getPlaintext('./original.txt') roundKey = tl.getKey("./key.txt") iv = tl.getIV() userInput = int(tmp) ciphertext = "" resulttext = "" if userInput == 1: # Encryption process of ECB start1 = time.time() ciphertext = [encryptionECB(roundKey,plaintext), padding_size] end1 = time.time() print "encryption time: " + str(end1 - start1) start2 = time.time() resulttext = decryptionECB(roundKey,ciphertext) end2 = time.time() print "decryption time: " + str(end2 - start2) elif userInput == 2: # Encryption process of CBC start1 = time.time() ciphertext = [encryptionCBC(roundKey,plaintext,iv),padding_size] end1 = time.time() print "encryption time: " + str(end1 - start1) start2 = time.time() resulttext = decryptionCBC(roundKey,ciphertext,iv) end2 = time.time() print "decryption time: " + str(end2 - start2) elif userInput == 3: # Encryption process of OFB start1 = time.time() ciphertext = [encryptionOFB(iv, roundKey, plaintext), padding_size] end1 = time.time() print "encryption time: " + str(end1 - start1) start2 = time.time() resulttext = decryptionOFB(iv, roundKey, tl.h2b(ciphertext[0]), padding_size) end2 = time.time() print "decryption time: " + str(end2 - start2) elif userInput == 4: # Encryption process of CFB start1 = time.time() ciphertext = [encryptionCFB(roundKey,plaintext,iv),padding_size] end1 = time.time() print "encryption time: " + str(end1 - start1) start2 = time.time() resulttext = decryptionCFB(roundKey,ciphertext,iv) end2 = time.time() print "decryption time: " + str(end2 - start2) elif userInput == 5: # Encryption process of CTR Counter = readCounter("./counter.txt") start1 = time.time() ciphertext = [encryptionCTR(roundKey,plaintext,Counter), padding_size] end1 = time.time() print "encryption time: " + str(end1 - start1) start2 = time.time() resulttext = decryptionCTR(roundKey,ciphertext[0],Counter,padding_size) end2 = time.time() print "decryption time: " + str(end2 - start2) else: print "Wrong Input" sys.exit(0) tl.writeData('./cipher.txt', ciphertext[0], padding_size) print resulttext
def readCounter(path): meta = tl.fileReader(path) binaryData = tl.h2b(meta) return binaryData