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 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 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 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 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 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 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 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 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