def Encrypt(k, S, SE, SBOX, TBOX, IndexArray, SrtB): E = ['' for i in range(4)] for i in range(0, LT - 1): for j in range(0, LT): E[j] = binstr.b_xor(W[j], S[j]) for j in range(0, LT): if (EvenParity(E[j]) == True): Rotate_Right(j, SBOX) else: Rotate_Left(j, SBOX) SBOX[2][j] = binstr.b_xor(SBOX[2][j], bin(SrtB[j])[2:].zfill(8)) Transpose(SBOX) E = [str(x) for x in numpy.roll(E, 1)] for i in range(0, LT): TIndex = transposition_index_generation(TBOX, i) #if k==0: # print "E:",TIndex while (IndexArray[TIndex % N] != 0): TIndex = TIndex + 1 TIndex = TIndex % N #if k==0: # print TIndex # print "E[i] {}".format(int(E[i],2)) #write E[i] in encrypted file SE in position of TIndex SE[TIndex] = int(E[i], 2) IndexArray[TIndex] = 1 SrtB[i] = int(S[i], 2) SBOX, TBOX = TBOX, SBOX #if k==0: # print " E \n SBOX {}\n TBOX {}".format(SBOX,TBOX) return SE, SBOX, TBOX
def encryption_key_gen(EKy, Ky, D): ''' Generates 32 Bytes EKy from user input Ky ( >= 16 Bytes ) and D ( MD5 hash of Ky = 16 Bytes ) ''' p1 = p2 = p3 = 0 L1 = len(Ky) for i in range(0, 32): if(i % 2 == 0): Eky[i] = D[p1] p1 += 1 else: p4 = p2 + 1 Eky[i] = binstr.b_xor(binstr.b_xor(D[p2], D[p4]), Ky[(p3) % L1]) p3 += 1 return Eky
def WORD_generation(SBOX, W): ''' Generates of 1 x LT WORD array by column wise XORing of SBOX ( used for encryption ) ''' for i in range(0, LT): for j in range(0, LT): W[i] = binstr.b_xor(W[i], SBOX[j][i])
def Decrypt(SE_N, front_index, SE, SD, SBOX, TBOX, IndexArray, SrtB): E = ['' for i in range(4)] S = ['' for i in range(4)] for i in range(0, LT): TIndex = transposition_index_generation(TBOX, i) while(IndexArray[TIndex % N] != 0): TIndex = TIndex + 1 TIndex = TIndex % N #print i, TIndex, len(SE) #write E[i] in encrypted file SE in position of TIndex E[i] = SE_N[TIndex] #print "E[i] {}".format(E[i]) IndexArray[TIndex] = 1 E = [str(x) for x in numpy.roll(E, -1)] for i in range(0, LT - 1): for j in range(0, LT): S[j] = binstr.b_xor(W[j],E[j]) # print "S[j] {} W[j] {} E[j] {}".format(S[j],W[j],E[j]) for j in range(0, LT): if(EvenParity(E[j]) == True): Rotate_Right(j, SBOX) else: Rotate_Left(j, SBOX) SBOX[2][j] = binstr.b_xor(SBOX[2][j], bin(SrtB[j])[2:].zfill(8)) Transpose(SBOX) for i in range(LT): # print i,S[i] SD[front_index+i] = int(S[i],2) SrtB[i] = int(S[i],2) SBOX, TBOX = TBOX, SBOX # print "D \n SBOX {}\n TBOX {}".format(SBOX,TBOX) return SD, SBOX, TBOX