[go: up one dir, main page]

0% found this document useful (0 votes)
18 views4 pages

PDS Ex 1 Implementation Theory and Practical

The document contains Python implementations of various cryptographic algorithms including GCD, Caesar Cipher, Multiplicative Cipher, Affine Cipher, Vigenere Cipher, Hill Cipher, RC4, RSA, AES, and DES. Each algorithm is accompanied by example usage demonstrating encryption and decryption processes. The document provides a comprehensive overview of classical and modern encryption techniques.

Uploaded by

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

PDS Ex 1 Implementation Theory and Practical

The document contains Python implementations of various cryptographic algorithms including GCD, Caesar Cipher, Multiplicative Cipher, Affine Cipher, Vigenere Cipher, Hill Cipher, RC4, RSA, AES, and DES. Each algorithm is accompanied by example usage demonstrating encryption and decryption processes. The document provides a comprehensive overview of classical and modern encryption techniques.

Uploaded by

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

# 1.

GCD (Greatest Common Divisor)


def gcd(a, b):
while b:
a, b = b, a % b
return a

a, b = 56, 98
print("GCD:", gcd(a, b))

# 2. Caesar Cipher
def caesar_encrypt(plaintext, key):
ciphertext = ""
for char in plaintext:
if char.isalpha():
shift = 65 if char.isupper() else 97
ciphertext += chr((ord(char) - shift + key) % 26 + shift)
else:
ciphertext += char
return ciphertext

def caesar_decrypt(ciphertext, key):


return caesar_encrypt(ciphertext, -key)

plaintext = "HELLO"
key = 3
encrypted = caesar_encrypt(plaintext, key)
decrypted = caesar_decrypt(encrypted, key)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

# 3. Multiplicative Cipher
def multiplicative_encrypt(plaintext, key):
ciphertext = ''
for char in plaintext.upper():
if char.isalpha():
ciphertext += chr(((ord(char) - 65) * key) % 26 + 65)
else:
ciphertext += char
return ciphertext

def multiplicative_decrypt(ciphertext, key):


def mod_inverse(a, m):
for x in range(1, m):
if (a * x) % m == 1:
return x
return -1

inverse_key = mod_inverse(key, 26)


plaintext = ''
for char in ciphertext:
if char.isalpha():
plaintext += chr(((ord(char) - 65) * inverse_key) % 26 + 65)
else:
plaintext += char
return plaintext

plaintext = "HELLO"
key = 9
encrypted = multiplicative_encrypt(plaintext, key)
decrypted = multiplicative_decrypt(encrypted, key)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

# 4. Affine Cipher
def mod_inverse(a, m):
for x in range(1, m):
if (a * x) % m == 1:
return x
return -1

def affine_encrypt(plaintext, a, b):


ciphertext = ''
for char in plaintext.upper():
if char.isalpha():
ciphertext += chr(((a * (ord(char) - 65) + b) % 26) + 65)
else:
ciphertext += char
return ciphertext

def affine_decrypt(ciphertext, a, b):


inverse_a = mod_inverse(a, 26)
plaintext = ''
for char in ciphertext:
if char.isalpha():
plaintext += chr(((inverse_a * ((ord(char) - 65) - b)) % 26) + 65)
else:
plaintext += char
return plaintext

plaintext = "HELLO"
a, b = 5, 8
encrypted = affine_encrypt(plaintext, a, b)
decrypted = affine_decrypt(encrypted, a, b)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

# 5. Vigenere Cipher
def vigenere_encrypt(plaintext, key):
key = key.upper()
ciphertext = ""
for i, char in enumerate(plaintext.upper()):
if char.isalpha():
shift = ord(key[i % len(key)]) - 65
ciphertext += chr((ord(char) - 65 + shift) % 26 + 65)
else:
ciphertext += char
return ciphertext

def vigenere_decrypt(ciphertext, key):


key = key.upper()
plaintext = ""
for i, char in enumerate(ciphertext.upper()):
if char.isalpha():
shift = ord(key[i % len(key)]) - 65
plaintext += chr((ord(char) - 65 - shift) % 26 + 65)
else:
plaintext += char
return plaintext

plaintext = "HELLO"
key = "KEY"
encrypted = vigenere_encrypt(plaintext, key)
decrypted = vigenere_decrypt(encrypted, key)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

# 6. 3x3 Hill Cipher


import numpy as np
def hill_encrypt(message, key_matrix):
message_vector = [ord(char) - 65 for char in message]
cipher_vector = np.dot(key_matrix, message_vector) % 26
ciphertext = ''.join([chr(num + 65) for num in cipher_vector])
return ciphertext

key_matrix = np.array([[6, 24, 1], [13, 16, 10], [20, 17, 15]])
plaintext = "ACT"
ciphertext = hill_encrypt(plaintext, key_matrix)
print("Ciphertext:", ciphertext)

# 7. RC4 Cipher
def rc4_encrypt(plaintext, key):
S = list(range(256))
j = 0
for i in range(256):
j = (j + S[i] + ord(key[i % len(key)])) % 256
S[i], S[j] = S[j], S[i]

i = j = 0
ciphertext = []
for char in plaintext:
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
K = S[(S[i] + S[j]) % 256]
ciphertext.append(chr(ord(char) ^ K))

return ''.join(ciphertext)

plaintext = "HELLO"
key = "KEY"
encrypted = rc4_encrypt(plaintext, key)
decrypted = rc4_encrypt(encrypted, key)
print("Encrypted:", ''.join([format(ord(c), '02X') for c in encrypted]))
print("Decrypted:", decrypted)

# 8. RSA
def gcd(a, b):
while b:
a, b = b, a % b
return a

def mod_inverse(e, phi):


for d in range(1, phi):
if (e * d) % phi == 1:
return d

p, q = 61, 53
n = p * q
phi = (p-1) * (q-1)
e = 17
d = mod_inverse(e, phi)

message = 65
ciphertext = pow(message, e, n)
decrypted = pow(ciphertext, d, n)

print("Ciphertext:", ciphertext)
print("Decrypted:", decrypted)

# 9. AES Key Generation (Manual Implementation)


def xor_bytes(a, b):
return bytes([x ^ y for x, y in zip(a, b)])

def aes_key_schedule(key):
# Simple key expansion (not full AES key schedule)
round_keys = [key]
for i in range(1, 11):
round_keys.append(xor_bytes(round_keys[-1], key))
return round_keys

key = bytes([0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x97,
0x75, 0x46, 0x9b, 0x6b, 0x2a]) # 16 bytes for AES-128
round_keys = aes_key_schedule(key)
print("AES Round Keys:", round_keys)

# AES Encryption and Decryption (simplified, not using full AES)


def aes_encrypt_decrypt(plaintext, round_keys):
ciphertext = xor_bytes(plaintext, round_keys[0])
for i in range(1, len(round_keys)):
ciphertext = xor_bytes(ciphertext, round_keys[i])
return ciphertext

plaintext = bytes([0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a, 0x31, 0x37, 0xf6, 0x30,
0x98, 0x07, 0xa3, 0x8d, 0xa0, 0x7a])
encrypted = aes_encrypt_decrypt(plaintext, round_keys)
decrypted = aes_encrypt_decrypt(encrypted, round_keys)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

# 10. DES Key Generation (Manual Implementation)


def permute_bits(key, permutation):
return [key[i] for i in permutation]

key = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef] # 8-byte key
permutation = [0, 1, 2, 3, 4, 5, 6, 7] # Example (simple, not full DES)
permuted_key = permute_bits(key, permutation)
print("DES Key:", permuted_key)

# DES Encryption and Decryption (simplified, not using full DES)


def des_encrypt_decrypt(plaintext, key):
return xor_bytes(plaintext, key)

plaintext = bytes([0x32, 0x88, 0x31, 0xe0, 0x43, 0x5a, 0x31, 0x37])


key = bytes([0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef]) # DES key
encrypted = des_encrypt_decrypt(plaintext, key)
decrypted = des_encrypt_decrypt(encrypted, key)
print("Encrypted:", encrypted)
print("Decrypted:", decrypted)

You might also like