Complex Variables and Linear Algebra Submission 1
Complex Variables and Linear Algebra Submission 1
cipher = {}
initial_ascii_cap = 65
initial_ascii_low = 97
for i in range(26):
char = chr(initial_ascii_cap + i)
cipher[char] = i
for i in range(26):
char = chr(initial_ascii_low + i)
cipher[char] = i
cip = ''
for j in plaintext:
if j.isalpha():
updatedval = cipher[j] + k
updatedval %= 26
if j.islower():
c = chr(updatedval + initial_ascii_low)
else:
c = chr(updatedval + initial_ascii_cap)
cip += c
else:
print(cip)
hill cipher
import numpy as np
if (a * x) % m == 1:
return x
return None
n = len(key)
pt = pad_text(pt, n)
pt_n = ttn(pt)
cipher = []
block = pt_n[i:i+n]
cipher.extend(enc_block)
return ntt(cipher)
n = len(key)
det = int(round(np.linalg.det(key)))
if det_inv is None:
key_inverse = (
) % 26
cipher = ttn(c)
pt_n = []
for i in range(0, len(cipher), n):
block = cipher[i:i+n]
pt_n.extend(dec_block)
return ntt(pt_n).rstrip('X')
# Example usage
key_m = np.array([[6, 24, 1], [13, 16, 10], [20, 17, 15]])
pt = "HELLO"
print(f"Encrypted = {enc}")
print(f"Decrypted = {dec}")
Vignete cipher
cipher = {}
initial_ascii_cap = 65
initial_ascii_low = 97
for i in range(26):
cipher[chr(initial_ascii_cap + i)] = i
cipher[chr(initial_ascii_low + i)] = i
cip = ''
vals = []
vals2 = []
for j in plaintext:
if j.isalpha():
val = cipher[j]
vals.append(val)
else:
extended_key = ''
key_index = 0
for ch in plaintext:
if ch.isalpha():
key_index += 1
else:
extended_key += ch
for j in extended_key:
if j.isalpha():
val = cipher[j]
vals2.append(val)
else:
vals2.append(j)
# Perform encryption
for i in range(len(vals)):
if isinstance(vals[i], int):
if plaintext[i].islower():
c = chr(new_val + initial_ascii_low)
else:
c = chr(new_val + initial_ascii_cap)
cip += c
else:
print(cip)
playfair cipher
def generate_playfair_matrix(key):
matrix = []
seen = set()
for char in key + "ABCDEFGHIKLMNOPQRSTUVWXYZ":
seen.add(char)
matrix.append(char)
for i in range(5):
for j in range(5):
if matrix[i][j] == char:
return i, j
return None
def prepare_text(text):
prepared = ""
i=0
char1 = text[i]
if char1 == char2:
i += 1
else:
i += 2
if len(prepared) % 2 != 0:
prepared += 'X'
return prepared
matrix = generate_playfair_matrix(key)
text = prepare_text(text)
encrypted = ""
encrypted += matrix[row1][(col1 + 1) % 5]
encrypted += matrix[row2][(col2 + 1) % 5]
encrypted += matrix[row1][col2]
encrypted += matrix[row2][col1]
return encrypted
matrix = generate_playfair_matrix(key)
decrypted = ""
decrypted += matrix[row1][(col1 - 1) % 5]
decrypted += matrix[row2][(col2 - 1) % 5]
decrypted += matrix[row1][col2]
decrypted += matrix[row2][col1]
return decrypted
RSA
import random
while b != 0:
a, b = b, a % b
return a
m0, x0, x1 = m, 0, 1
if m == 1:
return 0
while a > 1:
q = a // m
m, a = a % m, m
x0, x1 = x1 - q * x0, x0
if x1 < 0:
x1 += m0
return x1
def is_prime(num):
if num < 2:
return False
if num % i == 0:
return False
return True
elif p == q:
n=p*q
phi = (p - 1) * (q - 1)
e = random.randrange(1, phi)
g = gcd(e, phi)
while g != 1:
e = random.randrange(1, phi)
g = gcd(e, phi)
d = modinv(e, phi)
key, n = pk
return cipher
key, n = pk
return ''.join(plain)
if __name__ == '__main__':
p = 61
q = 53
dewie hellman
import random
a = random.randint(1, p - 1)
A = pow(g, a, p)
return a, A
return pow(B, a, p)
p = 23
g=5
a, A = gen_keys(p, g)
b, B = gen_keys(p, g)
shared_key_a = compute_sk(a, B, p)
shared_key_b = compute_sk(b, A, p)
print(shared_key_a == shared_key_b)
Elgamal
import random
while b != 0:
a, b = b, a % b
return a
result = 1
if exp % 2 == 1:
exp //= 2
return result
def generate_keys(p):
g = random.randint(2, p - 1)
x = random.randint(1, p - 2)
h = mod_exp(g, x, p)
return (p, g, h), x
p, g, h = public_key
y = random.randint(1, p - 2)
c1 = mod_exp(g, y, p)
return c1, c2
p, g, h = public_key
c1, c2 = ciphertext
s = mod_exp(c1, private_key, p)
return plaintext
plaintext = 123
print("Ciphertext:", ciphertext)
decrypted_message = decrypt(private_key, public_key, ciphertext)
RSA question
while b:
a, b = b, a % b
return a
d_old, d = 0, 1
r_old, r = phi, e
while r != 0:
q = r_old // r
r_old, r = r, r_old - q * r
d_old, d = d, d_old - q * d
n=p*q
phi = (p - 1) * (q - 1)
d = modinv(e, phi)
C = pow(M, e, n)
decrypted = pow(C, d, n)
return {
test_cases = [
(3, 7, 5, 10),
(17, 23, 9, 7)
result = rsa_encrypt_decrypt(p, q, e, M)
result = 1
if exp % 2 == 1:
return result
# Input
# Public keys
# Secret keys
# Output
print(f"Alpha = {alpha}")
print(f"q = {q}")
if ka == kb:
print("✅ Both Alice and Bob have the same secret key.")
else:
SHA
print("Sample Result")
multiplier = 1
multiplier += 1
print("The total size of the blocks after including the padding bits is:", padding_size + n +
128)
print(paddingBitsString)
MD5
def itos(n):
def dec_to_binary(n):
res = ""
count = 8
i=n
return res
i //= 2
count -= 1
return res
def main():
input_chain = []
padding_chain = []
for ch in inp:
temp = dec_to_binary(ord(ch))
input_chain.append(temp)
total_message_bits = n * 8
i=1
i += 1
x = (512 * i) - 64 - total_message_bits
message_length = dec_to_binary(total_message_bits)
padding_chain.append("10000000")
padding_blocks = x // 8
temp = padding_blocks - 1
while temp != 0:
padding_chain.append("00000000")
temp -= 1
for b in padding_chain:
print(f"\nThe Message in bits are below (The size of the message is:
{total_message_bits}):")
if __name__ == "__main__":
main()
s_box = [
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7,
0xAB,….
]
rcon = [0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36]
def rot_word(word):
def sub_word(word):
def key_expansion(key):
key_size = len(key)
assert key_size == 16
expanded = []
word = list(key[i:i+4])
expanded.append(word)
if i % 4 == 0:
temp = rot_word(temp)
temp = sub_word(temp)
temp[0] ^= rcon[(i // 4) - 1]
expanded.append(new_word)
return expanded
sample_key = bytes.fromhex("4A1A607DD64C444F6B2750E0975FE72E")
expanded_keys = key_expansion(sample_key)
round4_key = expanded_keys[16:20]
DES simulation
output = 0
return output
ip_table = [
58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,…
fp_table = [
40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23,…
SHA da
def print_hex_vector(vec):
print(f"W{i} = {val:016X}")
msg_bytes = message.encode("utf-8")
original_bit_length = len(msg_bytes) * 8
# === (a) Padding bits calculation ===
W = [0] * 80
for i in range(len(msg_bytes)):
W[15] = original_bit_length
print_hex_vector(W[:18])
A = 0x6a09e667f3bcc908
B = 0xbb67ae8584caa73b
C = 0x3c6ef372fe94f82b
majority_result = majority(A, B, C)
E = 0x510e527fade682d1
F = 0x9b05688c2b3e6c1f
G = 0x1f83d9abfb41bd6b
condition_result = condition(E, F, G)
Absolutely! Here's a brief algorithmic description for each of the provided code snippets:
1. Caesar Cipher:
2. Hill Cipher:
3. Vigenère Cipher:
4. Playfair Cipher:
5. RSA:
6. Diffie-Hellman:
• Input: Prime number (p), generator (g), private keys (a, b).
• Process:
o Calculate public keys:
§ A = g^a mod p
§ B = g^b mod p
o Calculate shared secret keys:
§ ka = B^a mod p
§ kb = A^b mod p
• Output: Shared secret key.
7. ElGamal:
• Input: Two prime numbers (p, q), public exponent (e), message (M).
• Process:
o Calculate n = p * q and phi = (p-1) * (q-1).
oCalculate private exponent d (modular inverse of e mod phi).
oEncryption: C = M^e mod n.
oDecryption: M = C^d mod n.
• Output: n, phi, d, encrypted message (C), decrypted message (M).
9. Diffie-Hellman Backup:
• Input: primitive root alpha, prime number q, private keys xa and xb.
• Process:
o calculate public keys ya and yb.
o calculate shared key ka and kb.
• Output: public keys, and shared secret key.
10. SHA:
11. MD5: