Пример #1
0
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))
Пример #2
0
def CBCEncryptRound(firstHalfFeedback, lastHalfFeedback, firstHalfText,
                    lastHalfText, iv1, iv2, DESKey1, DESKey2):
    # apply des for the two halves of the feedback
    firstHalfRes = applyDES(firstHalfFeedback, DESKey1, ENCRYPT)
    lastHalfRes = applyDES(lastHalfFeedback, DESKey2, ENCRYPT)

    # xor the two des results with a half of the plain text
    firstHalfCipher = xor(firstHalfText, firstHalfRes, 64)
    lastHalfCipher = xor(lastHalfText, lastHalfRes, 64)

    # apply des for the xor'd results
    lastCipher = applyDES(firstHalfCipher, DESKey2, ENCRYPT)
    firstCipher = applyDES(lastHalfCipher, DESKey1, ENCRYPT)

    # xor the result of des with a half of the initial vector
    firstCipher = xor(firstCipher, iv1, 64)
    lastCipher = xor(lastCipher, iv2, 64)

    # return the cipher text
    return [firstCipher, lastCipher]
Пример #3
0
def CBCDecryptRound(firstHalfFeedback, lastHalfFeedback, firstHalfCipher,
                    lastHalfCipher, iv1, iv2, DESKey1, DESKey2):
    # xor the halves of cipher text and initial vector
    firstHalfCipher = xor(firstHalfCipher, iv1, 64)
    lastHalfCipher = xor(lastHalfCipher, iv2, 64)

    # apply des for the two halves of cipher text in decryption mode
    firstHalfRes1 = applyDES(firstHalfCipher, DESKey1, DECRYPT)
    lastHalfRes1 = applyDES(lastHalfCipher, DESKey2, DECRYPT)

    # apply des for the two halves of the feedback in encryption mode
    firstHalfRes2 = applyDES(lastHalfFeedback, DESKey1, ENCRYPT)
    lastHalfRes2 = applyDES(firstHalfFeedback, DESKey2, ENCRYPT)

    # cross xor the halves the two des results
    firstHalfText = xor(firstHalfRes2, lastHalfRes1, 64)
    lastHalfText = xor(firstHalfRes1, lastHalfRes2, 64)

    # return the plain text
    return [firstHalfText, lastHalfText]
Пример #4
0
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
Пример #5
0
def callDES(text, key, index):
    r = applyDES(text, key)
    res[index] = r
Пример #6
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 DES for the binary string in encryption mode
        cipher = applyDES(pltxt, deskey, 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)
cv2.waitKey(0)
# save the encrypted image to this directory
cv2.imwrite("./encrypted.png", img)
print("Saved the encrypted image into current directory!")
cv2.destroyAllWindows()