Hill cipher encryption:cryptography(python)

#encryption
key = [[7, 8], [11, 11]]  # key
l2 = [[0], [0]]    # temporary list
for i in range(2):
    for j in range(2):
        print(key[i][j], end="\t")    # to display key in console
    print("\n")
plaintext = input("enter the paintext:")
ciphertext = ""
plaintext = plaintext.upper().replace(" ", "")
if len(plaintext) % 2 != 0:    # to convert in digraph form
    plaintext = plaintext+'X'
def encrypt(a,b):
    s = 0
    l1=[[ord(a)-65],[ord(b)-65]]
    for i in range(2):
        for j in range(1):
            for k in range(2):
                s += key[i][k]*l1[k][j]
            l2[i][j] = chr((s % 26)+65)
            s = 0
    return l2
for i in range(0,len(plaintext),2):
    ciphertext += ''.join(str(e) for e in (encrypt(plaintext[i],        plaintext[i+1])))#convert list to str
print("ciphertext=",ciphertext)



OUTPUT
C:\Users\Suraj\venv\hello\Scripts\python.exe C:/Users/Suraj/PycharmProjects/hello/hill.py
7 8

11 11

enter the paintext:short example
ciphertext= ['A']['P']['A']['D']['J']['T']['F']['T']['W']['L']['F']['J']


Process finished with exit code 0

No comments

Powered by Blogger.