0% found this document useful (0 votes)
2 views

python lecture

The document contains a Python program that implements two encryption methods: an odd-even transposition cipher and a random substitution cipher. The odd-even cipher separates characters based on their index parity for encryption and reconstructs the original message during decryption. The random substitution cipher uses a shuffled character set to encrypt and decrypt messages based on character indices.

Uploaded by

charmig prince
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)
2 views

python lecture

The document contains a Python program that implements two encryption methods: an odd-even transposition cipher and a random substitution cipher. The odd-even cipher separates characters based on their index parity for encryption and reconstructs the original message during decryption. The random substitution cipher uses a shuffled character set to encrypt and decrypt messages based on character indices.

Uploaded by

charmig prince
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/ 3

def odd_even_encrypt(message):

"""Encrypts a message using the odd-even transposition cipher."""


odd_chars = ""
even_chars = ""
for i, char in enumerate(message):
if i % 2 != 0: # Check if index is odd
odd_chars += char
else:
even_chars += char
return odd_chars + even_chars

def odd_even_decrypt(ciphertext):
"""Decrypts a ciphertext using the odd-even transposition cipher."""
length = len(ciphertext)
odd_length = length // 2
even_length = length - odd_length
odd_chars = ciphertext[:odd_length]
even_chars = ciphertext[odd_length:]

plaintext = ""
for i in range(even_length):
plaintext += even_chars[i]
if i < odd_length:
plaintext += odd_chars[i]
return plaintext

# Get user input


user_message = input("Enter a message to encrypt: ")

# Encryption
encrypted_message = odd_even_encrypt(user_message)
print("Encrypted message:", encrypted_message)

# Decryption
decrypted_message = odd_even_decrypt(encrypted_message)
print("Decrypted message:", decrypted_message)

Encryption program in python:

import random

import string
chars = " " + string.punctuation + string.digits + string.ascii_letters

chars = list(chars)

key = chars.copy()

random.shuffle(key)

#ENCRYPT

plain_text = input("Enter a message to encrypt: ")

cipher_text = ""

for letter in plain_text:

index = chars.index(letter)

cipher_text += key[index]

print(f"original message : {plain_text}")

print(f"encrypted message: {cipher_text}")

#DECRYPT

cipher_text = input("Enter a message to encrypt: ")

plain_text = ""

for letter in cipher_text:

index = key.index(letter)

plain_text += chars[index]
print(f"encrypted message: {cipher_text}")

print(f"original message : {plain_text}")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy