Хотите зашифровать и расшифровать аудиофайл с помощью алгоритма PQC ⇐ Python

Программы на Python
Anonymous
Хотите зашифровать и расшифровать аудиофайл с помощью алгоритма PQC

Сообщение Anonymous »

Итак, я создал два файла Python. Они шифруют аудиофайл (вход, полученный с микрофона пользователя), используя Kyber для KEM, а затем AES для шифрования. Затем второй файл расшифровывает тот же файл.
При использовании чистого AES файл при расшифровке звучит нормально. Однако при использовании PQC файл по-прежнему содержит шум даже после расшифровки. Коды показаны ниже. Любая помощь будет принята с благодарностью! (файлы запускаются на libqos python в Linux)
  • Код шифрования

Код: Выделить всё

import pyaudio
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
import os
import wave
import oqs
# Step 1: Quantum-safe key exchange using Kyber512
kem_alg = "Kyber512"
kem = oqs.KeyEncapsulation(kem_alg)
# Generate post-quantum public and private key pair
public_key = kem.generate_keypair()
print(f"Public key (hex): {public_key.hex()}")
# Encapsulate a shared secret using the public key
ciphertext, shared_secret_enc = kem.encap_secret(public_key)
print(f"Ciphertext (hex): {ciphertext.hex()}")
print(f"Shared secret (encryption - hex): {shared_secret_enc.hex()}")
# Use shared secret as AES key
key = shared_secret_enc[:32]  # Use the first 32 bytes as the AES key
# Step 2: AES encryption setup
iv = os.urandom(16)  # Generate a random IV
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
# Open the microphone stream
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
# Open the output WAV file for writing encrypted data
with wave.open("quantum_safe_encrypted_audio.wav", "wb") as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(44100)
# Write the IV and ciphertext (from the PQC key encapsulation) to the file (for decryption purposes)
f.writeframes(iv)
f.writeframes(ciphertext)
# Padding setup for AES block size (16 bytes)
padder = padding.PKCS7(algorithms.AES.block_size).padder()
try:
while True:
data = stream.read(1024)
# Encrypt the audio data
padded_data = padder.update(data)
ct = encryptor.update(padded_data)
f.writeframes(ct)
print("Writing encrypted audio data to file...")
except KeyboardInterrupt:
print("Received keyboard interrupt. Stopping encryption.")
# Finalize encryption with padding for the last block
padded_data = padder.finalize()
ct = encryptor.update(padded_data) + encryptor.finalize()
f.writeframes(ct)
# Close the microphone stream
stream.stop_stream()
stream.close()
p.terminate()
print("Quantum-safe encrypted audio data written to quantum_safe_encrypted_audio.wav")

  • Расшифровка

Код: Выделить всё

import pyaudio
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import os
import wave
import hashlib
# Fixed key for AES encryption
original_key = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x30\x31'
key = hashlib.sha256(original_key).digest()
print(key.hex())
#key = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11\x12\x13\x14\x15'
# Open the encrypted audio file
with wave.open("quantum_safe_encrypted_audio.wav", "rb") as f:
# Read the encrypted audio data
encrypted_data = f.readframes(f.getnframes())
# Create an AES cipher object
cipher = Cipher(algorithms.AES(key), modes.CBC(b'\x00' * 16), backend=default_backend())
# Decrypt the encrypted audio data
decryptor = cipher.decryptor()
pt = decryptor.update(encrypted_data) + decryptor.finalize()
# Write the decrypted audio data to a file
with wave.open("decrypted_audio.wav", "wb") as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(44100)
f.writeframes(pt)
print("Decrypted audio data written to quantum_safe_encrypted_audio.wav")
Я думал, что расшифрованный аудиофайл будет звучать как входной оригинал. Однако это издает шум.

Подробнее здесь: https://stackoverflow.com/questions/790 ... g-pqc-algo

Вернуться в «Python»