i have made an audio encryption script in liboqs-python and decryption script on executing the encryption is fine but on decryption i am only getting noise even though output say decryption is successful.
import pyaudio
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import os
import wave
import oqs
p = pyaudio.PyAudio()
try:
stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
except Exception as e:
print(f"Error opening stream: {e}")
exit(1)
kem = oqs.KeyEncapsulation("Kyber512")
keypair = kem.generate_keypair()
print(f"Key pair output: {keypair}")
if isinstance(keypair, bytes):
public_key = keypair
print("Received a single byte string as public key.")
else:
print("Unexpected keypair structure. Please check the documentation.")
exit(1)
ciphertext, shared_secret_enc = kem.encap_secret(public_key)
key = shared_secret_enc[:32]
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
with wave.open("quantum_safe_encrypted_audio.wav", "wb") as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(44100)
f.writeframes(iv)
try:
print("Recording... Press Ctrl+C to stop.")
while True:
data = stream.read(1024)
ct = encryptor.update(data)
f.writeframes(ct)
print("Writing encrypted audio data to file...")
except KeyboardInterrupt:
print("Stopping encryption.")
ct = encryptor.finalize()
f.writeframes(ct)
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 oqs
p = pyaudio.PyAudio()
try:
stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, frames_per_buffer=1024)
except Exception as e:
print(f"Error opening stream: {e}")
exit(1)
kem = oqs.KeyEncapsulation("Kyber512")
keypair = kem.generate_keypair()
print(f"Key pair output: {keypair}")
if isinstance(keypair, bytes):
public_key = keypair
print("Received a single byte string as public key.")
else:
print("Unexpected keypair structure. Please check the documentation.")
exit(1)
ciphertext, shared_secret_enc = kem.encap_secret(public_key)
key = shared_secret_enc[:32]
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
with wave.open("quantum_safe_encrypted_audio.wav", "wb") as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(44100)
f.writeframes(iv)
try:
print("Recording... Press Ctrl+C to stop.")
while True:
data = stream.read(1024)
ct = encryptor.update(data)
f.writeframes(ct)
print("Writing encrypted audio data to file...")
except KeyboardInterrupt:
print("Stopping encryption.")
ct = encryptor.finalize()
f.writeframes(ct)
stream.stop_stream()
stream.close()
p.terminate()
print("Quantum-safe encrypted audio data written to quantum_safe_encrypted_audio.wav")
this is my encryption script
What I have tried:
import pyaudio
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import wave
import oqs
p = pyaudio.PyAudio()
with wave.open("quantum_safe_encrypted_audio.wav", "rb") as f:
n_channels = f.getnchannels()
samp_width = f.getsampwidth()
framerate = f.getframerate()
encrypted_data = f.readframes(f.getnframes())
kem = oqs.KeyEncapsulation("Kyber512")
private_key = kem.generate_keypair()
encapsulated_key = b'...'
shared_secret_dec = kem.decapsulate(encapsulated_key, private_key)
key = shared_secret_dec[:32]
iv = b'...'
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
decrypted_data = decryptor.update(encrypted_data) + decryptor.finalize()
with wave.open("quantum_safe_decrypted_audio.wav", "wb") as f:
f.setnchannels(n_channels)
f.setsampwidth(samp_width)
f.setframerate(framerate)
f.writeframes(decrypted_data)
print("Quantum-safe decrypted audio data written to quantum_safe_decrypted_audio.wav")
stream = p.open(format=pyaudio.paInt16, channels=n_channels, rate=framerate, output=True)
stream.write(decrypted_data)
stream.stop_stream()
stream.close()
p.terminate()
this the decryption script