In Spr Gms Revised Again
In Spr Gms Revised Again
ceaser
def caesar_cipher(text, shift, encrypt=True):
result = ""
shift = shift if encrypt else -shift # Reverse shift for
decryption
return result
# Example usage
text = "Hello, World!"
shift = 3
2.Monoalphabetic
def encrypt(a, dict1):
l = [] # Use a local list instead of a global variable
for x in a:
y = dict1.get(x, x) # If character not in dict, keep it
unchanged
l.append(y)
return "".join(l)
print("\nDecrypting...")
decrypted_text = decrypt(cipher_text, dict2)
print("Decrypted text:", decrypted_text)
3.diffie helman
# Public keys
Ya = pow(a, Xa, q) # (a^Xa) % q
Yb = pow(a, Xb, q) # (a^Xb) % q
4. ECC
from tinyec import registry
import secrets
B_SharedKey = Kb * X
print("Shared Key calculated by User B:", compress(B_SharedKey))
5.Vignere
for i in range(len(text)):
if text[i].isalpha(): # Process only alphabetic characters
shift = ord(key[i % key_length]) - ord('A') # Shift
based on key
if encryption:
new_char = chr((ord(text[i]) - ord('A') + shift) % 26
+ ord('A'))
else:
new_char = chr((ord(text[i]) - ord('A') - shift) % 26
+ ord('A'))
result += new_char
else:
result += text[i] # Keep non-alphabetic characters
unchanged
return result
# Example Usage
plaintext = "HELLOVIGENERE"
key = "KEY"
# Encryption
ciphertext = vigenere_cipher(plaintext, key, encryption=True)
print("Ciphertext:", ciphertext)
# Decryption
decrypted_text = vigenere_cipher(ciphertext, key, encryption=False)
print("Decrypted Text:", decrypted_text)
part b
1.fiestel cipher
import random
def generate_keys(num_rounds):
return [random.randint(0, 255) for _ in range(num_rounds)]
def text_to_pairs(text):
text_bytes = [ord(c) for c in text]
if len(text_bytes) % 2 != 0: # If odd length, pad with 0
text_bytes.append(0)
return [(text_bytes[i], text_bytes[i + 1]) for i in range(0,
len(text_bytes), 2)]
def pairs_to_text(pairs):
chars = [chr(num) for pair in pairs for num in pair if num != 0]
# Ignore padded zeros
return "".join(chars)
if __name__ == "__main__":
num_rounds = 4
keys = generate_keys(num_rounds)
text = "HELLO"
text_pairs = text_to_pairs(text)
encrypted_text = pairs_to_text(encrypted_pairs)
decrypted_text = pairs_to_text(decrypted_pairs)
2.hill cipher
if decrypt:
key = Matrix(key).inv_mod(26)
key = [[int(key[row, col]) for col in range(n)] for row in
range(n)]
while len(text) % n != 0:
text += 'X' # Padding with 'X' if the text length is not a
multiple of key size
result = ""
for i in range(0, len(text), n):
block = [ord(c) - ord('A') for c in text[i:i + n]]
processed_block = [
sum(key[row][col] * block[col] for col in range(n)) % 26
for row in range(n)
]
result += ''.join(chr(num + ord('A')) for num in
processed_block)
return result
3.Playfair
key = input("Enter a key: ")
key = key.upper()
keysAlready = []
mapper = {}
matrix = [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
i = 0
j = 0
print("Key Matrix:")
for row in matrix:
print(row)
# Plaintext modification
plainText = input("Enter a plaintext: ")
plainText = plainText.upper().replace("J", "I") # Replace 'J' with
'I' if present
if len(plainText) % 2 != 0:
plainText += "X"
# Encryption
cipherText = ""
for i in range(0, len(plainText), 2):
char1, char2 = plainText[i], plainText[i + 1]
row1, col1 = mapper[char1]
row2, col2 = mapper[char2]
if row1 == row2: # Same row
cipherText += matrix[row1][(col1 + 1) % 5]
cipherText += matrix[row2][(col2 + 1) % 5]
elif col1 == col2: # Same column
cipherText += matrix[(row1 + 1) % 5][col1]
cipherText += matrix[(row2 + 1) % 5][col2]
else: # Rectangle swap
cipherText += matrix[row1][col2]
cipherText += matrix[row2][col1]
print("Ciphertext:", cipherText)
# Decryption
decryptedText = ""
for i in range(0, len(cipherText), 2):
char1, char2 = cipherText[i], cipherText[i + 1]
row1, col1 = mapper[char1]
row2, col2 = mapper[char2]
initial_key =
"00010110011010101101001001101010101111001111001111110011011101101"
pc1_table = [
57, 49, 41, 33, 25, 17, 9, 1,
58, 50, 42, 34, 26, 18, 10, 2,
59, 51, 43, 35, 27, 19, 11, 3,
60, 52, 44, 36, 63, 55, 47, 39,
31, 23, 15, 7, 62, 54, 46, 38,
30, 22, 14, 6, 61, 53, 45, 37,
29, 21, 13, 5, 28, 20, 12, 4
]
key_rotation_schedule = [
1, 1, 2, 2, 2, 2, 2, 2,
1, 2, 2, 2, 2, 2, 2, 1
]
pc2_table = [
14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32
]
subkeys = []
for i in range(16):
c0 = c0[key_rotation_schedule[i]:] +
c0[:key_rotation_schedule[i]]
d0 = d0[key_rotation_schedule[i]:] +
d0[:key_rotation_schedule[i]]
cd = c0 + d0
subkey = [cd[pc2_table[j] - 1] for j in range(48)]
subkeys.append(subkey)
5.RSA
def gcd(a, b):
while b:
a, b = b, a % b
return a
def find_e(phi):
for i in range(2, phi):
if gcd(i, phi) == 1:
return i # Return the first valid e
RSA(p, q, msg)