[英]Audio Steganography using lsb causing noise in audio with hidden message
我正在嘗試解決我的音頻隱寫術代碼中的問題。 Afted 將消息隱藏在 wav 音頻文件中,考慮到整個音頻隱寫術的意義,當然不應該有一些噪音。 非常感謝您的幫助!
這是代碼
import wave
import os
global chosen_audio
def hide():
hide_menu()
global chosen_audio
message = input('Input message to be hidden: \n')
print('hiding message ... \n')
audio = wave.open(chosen_audio, 'rb')
frame_bytes = bytearray(list(audio.readframes(audio.getnframes())))
message = message + int((len(frame_bytes) - (len(message) * 8 * 8)) / 8) * '#'
bits = list(
map(int, ''.join([bin(ord(i)).lstrip('0b').rjust(8, '0') for i in message])))
for i, bit in enumerate(bits):
frame_bytes[i] = (frame_bytes[i] & 254) | bit # replace lsb
frames_modified = bytes(frame_bytes)
with wave.open('C:/Users/*****/PycharmProjects/steganography/modified_audio.wav', 'wb') as modified_audio:
modified_audio.setparams(audio.getparams())
modified_audio.writeframes(frames_modified)
print('message hidden ... \n')
modified_audio.close()
audio.close()
def reveal():
modified_audio = wave.open('C:/Users/*****/PycharmProjects/steganography/modified_audio.wav', 'rb')
frame_bytes = bytearray(list(modified_audio.readframes(modified_audio.getnframes())))
ls_bits = [frame_bytes[i] & 1 for i in range(len(frame_bytes))]
text = "".join(chr(int("".join(map(str, ls_bits[i:i + 8])), 2)) for i in range(0, len(ls_bits), 8))
message = text.split("###")[0]
modified_audio.close()
return message
def mode():
method = input(
' \n\t\t\tPLEASE, CHOOSE THE PROCEDURE! \n\n \tPRESS H FOR HIDING THE MESSAGE \t PRESS R FOR REVEALING THE MESSAGE FROM THE AUDIO\n')
if method == 'H' or method == 'h':
hide()
elif method == 'r' or method == 'R':
reveal()
else:
print('I don\'t think we have such a option')
mode()
def hide_menu():
global chosen_audio
chosen_option = ''
print(chosen_option)
chosen_option = ''
chosen_audio = ''
print(' \nCHOOSE YOUR AUDIO FILE! ')
chosen_option = (
input('\t press 1 & ENTER for your own audio path\n''\t press 2 & ENTER for default audio file\n'))
if chosen_option == '1':
file_path = input('Enter a file path: ')
if os.path.exists(file_path):
print('The path is okay, file exists!')
chosen_audio = file_path
else:
print('The specified file in this path does NOT exist')
hide_menu()
elif chosen_option == '2':
chosen_audio_option = input(
'\t press V & enter to use voice audio \t press S & enter to use sound audio\t press M & enter to use '
'song audio\n')
if chosen_audio_option == 'M' or chosen_audio_option == 'm':
chosen_audio = 'C:\\Users\\*****\\PycharmProjects\\steganography\\song_audio.wav'
elif chosen_audio_option == 'v' or chosen_audio_option == 'V':
chosen_audio = 'C:\\Users\\*****\\PycharmProjects\\steganography\\voice_audio.wav'
elif chosen_audio_option == 's' or chosen_audio_option == 'S':
chosen_audio = 'C:\\Users\\*****\\Desktop\\audio\\hracka_pes.wav'
else:
print('No such a option !')
hide_menu()
else:
print('I don\'t think we have such a option')
hide_menu()
def reveal_menu():
global chosen_audio
chosen_audio = ''
print(' \nCHOOSE YOUR AUDIO FILE! ')
chosen_option = int(
input('\t press 1 & ENTER for your own audio path\n''\t press 2 & ENTER for default audio file\n'))
if chosen_option == 1:
file_path = input('Enter a file path: ')
if os.path.exists(file_path):
print('The path is okay, file exists!')
chosen_audio = file_path
else:
print('The specified file in this path does NOT exist')
hide_menu()
elif chosen_option == 2:
pass
mode()
# menu()
# hide()
注意 - 不能使用庫進行隱寫術
聽到 modified_audio 中的噪音是主要問題
您編寫的代碼是為了處理sampwidth=1
的 wave 文件,但根據您的評論,您的文件的sampwidth
是2
。 這意味着您的frame_bytes
數組不是一個樣本數組,它是一個字節數組,其中每兩個字節一起構成一個樣本。 (因為nchannels
是1
,每幀有一個樣本,mono 音頻。)所以當你改變所有字節的 LSB 時,你不僅改變了每個樣本的 LSB,而且改變了它中間的一點。
您應該將其轉換為樣本數組,然后更改樣本的 LSB。 您可以為此使用array
:
import array
samples = array.array('h', frame_bytes) # signed 16-bit
# ... modify the samples ...
frames_modified = samples.tobytes()
當然,如果您處理的是有符號短褲 ( sampsize==2
) 或無符號字節 ( sampsize==1
),最好事先檢查您的代碼,然后相應地處理它們。 你可能會這樣做:
samples = array.array('h' if sampsize == 2 else 'B', frame_bytes)
for i, bit in enumerate(bits):
samples[i] = samples[i] & -2 | bit
(我沒有測試這么多)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.