1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
import wave import numpy as np import struct import math import argparse import os
letter_code = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."] number_code = ["-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."]
def wav_encrypt(input_file, plaintext, outfile): if not os.path.exists(input_file): print("Wav is not exist") print("Encrypt fail") return if input_file[-4:] != ".wav": print("input file is not wav") return if outfile[-4:] != ".wav": print("output file is not wav") return f = wave.open(input_file, 'r') print("Read wav successful")
params = f.getparams() nchannels, sampwidth, framerate, nframes, comptype, compname = params[:6] if int(nchannels) == 1: print("the wav file need two sound channel at least") return
strData = f.readframes(nframes)
waveData = np.fromstring(strData, dtype=np.int16)
waveData = waveData * 1.0 / (max(abs(waveData))) waveData = np.reshape(waveData, [nframes, nchannels])
f.close()
size = waveData[:, 0].size k = int(size * 0.1) di_length = 1000 da_length = 3000 space_length = 10000 gap = 1000 morse_code = morse_code_encrypt(plaintext) print("Morse_code:",morse_code) max_cipher_text_length = k for i in morse_code: if i == '.': max_cipher_text_length += gap + di_length elif i == '-': max_cipher_text_length += gap + da_length else: max_cipher_text_length += 10000 if max_cipher_text_length > int(size * 0.99): print("the wav file is small or the plaintext is big,Cannot hide data") return for i in morse_code: if i == '.': for j in range(k, k + di_length): waveData[:, 1][j] = 0.1 * math.sin((j - k) / math.pi) k += di_length + gap elif i == '-': for j in range(k, k + da_length): waveData[:, 1][j] = 0.1 * math.sin((j - k) / math.pi) k += da_length + gap else: k = k + space_length
waveData = np.reshape(waveData, [nframes * nchannels, 1])
f = wave.open(outfile, 'wb')
f.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
print("Writing file, please wait.") for v in waveData: f.writeframes(struct.pack('h', int(v * 64000 / 2)))
f.close() print("Wav encrypt Successful")
def morse_code_encrypt(plaintext): morse_code = "" for i in plaintext: if 97 <= ord(i) <= 122: morse_code += letter_code[ord(i) - 97]+' ' elif 65 <= ord(i) <= 90: morse_code += letter_code[ord(i) - 65] + ' ' elif 48 <= ord(i) <= 57: morse_code += number_code[ord(i) - 48] + ' ' return morse_code
def main(): parser = argparse.ArgumentParser(description="usage:python wav_encrypt.py -i inputfile.wav -p plaintext -o outfile.wav") parser.add_argument("-i", help="Wav file name", type=str) parser.add_argument("-p", help="plaintext", type=str) parser.add_argument("-o", help="output file name", type=str) args = parser.parse_args() wav_encrypt(args.i, args.p, args.o)
if __name__ == "__main__": main()
|