Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.
Python
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)

# Quantum-safe key exchange using Kyber512
kem = oqs.KeyEncapsulation("Kyber512")

# Get key pair
keypair = kem.generate_keypair()
print(f"Key pair output: {keypair}")  # Print the output to inspect it

# Check the type of keypair
if isinstance(keypair, bytes):
    public_key = keypair  # Treat the output as a public key
    print("Received a single byte string as public key.")
    # You might need to modify this section depending on how you handle the private key
else:
    print("Unexpected keypair structure. Please check the documentation.")
    exit(1)

# Generate the ciphertext and shared secret
ciphertext, shared_secret_enc = kem.encap_secret(public_key)

# Use shared secret as AES key
key = shared_secret_enc[:32]  # First 32 bytes

# AES encryption setup
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()

# Open output WAV file
with wave.open("quantum_safe_encrypted_audio.wav", "wb") as f:
    f.setnchannels(1)
    f.setsampwidth(2)
    f.setframerate(44100)
    f.writeframes(iv)  # Write IV to the beginning of the file

    try:
        print("Recording... Press Ctrl+C to stop.")
        while True:
            data = stream.read(1024)

            # Encrypt the audio data
            ct = encryptor.update(data)
            f.writeframes(ct)
            print("Writing encrypted audio data to file...")

    except KeyboardInterrupt:
        print("Stopping encryption.")

    # Finalize encryption
    ct = 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 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)

# Quantum-safe key exchange using Kyber512
kem = oqs.KeyEncapsulation("Kyber512")

# Get key pair
keypair = kem.generate_keypair()
print(f"Key pair output: {keypair}")  # Print the output to inspect it

# Check the type of keypair
if isinstance(keypair, bytes):
    public_key = keypair  # Treat the output as a public key
    print("Received a single byte string as public key.")
    # You might need to modify this section depending on how you handle the private key
else:
    print("Unexpected keypair structure. Please check the documentation.")
    exit(1)

# Generate the ciphertext and shared secret
ciphertext, shared_secret_enc = kem.encap_secret(public_key)

# Use shared secret as AES key
key = shared_secret_enc[:32]  # First 32 bytes

# AES encryption setup
iv = os.urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()

# Open output WAV file
with wave.open("quantum_safe_encrypted_audio.wav", "wb") as f:
    f.setnchannels(1)
    f.setsampwidth(2)
    f.setframerate(44100)
    f.writeframes(iv)  # Write IV to the beginning of the file

    try:
        print("Recording... Press Ctrl+C to stop.")
        while True:
            data = stream.read(1024)

            # Encrypt the audio data
            ct = encryptor.update(data)
            f.writeframes(ct)
            print("Writing encrypted audio data to file...")

    except KeyboardInterrupt:
        print("Stopping encryption.")

    # Finalize encryption
    ct = 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")

this is my encryption script

What I have tried:

Python
import pyaudio
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
import wave
import oqs

# Initialize audio settings
p = pyaudio.PyAudio()

# Open the encrypted WAV file
with wave.open("quantum_safe_encrypted_audio.wav", "rb") as f:
    # Read the number of channels, sample width, and framerate
    n_channels = f.getnchannels()
    samp_width = f.getsampwidth()
    framerate = f.getframerate()

    # Read the entire encrypted audio data
    encrypted_data = f.readframes(f.getnframes())

# Quantum-safe key exchange using Kyber512
kem = oqs.KeyEncapsulation("Kyber512")

# Generate a key pair for decryption (store only the private key)
private_key = kem.generate_keypair()  # This may just return the private key

# The encapsulated key must be obtained from the encryption process
encapsulated_key = b'...'  # Replace with actual encapsulated key used during encryption

# Decapsulate the shared secret (make sure to verify the correct method)
shared_secret_dec = kem.decapsulate(encapsulated_key, private_key)  # Confirm this method exists

# Use the shared secret as the AES key
key = shared_secret_dec[:32]  # Use first 32 bytes for AES-256

# IV must be saved/known; for example, if you saved it during encryption
iv = b'...'  # Replace with the actual IV if stored separately during encryption

# AES decryption setup
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()

# Decrypt the audio data
decrypted_data = decryptor.update(encrypted_data) + decryptor.finalize()

# Open the output WAV file
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")

# Play the decrypted audio
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
Posted
Updated 30-Sep-24 22:09pm
v2
Comments
[no name] 1-Oct-24 6:01am    
You should try reading and writing in straight byte format, rather than trying to extract audio channels. That way you should get exactly the same stream of bytes back after decryption.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900