Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

hill cipher

# Python3 code to implement Hill Cipher
 
keyMatrix = [[0] * 3 for i in range(3)]
 
# Generate vector for the message
messageVector = [[0] for i in range(3)]
 
# Generate vector for the cipher
cipherMatrix = [[0] for i in range(3)]
 
# Following function generates the
# key matrix for the key string
def getKeyMatrix(key):
    k = 0
    for i in range(3):
        for j in range(3):
            keyMatrix[i][j] = ord(key[k]) % 65
            k += 1
 
# Following function encrypts the message
def encrypt(messageVector):
    for i in range(3):
        for j in range(1):
            cipherMatrix[i][j] = 0
            for x in range(3):
                cipherMatrix[i][j] += (keyMatrix[i][x] *
                                       messageVector[x][j])
            cipherMatrix[i][j] = cipherMatrix[i][j] % 26
 
def HillCipher(message, key):
 
    # Get key matrix from the key string
    getKeyMatrix(key)
 
    # Generate vector for the message
    for i in range(3):
        messageVector[i][0] = ord(message[i]) % 65
 
    # Following function generates
    # the encrypted vector
    encrypt(messageVector)
 
    # Generate the encrypted text
    # from the encrypted vector
    CipherText = []
    for i in range(3):
        CipherText.append(chr(cipherMatrix[i][0] + 65))
 
    # Finally print the ciphertext
    print("Ciphertext: ", "".join(CipherText))
 
# Driver Code
def main():
 
    # Get the message to
    # be encrypted
    message = "ACT"
 
    # Get the key
    key = "GYBNQKURP"
 
    HillCipher(message, key)
 
if __name__ == "__main__":
    main()
 
# This code is contributed
# by Pratik Somwanshi
Comment

PREVIOUS NEXT
Code Example
Python :: install python 3.4 mac terminal 
Python :: how to split strings in python 
Python :: how to run mac terminal from python script 
Python :: .dropna() python 
Python :: python cheat 
Python :: Python program to read a random line from a file 
Python :: que es una funcion en python 
Python :: pandas change string column to datetime 
Python :: Tuple: Create tuple 
Python :: trim strings python 
Python :: pandas include nan in value_counts 
Python :: dynamic printing 
Python :: sftp python 
Python :: closure python 
Python :: column of lists pandas 
Python :: remove duplicates in json python 
Python :: python np.sum 
Python :: py search and get objects from array 
Python :: django pk 
Python :: flattern in keras 
Python :: quotation marks in string 
Python :: use get method request html page python 
Python :: create list of dictionaries from list of list python 
Python :: python count elements in sublists 
Python :: python access class variable by string 
Python :: update django model with dict 
Python :: how to address null in python 
Python :: operators in python 
Python :: flask orm update query 
Python :: load list from file python 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =