Write a program for Hill Cipher Method (Use 2 x 2 Matrix and 3 x 3Matrix)Note: (Optionsshould be given to user for Key matrix. it is either 2 x 2 matrix or 3 x 3 matrix)Hint: https://www.javatpoint.com/hill-cipher-program-in-javaBelow change is required in your submission:1. You have to take input as key matrix2. Entered matrix is square matrix or not, if not pls re-enter the matrix3. Take input of plaintext is also in square matrix form for ex if 2 X 2 matrix is entered than plaintext should be 4 character, if matrix is 3X3 than plaintext should be 9 character like wise...Note: use padding if necessary
Question
Write a program for Hill Cipher Method (Use 2 x 2 Matrix and 3 x 3Matrix)Note: (Optionsshould be given to user for Key matrix. it is either 2 x 2 matrix or 3 x 3 matrix)Hint: https://www.javatpoint.com/hill-cipher-program-in-javaBelow change is required in your submission:1. You have to take input as key matrix2. Entered matrix is square matrix or not, if not pls re-enter the matrix3. Take input of plaintext is also in square matrix form for ex if 2 X 2 matrix is entered than plaintext should be 4 character, if matrix is 3X3 than plaintext should be 9 character like wise...Note: use padding if necessary
Solution
Here is a Python program for the Hill Cipher Method. This program will take a 2x2 or 3x3 key matrix as input and will check if the entered matrix is a square matrix. It will also take the plaintext as input in a square matrix form.
import numpy as np
from sympy import Matrix
def encrypt(message_vector, key_matrix):
cipher_matrix = key_matrix.dot(message_vector)
cipher_matrix = np.mod(cipher_matrix, 26)
return cipher_matrix
def decrypt(cipher_matrix, key_matrix):
key_inverse = key_matrix.inv_mod(26)
message_matrix = key_inverse.dot(cipher_matrix)
message_matrix = np.mod(message_matrix, 26)
return message_matrix
def main():
size = int(input("Enter the size of the key matrix (2 or 3): "))
if size not in [2, 3]:
print("Invalid size. Please enter 2 or 3.")
return
print("Enter the elements of the key matrix:")
key_matrix = []
for _ in range(size):
row = list(map(int, input().split()))
if len(row) != size:
print("Invalid matrix. Please enter a square matrix.")
return
key_matrix.append(row)
key_matrix = Matrix(key_matrix)
if key_matrix.det() == 0:
print("Invalid key matrix. Determinant is 0.")
return
message = input("Enter the plaintext (length should be a multiple of the matrix size): ")
if len(message) % size != 0:
print("Invalid plaintext length. It should be a multiple of the matrix size.")
return
message_vector = [ord(c) - ord('A') for c in message.upper()]
message_vector = np.array(message_vector).reshape(-1, size).T
cipher_matrix = encrypt(message_vector, key_matrix)
print("Encrypted text: ", ''.join(chr(c + ord('A')) for c in cipher_matrix.flatten()))
message_matrix = decrypt(cipher_matrix, key_matrix)
print("Decrypted text: ", ''.join(chr(int(round(c)) + ord('A')) for c in message_matrix.flatten()))
if __name__ == "__main__":
main()
This program uses the numpy and sympy libraries for matrix operations. The encrypt function encrypts the message using the key matrix, and the decrypt function decrypts the cipher using the inverse of the key matrix. The main function takes the key matrix and plaintext as input, checks if they are valid, and then calls the encrypt and decrypt functions.
Similar Questions
For a certain Hill Cipher the Encryption Matrix is [[17 17 5] [21 18 21] [2 2 19]]. The Plaintext is orb. The corresponding Cipher text will be
Encrypt the message “GTU EXAMINATION” using the hill cipheralgorithm with the key matrix 5 174 15
you have to implement the followinhg algoruthm usi9mg sagmaths tool affine cipher , hill cipher, shift cipher, substitution cipher, transposition cipher, vigenere cipher. this should be followed by cryptanalysis of all of the above classical cipher algorithm
What will be the correct cipher text for the message “money helps” and , using Hill Cipher. Group of answer choicesUNPAW YOELFUNKWA YOELEUNKAW YOELFUNPAW YOELEUNKWA YOELE
Write a Java program to manipulate a matrix by swapping the elements in even columns. The program should prompt the user to input the number of rows and columns of the matrix, followed by the elements of the matrix. Afterward, it should swap the elements in even columns of the matrix and display the modified matrix. Demonstrate the functionality of your program with a sample input/output scenario.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.