0% found this document useful (0 votes)
22 views6 pages

Import Stringimport String

The document defines functions to generate an encryption table from a key, encrypt text using the table, and decrypt encrypted text back to the original using the same table. It takes a key, generates a table removing those letters from the alphabet, then splits the remaining letters into the table. It encrypts and decrypts text by looking up letters in the table based on the character position and index. An example usage encrypts and decrypts a sample plaintext.

Uploaded by

kokobberihu14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

Import Stringimport String

The document defines functions to generate an encryption table from a key, encrypt text using the table, and decrypt encrypted text back to the original using the same table. It takes a key, generates a table removing those letters from the alphabet, then splits the remaining letters into the table. It encrypts and decrypts text by looking up letters in the table based on the character position and index. An example usage encrypts and decrypts a sample plaintext.

Uploaded by

kokobberihu14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import stringimport string

def generate_table(key):

key = key.upper()

alphabet = string.ascii_uppercase

table = [[] for _ in range(5)]

for char in key:

if char in alphabet:

alphabet = alphabet.replace(char, '')

table[0] = list(alphabet[0:5])

table[1] = list(alphabet[5:10])

table[2] = list(alphabet[10:15])

table[3] = list(alphabet[15:20])

table[4] = list(alphabet[20:25])

return table

def encrypt(table, text):

encrypted_text = ""

index = 0

while index < len(text):

if text[index] in string.ascii_uppercase:
encrypted_text += table[index % 5][string.ascii_uppercase.index(text[index]) // 5]

index += 1

if index < len(text) and text[index] in string.ascii_uppercase:

encrypted_text += table[index % 5][string.ascii_uppercase.index(text[index]) // 5]

index += 1

else:

break

else:

index += 1

return encrypted_text

def decrypt(table, text):

decrypted_text = ""

index = 0

while index < len(text):

if text[index] in string.ascii_uppercase:

decrypted_text += table[index % 5][string.ascii_uppercase.index(text[index]) // 5]

index += 1

if index < len(text) and text[index] in string.ascii_uppercase:

decrypted_text += table[index % 5][string.ascii_uppercase.index(text[index]) // 5]

index += 1
else:

break

else:

index += 1

return decrypted_text

# Usage example

key = "security"

plain_text = "attack on dawn"

table = generate_table(key)

encrypted_text = encrypt(table, plain_text)

decrypted_text = decrypt(table, encrypted_text)

print(f"Key: {key}")

print(f"Plain Text: {plain_text}")

print(f"Encrypted Text: {encrypted_text}")

print(f"Decrypted Text: {decrypted_text}")

def generate_table(key):

key = key.upper()

alphabet = string.ascii_uppercase

table = [[] for _ in range(5)]


for char in key:

if char in alphabet:

alphabet = alphabet.replace(char, '')

table[0] = list(alphabet[0:5])

table[1] = list(alphabet[5:10])

table[2] = list(alphabet[10:15])

table[3] = list(alphabet[15:20])

table[4] = list(alphabet[20:25])

return table

def encrypt(table, text):

encrypted_text = ""

index = 0

while index < len(text):

if text[index] in string.ascii_uppercase:

encrypted_text += table[index % 5][string.ascii_uppercase.index(text[index]) // 5]

index += 1

if index < len(text) and text[index] in string.ascii_uppercase:

encrypted_text += table[index % 5][string.ascii_uppercase.index(text[index]) // 5]

index += 1

else:
break

else:

index += 1

return encrypted_text

def decrypt(table, text):

decrypted_text = ""

index = 0

while index < len(text):

if text[index] in string.ascii_uppercase:

decrypted_text += table[index % 5][string.ascii_uppercase.index(text[index]) // 5]

index += 1

if index < len(text) and text[index] in string.ascii_uppercase:

decrypted_text += table[index % 5][string.ascii_uppercase.index(text[index]) // 5]

index += 1

else:

break

else:

index += 1

return decrypted_text
# Usage example

key = "security"

plain_text = "attack on dawn"

table = generate_table(key)

encrypted_text = encrypt(table, plain_text)

decrypted_text = decrypt(table, encrypted_text)

print(f"Key: {key}")

print(f"Plain Text: {plain_text}")

print(f"Encrypted Text: {encrypted_text}")

print(f"Decrypted Text: {decrypted_text}")

You might also like