├── DES.py ├── Jumbled_words_game.py ├── README.md └── cryptographicEncryption.py /DES.py: -------------------------------------------------------------------------------- 1 | from Crypto.Cipher import DES 2 | import os 3 | 4 | # Ppad the data to make it a multiple of 8 bytes 5 | def pad_data(data): 6 | while len(data) % 8 != 0: 7 | data += b' ' # Add space characters to pad 8 | return data 9 | 10 | # Encrypt the plaintext file using DES 11 | def encrypt_file(key, filename): 12 | with open(filename, 'rb') as f: 13 | plaintext = f.read() 14 | 15 | # Padding the plaintext 16 | plaintext = pad_data(plaintext) 17 | 18 | # Creating DES cipher 19 | des = DES.new(key, DES.MODE_ECB) 20 | 21 | # Encrypting the data 22 | ciphertext = des.encrypt(plaintext) 23 | 24 | # Converting the encrypted data to hexadecimal 25 | hex_ciphertext = ciphertext.hex() 26 | 27 | # Writing the hex-encoded encrypted data to cipher.txt 28 | with open('cipher.txt', 'w') as f: 29 | f.write(hex_ciphertext) 30 | 31 | print("Encryption complete. Encrypted hex text saved to cipher.txt") 32 | 33 | # Function to decrypt the ciphertext file using DES 34 | def decrypt_file(key, filename): 35 | # Reading hex-encoded ciphertext from the file 36 | with open(filename, 'r') as f: 37 | hex_ciphertext = f.read() 38 | 39 | # Converting the hex-encoded ciphertext back to bytes 40 | ciphertext = bytes.fromhex(hex_ciphertext) 41 | 42 | # Creating DES cipher 43 | des = DES.new(key, DES.MODE_ECB) 44 | 45 | # Decrypting the data 46 | decrypted_data = des.decrypt(ciphertext) 47 | 48 | # Writing the decrypted data to decrypted.txt 49 | with open('decrypted.txt', 'wb') as f: 50 | f.write(decrypted_data.strip()) # Remove padding 51 | 52 | print("Decryption complete. Decrypted text saved to decrypted.txt") 53 | 54 | # Convert key from binary or hexadecimal to bytes 55 | def get_key_in_bytes(key_input, key_format): 56 | if key_format.lower() == 'binary': 57 | return int(key_input, 2).to_bytes(8, 'big') 58 | elif key_format.lower() == 'hex': 59 | return bytes.fromhex(key_input) 60 | else: 61 | print("Invalid format. Please enter 'binary' or 'hexadecimal'.") 62 | return None 63 | 64 | if __name__ == "__main__": 65 | key_format = input("Enter key format (binary or hex): ").strip() 66 | key_input = input(f"Enter your {key_format} key (must be 64 bits): ").strip() 67 | key = get_key_in_bytes(key_input, key_format) 68 | if key is None or len(key) != 8: 69 | print("Error: Key must be exactly 64 bits (8 bytes) long!") 70 | else: 71 | filename = input("Enter the file name (plain.txt for encryption, cipher.txt for decryption): ") 72 | if filename == 'plain.txt': 73 | encrypt_file(key, filename) 74 | elif filename == 'cipher.txt': 75 | decrypt_file(key, filename) 76 | else: 77 | print("Invalid file. Please provide either 'plain.txt' or 'cipher.txt'.") 78 | -------------------------------------------------------------------------------- /Jumbled_words_game.py: -------------------------------------------------------------------------------- 1 | import random 2 | def choose_rand(): 3 | wordList = ["Purva", "Purvam", "Niya"] 4 | word = random.choice(wordList) 5 | return word 6 | 7 | def shuffle_letters(): 8 | original_word = choose_rand() 9 | shuffleList = random.sample(original_word, k = len(original_word)) 10 | shuffleWord = "" 11 | for i in shuffleList: 12 | shuffleWord += i 13 | return original_word,shuffleWord 14 | 15 | def play_game(): 16 | user1 = input("Player1 enter your name:") 17 | if user1.lower() == "stop" or user1.lower() == "exit": 18 | return "none","none" #used to return two values as when function is called it needs 2 values assigned 19 | user2 = input("Player2 enter your name:") 20 | if user2.lower() == "stop" or user2.lower() == "exit": 21 | return "none","none" 22 | return user1,user2 23 | 24 | def guess(guess): 25 | count = 0 26 | temp = guess.lower() 27 | word = list(temp) 28 | alpha_list = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] 29 | for i in word: 30 | if i in alpha_list: 31 | count += 1 32 | if count != len(word): 33 | return True 34 | else: 35 | return False 36 | 37 | def choose_turn(turn,user1,user2): 38 | if turn % 2 == 0: 39 | print(user1, "your turn") 40 | word = input(f'{user1} your answer is:') 41 | while guess(word): 42 | print("Incorrect format! Please enter the correct format (only alphabets)") 43 | word = input(f'{user1} your answer is:') 44 | return word 45 | else: 46 | print(user2, "your turn") 47 | word1 = input(f'{user2} your answer is:') 48 | while guess(word1): 49 | print("Incorrect format! Please enter the correct format (only alphabets)") 50 | word1 = input(f'{user2} your answer is:') 51 | return word1 52 | 53 | def print_score(turn,score1,score2): 54 | print(f'Your score:{score1}') if turn % 2 == 0 else print(f'Your score:{score2}') 55 | 56 | 57 | score1,score2 = 0,0 58 | print("Welcome to Two-Player-Jumbled-Words Game") 59 | while True: 60 | user1, user2 = play_game() 61 | if user1 == "none" or user2 == "none": #Used to break when user enters stop or break 62 | print("Game Ends!") 63 | break 64 | turn = 0 65 | while True: 66 | original_word , jumbled_word = shuffle_letters() 67 | print("Jumbled word is:", jumbled_word) 68 | word = choose_turn(turn,user1,user2) 69 | if original_word == guess: 70 | print("Right Answer!") 71 | if turn % 2 == 0: 72 | score1 += 1 73 | else: 74 | score2 += 1 75 | print_score(turn,score1,score2) 76 | turn += 1 77 | elif guess.lower() == "stop" or guess.lower() == "exit": 78 | print("Game Ends!") 79 | 80 | else: 81 | print("You are wrong!") 82 | print_score(turn, score1, score2) 83 | turn += 1 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # University Coursework Projects 2 | 3 | - Welcome to my university coursework repository! This repository includes Python projects I completed as part of my coursework. Feel free to refer to the code for understanding and learning purposes. 4 | 5 | ## Projects 6 | 7 | ### Jumbled_words_game.py 8 | - A Python script for a jumbled words game, where the objective is to guess jumbled words to form meaningful words. 9 | 10 | ### cryptographicEncryption.py 11 | - A Python script demonstrating basic cryptographic encryption and decryption techniques, providing insights into how encryption and decryption are handled. 12 | 13 | ### DES.py 14 | - Python script using the `pycryptodome` module to encrypt and decrypt data from files (plain.txt and cipher.txt) using DES. 15 | 16 | ## Usage 17 | 18 | - You can run each script individually to see how they work. These projects are designed to help you understand various Python programming concepts. Feel free to use and modify the code for educational purposes. However, please do not use it for commercial purposes without permission. 19 | 20 | ## Author 21 | 22 | - **Purva Patel** 23 | - **Purvam Patel** 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /cryptographicEncryption.py: -------------------------------------------------------------------------------- 1 | import random 2 | import string 3 | import math 4 | 5 | 6 | class Message: 7 | """Base class for messages.""" 8 | 9 | def __init__(self, message): 10 | self.message = message 11 | 12 | def __str__(self): 13 | return self.message 14 | 15 | def apply_cipher(self, cipher): 16 | """Applies the given cipher to the message.""" 17 | self.message = cipher.encrypt(self.message) 18 | 19 | 20 | class plaintextMsg(Message): 21 | """Subclass of Message for plaintext messages.""" 22 | 23 | def encrypt(self, cipher): 24 | """Encrypts the message using the given cipher.""" 25 | self.apply_cipher(cipher) 26 | return ciphertextMsg(self.message) 27 | 28 | 29 | class ciphertextMsg(Message): 30 | """Subclass of Message for ciphertext messages.""" 31 | 32 | def decrypt(self, cipher): 33 | """Decrypts the message using the given cipher.""" 34 | self.apply_cipher(cipher) 35 | return plaintextMsg(self.message) 36 | 37 | 38 | class SubstitutionCipher: 39 | """Class implementing the Substitution cipher.""" 40 | 41 | def __init__(self): 42 | self.mapping = {} 43 | chars = list(string.ascii_lowercase) 44 | random.shuffle(chars) 45 | for i, c in enumerate(string.ascii_lowercase): 46 | self.mapping[c] = chars[i] 47 | 48 | def encrypt(self, message): 49 | return ''.join([self.mapping.get(c.lower(), c) for c in message]) 50 | 51 | 52 | class PlayfairCipher: 53 | """Class implementing the Playfair cipher.""" 54 | 55 | def __init__(self): 56 | self.alphabet = string.ascii_lowercase.replace('j', 'i') 57 | self.matrix = [] 58 | for i in range(5): 59 | row = [] 60 | for j in range(5): 61 | row.append(self.alphabet[i*5+j]) 62 | self.matrix.append(row) 63 | 64 | def find_char(self, char): 65 | """Returns the row and column of the given character in the matrix.""" 66 | for i in range(5): 67 | for j in range(5): 68 | if self.matrix[i][j] == char: 69 | return i, j 70 | return None 71 | 72 | def encrypt(self, message): 73 | # Pad message with 'x' if necessary 74 | if len(message) % 2 != 0: 75 | message += 'x' 76 | # Split message into pairs of characters 77 | pairs = [message[i:i+2] for i in range(0, len(message), 2)] 78 | # Apply cipher to each pair 79 | result = '' 80 | for pair in pairs: 81 | c1, c2 = pair 82 | i1, j1 = self.find_char(c1) 83 | i2, j2 = self.find_char(c2) 84 | if i1 == i2: 85 | j1 = (j1 + 1) % 5 86 | j2 = (j2 + 1) % 5 87 | elif j1 == j2: 88 | i1 = (i1 + 1) % 5 89 | i2 = (i2 + 1) % 5 90 | else: 91 | j1, j2 = j2, j1 92 | result += self.matrix[i1][j1] + self.matrix[i2][j2] 93 | return result 94 | 95 | 96 | class CaesarCipher: 97 | """Class implementing the Caesar cipher.""" 98 | 99 | def __init__(self): 100 | self.shift = random.randint(1, 25) 101 | 102 | def encrypt(self, message): 103 | result = '' 104 | for c in message: 105 | if c.isalpha(): 106 | if c.isupper(): 107 | result += chr((ord(c) - ord('A') + self.shift) % 26 + ord('A')) 108 | else: 109 | result += chr((ord(c) - ord('a') + self.shift) % 26 + ord('a')) 110 | 111 | 112 | class ProductCipher(Message): 113 | def init(self): 114 | super().init() 115 | 116 | def encrypt(self, message): 117 | sub_message_1 = message[::2] 118 | sub_message_2 = message[1::2] 119 | encrypted_message = sub_message_2 + sub_message_1 120 | return encrypted_message 121 | 122 | def decrypt(self, message): 123 | mid = len(message) // 2 124 | sub_message_1 = message[mid:] 125 | sub_message_2 = message[:mid] 126 | decrypted_message = "" 127 | for i in range(mid): 128 | decrypted_message += sub_message_1[i] + sub_message_2[i] 129 | if len(sub_message_1) > len(sub_message_2): 130 | decrypted_message += sub_message_1[-1] 131 | return decrypted_message 132 | 133 | class RSA(Message): 134 | def init(self): 135 | super().init() 136 | 137 | 138 | def encrypt(self, message): 139 | n, e = 2537, 13 140 | encrypted_message = [(ord(char) ** e) % n for char in message] 141 | return encrypted_message 142 | 143 | def decrypt(self, message): 144 | n, d = 2537, 937 145 | decrypted_message = "" 146 | for num in message: 147 | decrypted_message += chr((num ** d) % n) 148 | return decrypted_message 149 | 150 | 151 | 152 | def main(): 153 | ciphers = [SubstitutionCipher(), PlayfairCipher(), CaesarCipher(), TranspositionCipher(), ProductCipher(), RSA()] 154 | plaintext_messages = [] 155 | encrypted_messages = [] 156 | methods = [] 157 | while True: 158 | message = input("Enter a message to encrypt (or 'Stop' to end the program): ") 159 | if message.lower() == "stop": 160 | break 161 | cipher = random.choice(ciphers) 162 | methods.append(type(cipher).name) 163 | plaintext_messages.append(message) 164 | encrypted_messages.append(cipher.encrypt(message)) 165 | print("Results:") 166 | for i in range(len(plaintext_messages)): 167 | print("Method: ", methods[i]) 168 | print("Original message: ", plaintext_messages[i]) 169 | print("Encrypted message: ", encrypted_messages[i]) 170 | print("Decrypted message: ", ciphers[i].decrypt(encrypted_messages[i])) 171 | print() 172 | 173 | if __name__ == "main": 174 | main() 175 | --------------------------------------------------------------------------------