HillCipher Crypto Assign PDF
HillCipher Crypto Assign PDF
ASSIGNMENT 2
NAME:
HAREESHA G : 1JS16CS406
MAHESH J R : 1JS16CS411
SURYA G : 1JS16CS426
MARKS OBTAINED:
What is Cryptography?
Hill cipher
The matrix used for encryption is the cipher key, and it should be chosen
randomly from the set of invertible n × n matrices (modulo 26). The cipher can, of
course, be adapted to an alphabet with any number of letters; all arithmetic just
needs to be done modulo the number of letters instead of modulo 26.
Encryption
Plain text: HELP
Decryption
Cipher text: HIAT
Implementation of Hill Cipher in Python
def hill(code):
decryptionKey = [[3,3],
[2,5]]
code = code.lower()
output = [[0],[0]]
counter = 0
for character in code:
number = ord(character) - 97
output[counter][0] = number
counter += 1
result = [[0],
[0]]
for i in range(len(decryptionKey)):
for j in range(len(output[0])):
for k in range(len(output)):
result[i][0] += decryptionKey[i][k] * output[k][j]
unCiphered = ""
for r in result:
numeric_letter = r[0] % 26
val = chr(numeric_letter + 97)
unCiphered = unCiphered + val
return unCiphered
def main():
code = input("Enter ciphertext: ")
print
plaintext = ""
while(code):
ciphertext = code[:2]
code = code[2:]
plaintext = plaintext + hill(ciphertext)
print(plaintext)
print
main()
Output