Compare commits
5 commits
c51f88103e
...
de98ad4268
Author | SHA1 | Date | |
---|---|---|---|
de98ad4268 | |||
493d31e3c6 | |||
5149846c61 | |||
4ca2829e4e | |||
376f2ccf5e |
2 changed files with 14 additions and 32 deletions
|
@ -25,34 +25,27 @@ def vigenere_de(ctext, vk):
|
||||||
result.append(ch)
|
result.append(ch)
|
||||||
return ''.join(result)
|
return ''.join(result)
|
||||||
|
|
||||||
|
|
||||||
def autokey_en(ptext, ak):
|
def autokey_en(ptext, ak):
|
||||||
result = []
|
k = ord(ak.upper()) if isinstance(ak, str) else ak
|
||||||
ptext = ptext.upper()
|
out = []
|
||||||
current_key = ak
|
for ch in ptext.upper():
|
||||||
for ch in ptext:
|
|
||||||
if ch.isalpha():
|
if ch.isalpha():
|
||||||
shift = (current_key - ord('A')) % 26
|
out_ch = chr((ord(ch) - 65 + (k - 65)) % 26 + 65)
|
||||||
cipher_char = chr((ord(ch) - ord('A') + shift) % 26 + ord('A'))
|
out.append(out_ch); k = ord(ch)
|
||||||
result.append(cipher_char)
|
|
||||||
current_key = ord(cipher_char)
|
|
||||||
else:
|
else:
|
||||||
result.append(ch)
|
out.append(ch)
|
||||||
return ''.join(result)
|
return ''.join(out)
|
||||||
|
|
||||||
def autokey_de(ctext, ak):
|
def autokey_de(ctext, ak):
|
||||||
result = []
|
k = ord(ak.upper()) if isinstance(ak, str) else ak
|
||||||
ctext = ctext.upper()
|
out = []
|
||||||
current_key = ak
|
for ch in ctext.upper():
|
||||||
for ch in ctext:
|
|
||||||
if ch.isalpha():
|
if ch.isalpha():
|
||||||
shift = (current_key - ord('A')) % 26
|
p = chr((ord(ch) - 65 - (k - 65)) % 26 + 65)
|
||||||
plain_char = chr((ord(ch) - ord('A') - shift) % 26 + ord('A'))
|
out.append(p); k = ord(p)
|
||||||
result.append(plain_char)
|
|
||||||
current_key = ord(plain_char)
|
|
||||||
else:
|
else:
|
||||||
result.append(ch)
|
out.append(ch)
|
||||||
return ''.join(result)
|
return ''.join(out)
|
||||||
|
|
||||||
def operator(argument,ptext,ak,vk):
|
def operator(argument,ptext,ak,vk):
|
||||||
match argument:
|
match argument:
|
||||||
|
|
|
@ -38,12 +38,6 @@ def int_to_fixed_length_bytes(value: int, length_bits: int) -> bytes:
|
||||||
|
|
||||||
|
|
||||||
def generate_keypair(p: int, g: int, private_bits: int = 256) -> tuple[int, int]:
|
def generate_keypair(p: int, g: int, private_bits: int = 256) -> tuple[int, int]:
|
||||||
"""
|
|
||||||
Generate a Diffie-Hellman private/public key pair.
|
|
||||||
|
|
||||||
- private_bits controls the size of the secret exponent for performance and security.
|
|
||||||
256 bits is a common choice for Group 14.
|
|
||||||
"""
|
|
||||||
a = 0
|
a = 0
|
||||||
while a < 2:
|
while a < 2:
|
||||||
a = number.getRandomNBitInteger(private_bits)
|
a = number.getRandomNBitInteger(private_bits)
|
||||||
|
@ -52,10 +46,6 @@ def generate_keypair(p: int, g: int, private_bits: int = 256) -> tuple[int, int]
|
||||||
|
|
||||||
|
|
||||||
def derive_shared_key(peer_public: int, private: int, p: int, key_len: int = 32) -> bytes:
|
def derive_shared_key(peer_public: int, private: int, p: int, key_len: int = 32) -> bytes:
|
||||||
"""
|
|
||||||
Compute the shared secret and derive a symmetric key using HKDF-SHA256.
|
|
||||||
Returns key_len bytes (default 32 bytes = 256-bit key).
|
|
||||||
"""
|
|
||||||
shared_secret = pow(peer_public, private, p)
|
shared_secret = pow(peer_public, private, p)
|
||||||
# Use fixed-length big-endian encoding of the shared secret for KDF input
|
# Use fixed-length big-endian encoding of the shared secret for KDF input
|
||||||
shared_bytes = int_to_fixed_length_bytes(shared_secret, p.bit_length())
|
shared_bytes = int_to_fixed_length_bytes(shared_secret, p.bit_length())
|
||||||
|
@ -63,7 +53,6 @@ def derive_shared_key(peer_public: int, private: int, p: int, key_len: int = 32)
|
||||||
return key
|
return key
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class TimingResult:
|
class TimingResult:
|
||||||
alice_keygen_s: float
|
alice_keygen_s: float
|
||||||
bob_keygen_s: float
|
bob_keygen_s: float
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue