def applyCBC(text, DESKey, initialVector, mode): # convert all parameters into binary # text = convertToBinary(text) # DESKey = convertToBinary(DESKey) # initialVector = convertToBinary(initialVector) feedback = initialVector # divide the text into blocks each of size 64 bits textIntoParts = splitIntoParts(text, 64) result = [] for i in range(len(textIntoParts)): if (mode == ENCRYPT): res = xor(feedback, textIntoParts[i], 64) feedback = applyDES(res, DESKey, ENCRYPT) result.append(feedback) else: desres = applyDES(textIntoParts[i], DESKey, DECRYPT) res = "" if (i == 0): res = xor(desres, initialVector, 64) else: res = xor(desres, textIntoParts[i - 1], 64) result.append(res) # convert the binary back to characters return ("".join(result))
def applyDES(text, key, mode): # split the text into parts each of size 64 text = splitIntoParts(text, 64) res = "" for x in text: # apply DES for each part res += DES(x, key, mode) # return the result return res
def applyCBC(text, DESKey1, DESKey2, inititalVector, mode): # convert all parameters into binary # divide the text into blocks each of size 128 bits textIntoParts = splitIntoParts(text, 128) # first generate all keys DESKey1 = generateKeys(DESKey1) DESKey2 = generateKeys(DESKey2) # split the inititalVector into 2 halves each of 64bit length firstHalf = inititalVector[:64] lastHalf = inititalVector[64:] # shift the first half of the initial vector once and second half twice iv1 = firstHalf[1:] + firstHalf[0] iv2 = lastHalf[2:] + lastHalf[:2] # check if encryption or decryption if (mode == ENCRYPT): firstHalf, lastHalf = CBCEncryptRound(firstHalf, lastHalf, textIntoParts[0][:64], textIntoParts[0][64:], iv1, iv2, DESKey1, DESKey2) else: firstHalf, lastHalf = CBCDecryptRound(lastHalf, firstHalf, textIntoParts[0][:64], textIntoParts[0][64:], iv1, iv2, DESKey1, DESKey2) # add the first cipher/plaintext into the list result = [firstHalf + lastHalf] for i in range(1, len(textIntoParts)): # shift the first half of the initial vector once and second half twice iv1 = iv1[1:] + iv1[0] iv2 = iv2[2:] + iv2[:2] # check if encryption or decryption if (mode == ENCRYPT): firstHalf, lastHalf = CBCEncryptRound(lastHalf, firstHalf, textIntoParts[i][:64], textIntoParts[i][64:], iv1, iv2, DESKey1, DESKey2) else: firstHalf, lastHalf = CBCDecryptRound(textIntoParts[i - 1][:64], textIntoParts[i - 1][64:], textIntoParts[i][:64], textIntoParts[i][64:], iv1, iv2, DESKey1, DESKey2) # add the cipher/plaintext into the list result.append(firstHalf + lastHalf) # return result return ("".join(result))
def applyDESOnAChannel(channel, h, deskey, mode): # convert the key to binary string deskey = convertCharToBinary(deskey) # iterate through row for i in range(h): # first split the row values into groups of 8 channelListIntoParts = splitIntoParts(channel[i], 8) # then iterate through each group for k in range(len(channelListIntoParts)): # convert the list into a binary string pltxt = convertNumToBinary(channelListIntoParts[k]) # apply the DES for the binary string in encryption mode cipher = applyDES(pltxt, deskey, mode) # convert the binary string into number list and create a list of type uint8 for the encrypted binary string cipherList = numpy.array(convertBinaryToNum(cipher), dtype=numpy.uint8) # replace the original value with the cipher value channelListIntoParts[k] = cipherList # replace the original row value with the new ciphered value channel[i] = mergeParts(channelListIntoParts) # return the channel return channel
def sBoxes(text): sBoxesList = [[ [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7], [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8], [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0], [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13], ], [ [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10], [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5], [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15], [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9], ], [ [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8], [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1], [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7], [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12], ], [ [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15], [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9], [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4], [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14], ], [ [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9], [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6], [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14], [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3], ], [ [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11], [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8], [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6], [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13], ], [ [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1], [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6], [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2], [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12], ], [ [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7], [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2], [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8], [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11], ]] # firs split the text into 8 parts of 6 bits each textInto8Parts = splitIntoParts(text, 6) res = "" # then apply the sbox for eaxh part for i, sbox in enumerate(sBoxesList): # get the row from the first and the last bits row = int((textInto8Parts[i][0] + textInto8Parts[i][5]), 2) # get the column from the bits in between first and last bits col = int(textInto8Parts[i][1:5], 2) # from the sbox get the result res += bin(sbox[row][col])[2:].zfill(4) return res
imgPath = "./geass-gray-img.jpg" try: img = cv2.imread(imgPath, 0) except: print("Invalid path!") exit(0) print("-" * 64) # ---Encryption--- # get the width and height of the image w, h = img.shape print("Encrypting...") # iterate through each row for i in range(h): # first split the row values into groups of 8 imgListIntoParts = splitIntoParts(img[i], 8) # then iterate through each group for k in range(len(imgListIntoParts)): # convert the list into a binary string pltxt = convertNumToBinary(imgListIntoParts[k]) # apply the CBC for the binary string in encryption mode cipher = applyCBC(pltxt, deskey, initVect, ENCRYPT) # convert the binary string into number list and create a list of type uint8 for the encrypted binary string cipherList = numpy.array(convertBinaryToNum(cipher), dtype=numpy.uint8) # replace the original value with the cipher value imgListIntoParts[k] = cipherList # replace the original row value with the new ciphered value img[i] = mergeParts(imgListIntoParts) print("Encryption done!") # show the encrypted image cv2.imshow("encrypted image", img)