31 lines
1.3 KiB
Python
31 lines
1.3 KiB
Python
import os
|
|
from Crypto.Cipher import ChaCha20
|
|
|
|
def encrypt_message(message, key, nonce):
|
|
cipher = ChaCha20.new(key=key, nonce=nonce)
|
|
ciphertext = cipher.encrypt(message)
|
|
return ciphertext
|
|
|
|
def chosen_plaintext_attack(ciphertext, key, nonce):
|
|
# Create a dictionary of known plaintexts and their corresponding ciphertexts
|
|
known_plaintexts = {b"Our counter agencies have": b"", b"example2": b""}
|
|
for plaintext in known_plaintexts:
|
|
# Encrypt the known plaintext using the same key and nonce as the original ciphertext
|
|
known_ciphertext = encrypt_message(plaintext, key, nonce)
|
|
# Compare the known ciphertext with the original ciphertext
|
|
if known_ciphertext == ciphertext:
|
|
# If the ciphertexts match, the original plaintext is likely the known plaintext
|
|
return plaintext
|
|
return None
|
|
|
|
if __name__ == "__main__":
|
|
message = b"Our counter agencies have intercepted your messages and a lot"
|
|
key, nonce = os.urandom(32), os.urandom(12)
|
|
encrypted_message = encrypt_message(message, key, nonce)
|
|
|
|
# Perform the chosen plaintext attack
|
|
recovered_message = chosen_plaintext_attack(encrypted_message, key, nonce)
|
|
if recovered_message is not None:
|
|
print("Recovered message:", recovered_message)
|
|
else:
|
|
print("Failed to recover message") |