【问题标题】:Audio Steganography using lsb causing noise in audio with hidden message使用 lsb 的音频隐写术在带有隐藏消息的音频中引起噪音
【发布时间】:2022-11-27 10:08:41
【问题描述】:

我正在尝试解决音频隐写术代码中的问题。 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 中的噪音是主要问题

【问题讨论】:

  • audio.getparams() 对您的文件的价值是多少?
  • @DanGetz _wave_params(nchannels=1, sampwidth=2, framerate=48000, nframes=194560, comptype='NONE', compname='not compressed')

标签: python lsb


【解决方案1】:

您编写的代码是为了处理sampwidth=1 的 wave 文件,但根据您的评论,您的文件的 sampwidth2。这意味着您的 frame_bytes 数组不是样本数组,而是样本数组字节其中每两个字节一起构成一个样本。 (因为 nchannels1,所以每帧有一个样本,单声道音频。)所以当你改变所有的 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

(我没有测试这么多)

【讨论】:

    猜你喜欢
    • 2011-05-03
    • 2011-07-12
    • 2021-10-31
    • 1970-01-01
    • 2011-11-18
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多