From b80428d7164eea19d1af876ea3eefaa0224d7806 Mon Sep 17 00:00:00 2001 From: aishvarya252 Date: Sat, 7 Dec 2024 00:59:20 +0530 Subject: [PATCH 1/4] Added cipher code --- pol | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 pol diff --git a/pol b/pol new file mode 100644 index 000000000000..bd2209cc0348 --- /dev/null +++ b/pol @@ -0,0 +1,55 @@ +def encrypt(text, key): + + #Encrypts the plaintext using the polyalphabetic cipher. + + encrypted_text = [] + key = key.upper() + key_length = len(key) + key_index = 0 + + for char in text: + if char.isalpha(): + shift = ord(key[key_index]) - ord('A') + if char.isupper(): + new_char = chr((ord(char) - ord('A') + shift) % 26 + ord('A')) + else: + new_char = chr((ord(char) - ord('a') + shift) % 26 + ord('a')) + encrypted_text.append(new_char) + key_index = (key_index + 1) % key_length + else: + encrypted_text.append(char) + + return ''.join(encrypted_text) + + +def decrypt(text, key): + #Decrypts the ciphertext using the polyalphabetic cipher + + decrypted_text = [] + key = key.upper() + key_length = len(key) + key_index = 0 + + for char in text: + if char.isalpha(): + shift = ord(key[key_index]) - ord('A') + if char.isupper(): + new_char = chr((ord(char) - ord('A') - shift) % 26 + ord('A')) + else: + new_char = chr((ord(char) - ord('a') - shift) % 26 + ord('a')) + decrypted_text.append(new_char) + key_index = (key_index + 1) % key_length + else: + decrypted_text.append(char) + + return ''.join(decrypted_text) + + +plaintext = "HELLO, WORLD!" +key = "KEY" + +ciphertext = encrypt(plaintext, key) +print("Encrypted Text:", ciphertext) + +decrypted_text = decrypt(ciphertext, key) +print("Decrypted Text:", decrypted_text) From 7d9e8c28307e166408722e07b301a169be584e24 Mon Sep 17 00:00:00 2001 From: aishvarya252 Date: Sat, 7 Dec 2024 01:03:11 +0530 Subject: [PATCH 2/4] updated file name --- pol => polyalphabetic cipher | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename pol => polyalphabetic cipher (100%) diff --git a/pol b/polyalphabetic cipher similarity index 100% rename from pol rename to polyalphabetic cipher From da34c0591901fe2955de942a525e2c5f6ffd5ddd Mon Sep 17 00:00:00 2001 From: aishvarya252 Date: Sat, 7 Dec 2024 01:07:01 +0530 Subject: [PATCH 3/4] renamed file --- polyalphabetic cipher => polyalphabetic_cipher | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename polyalphabetic cipher => polyalphabetic_cipher (100%) diff --git a/polyalphabetic cipher b/polyalphabetic_cipher similarity index 100% rename from polyalphabetic cipher rename to polyalphabetic_cipher From c42da95d86b79d2b6fec4a3310b88e5253804d35 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Dec 2024 19:39:35 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- polyalphabetic_cipher | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/polyalphabetic_cipher b/polyalphabetic_cipher index bd2209cc0348..8771cbffc4a2 100644 --- a/polyalphabetic_cipher +++ b/polyalphabetic_cipher @@ -8,14 +8,14 @@ def encrypt(text, key): key_index = 0 for char in text: - if char.isalpha(): + if char.isalpha(): shift = ord(key[key_index]) - ord('A') if char.isupper(): new_char = chr((ord(char) - ord('A') + shift) % 26 + ord('A')) else: new_char = chr((ord(char) - ord('a') + shift) % 26 + ord('a')) encrypted_text.append(new_char) - key_index = (key_index + 1) % key_length + key_index = (key_index + 1) % key_length else: encrypted_text.append(char) @@ -31,14 +31,14 @@ def decrypt(text, key): key_index = 0 for char in text: - if char.isalpha(): + if char.isalpha(): shift = ord(key[key_index]) - ord('A') if char.isupper(): new_char = chr((ord(char) - ord('A') - shift) % 26 + ord('A')) else: new_char = chr((ord(char) - ord('a') - shift) % 26 + ord('a')) decrypted_text.append(new_char) - key_index = (key_index + 1) % key_length + key_index = (key_index + 1) % key_length else: decrypted_text.append(char)