|
| 1 | +import secrets |
| 2 | + |
| 3 | +random = secrets.SystemRandom() |
| 4 | + |
| 5 | +MSG = "CTF{????}" |
| 6 | + |
| 7 | + |
| 8 | +class PrivateKey: |
| 9 | + def __init__(self, length: int = 256, keytup: tuple = ()): |
| 10 | + if keytup: |
| 11 | + self.s1, self.s2, self.s, self.p1, self.p2, self.e1, self.e2 = keytup |
| 12 | + else: |
| 13 | + while True: |
| 14 | + self.s1 = self._gen_sequence(length) |
| 15 | + self.p1 = sum(self.s1) + 2 |
| 16 | + self.e1 = self._gen_pos_ints(self.p1) |
| 17 | + if is_prime(self.p1): break |
| 18 | + |
| 19 | + while True: |
| 20 | + self.s2 = self._gen_sequence(length) |
| 21 | + self.p2 = sum(self.s2) + 2 |
| 22 | + self.e2 = self._gen_pos_ints(self.p2) |
| 23 | + if is_prime(self.p2): break |
| 24 | + |
| 25 | + self.s = [self.s1[i] + self.s2[i] for i in range(length)] |
| 26 | + assert self.p1 != self.p2 |
| 27 | + |
| 28 | + def _gen_sequence(self, length: int) -> list[int]: |
| 29 | + return [random.getrandbits(128) for _ in range(length)] |
| 30 | + |
| 31 | + def _gen_pos_ints(self, p) -> int: |
| 32 | + return random.randint((p-1)//2, p-1) |
| 33 | + |
| 34 | + def export_secret(self): |
| 35 | + return {"s1": self.s1, "s2": self.s2, "s": self.s, |
| 36 | + "p1": self.p1, "p2": self.p2, "e1": self.e1, "e2": self.e2} |
| 37 | + |
| 38 | + |
| 39 | +class PublicKey: |
| 40 | + def __init__(self, private_key: PrivateKey): |
| 41 | + self.a1 = [(private_key.e1 * s) % private_key.p1 for s in private_key.s1] |
| 42 | + self.a2 = [(private_key.e2 * s) % private_key.p2 for s in private_key.s2] |
| 43 | + |
| 44 | + self.b1 = [i % 2 for i in private_key.s1] |
| 45 | + self.b2 = [i % 2 for i in private_key.s2] |
| 46 | + self.b = [i % 2 for i in private_key.s] |
| 47 | + |
| 48 | + self.t = random.randint(1, 2) |
| 49 | + self.c = self.b1 if self.t == 1 else self.b2 |
| 50 | + |
| 51 | + def public_key_export(self): |
| 52 | + return {"a1": self.a1, "a2": self.a2, "b": self.b, "c": self.c} |
| 53 | + |
| 54 | + |
| 55 | +class MHK2: |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + length: int, |
| 59 | + private_key: PrivateKey = PrivateKey, |
| 60 | + public_key: PublicKey = PublicKey, |
| 61 | + ): |
| 62 | + self.private_key = private_key(length) |
| 63 | + self.public_key = public_key(self.private_key) |
| 64 | + |
| 65 | + def _random_bin_sequence(self, n): |
| 66 | + return [random.randint(0, 1) for _ in range(n)] |
| 67 | + |
| 68 | + def encrypt(self, msg: str): |
| 69 | + ciphertext = [] |
| 70 | + msg_int = f'{(int.from_bytes(str.encode(msg), "big")):b}' |
| 71 | + for i in msg_int: |
| 72 | + ciphertext.append(self.encrypt_bit(int(i))) |
| 73 | + return ciphertext |
| 74 | + |
| 75 | + def decrypt(self, ciphertext): |
| 76 | + plaintext_bin = "" |
| 77 | + for i in ciphertext: |
| 78 | + plaintext_bin += str(self.decrypt_bit(i)) |
| 79 | + |
| 80 | + split_bin = [plaintext_bin[i : i + 7] for i in range(0, len(plaintext_bin), 8)] |
| 81 | + |
| 82 | + plaintext = "" |
| 83 | + for seq in split_bin: |
| 84 | + plaintext += chr(int(seq, 2)) |
| 85 | + return plaintext |
| 86 | + |
| 87 | + # single bit {0,1} |
| 88 | + def encrypt_bit(self, bit): |
| 89 | + r1 = self._random_bin_sequence(len(self.public_key.b)) |
| 90 | + r2 = self._random_bin_sequence(len(self.public_key.b)) |
| 91 | + |
| 92 | + m1 = sum([(self.public_key.b[i] * r1[i]) for i in range(len(r1))]) % 2 |
| 93 | + m2 = sum([(self.public_key.b[i] * r2[i]) for i in range(len(r2))]) % 2 |
| 94 | + |
| 95 | + eq = sum([(self.public_key.c[i] * r1[i]) for i in range(len(r1))]) == sum( |
| 96 | + [(self.public_key.c[i] * r2[i]) for i in range(len(r2))] |
| 97 | + ) |
| 98 | + |
| 99 | + while m1 != bit or m2 != bit or not eq or r1 == r2: |
| 100 | + r1 = self._random_bin_sequence(len(self.public_key.b)) |
| 101 | + r2 = self._random_bin_sequence(len(self.public_key.b)) |
| 102 | + |
| 103 | + m1 = ( |
| 104 | + sum( |
| 105 | + [ |
| 106 | + (self.public_key.b[i] * r1[i]) |
| 107 | + for i in range(len(self.public_key.b)) |
| 108 | + ] |
| 109 | + ) |
| 110 | + % 2 |
| 111 | + ) |
| 112 | + m2 = ( |
| 113 | + sum( |
| 114 | + [ |
| 115 | + (self.public_key.b[i] * r2[i]) |
| 116 | + for i in range(len(self.public_key.b)) |
| 117 | + ] |
| 118 | + ) |
| 119 | + % 2 |
| 120 | + ) |
| 121 | + |
| 122 | + eq = sum( |
| 123 | + [(self.public_key.c[i] * r1[i]) for i in range(len(self.public_key.b))] |
| 124 | + ) == sum( |
| 125 | + [(self.public_key.c[i] * r2[i]) for i in range(len(self.public_key.b))] |
| 126 | + ) |
| 127 | + |
| 128 | + C1 = sum([(self.public_key.a1[i] * r1[i]) for i in range(len(r1))]) |
| 129 | + C2 = sum([(self.public_key.a2[i] * r2[i]) for i in range(len(r2))]) |
| 130 | + return C1, C2 |
| 131 | + |
| 132 | + def decrypt_bit(self, ciphertext: tuple[int, int]) -> int: |
| 133 | + C1, C2 = ciphertext |
| 134 | + M1 = ( |
| 135 | + pow(self.private_key.e1, -1, self.private_key.p1) * C1 % self.private_key.p1 |
| 136 | + ) |
| 137 | + M2 = ( |
| 138 | + pow(self.private_key.e2, -1, self.private_key.p2) * C2 % self.private_key.p2 |
| 139 | + ) |
| 140 | + m = (M1 + M2) % 2 |
| 141 | + return m |
| 142 | + |
| 143 | + |
| 144 | +def main(): |
| 145 | + crypto = MHK2(256) |
| 146 | + ciphertext = crypto.encrypt(MSG) |
| 147 | + plaintext = crypto.decrypt(ciphertext) |
| 148 | + |
| 149 | + print(crypto.public_key.public_key_export()) |
| 150 | + print(ciphertext) |
| 151 | + |
| 152 | + assert plaintext == MSG |
| 153 | + |
| 154 | + |
| 155 | +if __name__ == "__main__": |
| 156 | + main() |
0 commit comments