加密后的Wav文件听起来和加密前的类似

2024-10-03 17:15:15 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图做一个音频加密程序,但我的问题是,我加密了wav文件后,我仍然可以听到原始声音的噪音。你知道吗

以下是一些原始波形文件值: https://imgur.com/OgT8bzk

这些是加密后的值: https://imgur.com/WCV7kGy

起初,我试图用这些加密值创建一个wav文件,但其中一些需要超过32位,所以我将它们标准化为-1和1之间的值。你知道吗

标准化值: https://imgur.com/wuLJkUr

现在的问题是,在我用这些标准化的值创建了一个wav之后,我仍然可以听到原始的声音,我不知道为什么。你知道吗

以下是原始声音和加密声音,如果您想听到它们: https://ufile.io/c684hbdu


class Spectrum:

    def __init__(self,ampArray,freqArray,framerate):

        self.ampArray=ampArray
        self.freqArray=freqArray
        self.framerate=framerate

    def make_wave(self,size):

        data=np.fft.irfft(self.ampArray,size)
        return Wave(data,framerate=self.framerate)

    # encryption 
    # basically i change the values generated by FFT (the values are in ampArray).
    #I write the complex and real part as a Fraction and the new values (the encrypted ones) will be the lenght of the numerator + the numerator + the denominator
    def make_spectrum_crypted(self):


      ampArrayCopy=copy.deepcopy(self.ampArray)

      for i in range(len(self.ampArray)):

            fractionR=Fraction(self.ampArray[i].real).limit_denominator()
            numeratorR=fractionR.numerator;
            denominatorR=fractionR.denominator;


            fractionI=Fraction(self.ampArray[i].imag).limit_denominator()


            numeratorI=fractionI.numerator;
            denominatorI=fractionI.denominator;


            realPartString=str(len(str(abs((numeratorR)))))+str(abs(numeratorR))+str(abs(denominatorR))
            imagPartString=str(len(str(abs((numeratorI)))))+str(abs(numeratorI))+str(abs(denominatorI))

            wr=int(realPartString)
            wi=int(imagPartString)

            if(numeratorR<0):
                wr=-wr;
            if(numeratorI<0):
                wi=-wi;
            ampArrayCopy[i]=complex(wr,wi);

        return Spectrum(ampArrayCopy,self.freqArray,self.framerate)


# read data from wav
data=read_wave('count.wav')

# encryption and creating the encrypted wav
spectrum=data.make_spectrum()
spectrumC=spectrum.make_spectrum_crypted()
wave=spectrumC.make_wave(len(data))

#normalize encrypted wav values between -1 and 1
old_max = np.max(wave.data)
old_min = np.min(wave.data)

new_min = -1
new_max = 1
normalizeList=[]

for i in range(len(wave.data.tolist())):
    normalizeList.append(( (wave.data[i] - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min)

# create wave
dataToWrite = np.array(normalizeList, dtype=np.float32)
scipy.io.wavfile.write('encrypted.wav',wave.framerate,dataToWrite )

Tags: theselfnewdatamakenpabsmin