Skip to content

Instantly share code, notes, and snippets.

@reachsumit
Last active June 14, 2023 06:33
Show Gist options
  • Save reachsumit/5376441d341bb5c8b361a2f3e0798993 to your computer and use it in GitHub Desktop.
Save reachsumit/5376441d341bb5c8b361a2f3e0798993 to your computer and use it in GitHub Desktop.
This code contains a demo for Audio Steganography. It is to be used by sender end to embed text mentioned in string variable to the audio file.
# We will use wave package available in native Python installation to read and write .wav audio file
import wave
# read wave audio file
song = wave.open("song.wav", mode='rb')
# Read frames and convert to byte array
frame_bytes = bytearray(list(song.readframes(song.getnframes())))
# The "secret" text message
string='Peter Parker is the Spiderman!'
# Append dummy data to fill out rest of the bytes. Receiver shall detect and remove these characters.
string = string + int((len(frame_bytes)-(len(string)*8*8))/8) *'#'
# Convert text to bit array
bits = list(map(int, ''.join([bin(ord(i)).lstrip('0b').rjust(8,'0') for i in string])))
# Replace LSB of each byte of the audio data by one bit from the text bit array
for i, bit in enumerate(bits):
frame_bytes[i] = (frame_bytes[i] & 254) | bit
# Get the modified bytes
frame_modified = bytes(frame_bytes)
# Write bytes to a new wave audio file
with wave.open('song_embedded.wav', 'wb') as fd:
fd.setparams(song.getparams())
fd.writeframes(frame_modified)
song.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment