def cover_make_mp3stego(wav_files_path, mp3_files_path, bitrate, start_idx=0, end_idx=10000): """ make mp3 cover samples via mp3stego encoder :param wav_files_path: path of wav audio files :param mp3_files_path:path of mp3 audio files :param bitrate: bitrate (128, 192, 256, 320) :param start_idx: the start index of audio files to be processed :param end_idx: the end index of audio files to be processed :return: NULL """ if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, start_idx=start_idx, end_idx=end_idx) if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) for wav_file_path in wav_files_list: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) if not os.path.exists(mp3_file_path): command = "encode_MP3Stego.exe -b " + bitrate + " " + wav_file_path + " " + mp3_file_path os.system(command) else: pass print("MP3Stego cover samples with bitrate %s are completed." % bitrate)
def stego_make_acs(wav_files_path, mp3_files_path, bitrate, width, height="7", embed=embedding_file_path, embedding_rate="10", start_idx=None, end_idx=None): """ make stego samples (ACS) :param wav_files_path: path of wav audio files :param mp3_files_path: path of mp3 audio files :param bitrate: bitrate :param width: width of parity-check matrix :param height: height of parity-check matrix, default is "7" :param embed: path of embedding file :param embedding_rate: embedding rate, default is "10" :param start_idx: start index of audio files :param end_idx: end index of audio files :return: NULL """ if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, start_idx=start_idx, end_idx=end_idx) if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) for wav_file_path in wav_files_list: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) if not os.path.exists(mp3_file_path): command = "C:/Users/Charles_CatKing/Desktop/ACS/lame.exe -b " + bitrate + " -embed " + embed + " -width " + width + " -height " + height + \ " -er " + embedding_rate + " -region 2 -layerii 1 -threshold 2 -key 123456 " + wav_file_path + " " + mp3_file_path os.system(command) else: pass
def calibration(mp3_files_path, calibration_files_path, bitrate, start_idx=0, end_idx=10000): """ mp3 calibration via lame encoder -> lame.exe -b 128 ***.mp3 c_***.mp3 :param mp3_files_path: the mp3 files path :param calibration_files_path: the calibrated mp3 files path :param bitrate: bitrate :param start_idx: start index :param end_idx: end index :return: """ if not os.path.exists(mp3_files_path): print("The mp3 files path does not exist.") else: mp3_files_list = get_files_list(file_dir=mp3_files_path, start_idx=start_idx, end_idx=end_idx) if not os.path.exists(calibration_files_path): os.mkdir(calibration_files_path) for mp3_file_path in mp3_files_list: mp3_file_name = get_file_name(mp3_file_path) calibrated_mp3_file_path = fullfile(calibration_files_path, mp3_file_name) if not os.path.exists(calibrated_mp3_file_path): command = "encode.exe -b " + bitrate + " " + mp3_file_path + " " + calibrated_mp3_file_path os.system(command) else: pass print("calibration with bitrate %s are completed." % bitrate)
def stego_make_hcm(wav_files_path, mp3_files_path, bitrate, cost="2", embed=embedding_file_path, frame_num="50", embedding_rate="10", start_idx=None, end_idx=None): """ make stego samples (HCM) :param wav_files_path: path of wav audio files :param mp3_files_path: path of mp3 audio files :param bitrate: bitrate :param cost: type of cost function, default is "2" :param embed: path of embedding file :param frame_num: frame number of embedding message, default is "50" :param embedding_rate: embedding rate, default is "10" :param start_idx: start index of audio files :param end_idx: end index of audio files :return: NULL """ if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, start_idx=start_idx, end_idx=end_idx) if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) for wav_file_path in wav_files_list: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) if not os.path.exists(mp3_file_path): command = "encode_HCM.exe -b " + bitrate + " -embed " + embed + " -cost " + cost + " -er " + embedding_rate \ + " -framenumber " + frame_num + " " + wav_file_path + " " + mp3_file_path os.system(command) else: pass
def stego_make_eecs(wav_files_path, mp3_files_path, bitrate, width, height="7", embed=embedding_file_path, frame_num="50", embedding_rate="10", start_idx=None, end_idx=None): """ make stego samples (EECS) :param wav_files_path: path of wav audio files :param mp3_files_path: path of mp3 audio files :param bitrate: bitrate :param width: width of parity-check matrix :param height: height of parity-check matrix, default is "7" :param embed: path of embedding file :param frame_num: frame number of embedding message, default is "50" :param embedding_rate: embedding rate, default is "10" :param start_idx: start index of audio files :param end_idx: end index of audio files :return: NULL """ if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, start_idx=start_idx, end_idx=end_idx) if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) for wav_file_path in wav_files_list: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) if not os.path.exists(mp3_file_path): command = "encode_EECS.exe -b " + bitrate + " -embed " + embed + " -width " + width + " -height " + height + " -er " + embedding_rate \ + " -framenumber " + frame_num + " " + wav_file_path + " " + mp3_file_path os.system(command) else: pass
def steganalysis_one(args): height, width = args.height, args.width model_file_path = args.model_file_path image_path = args.test_file_path data = tf.placeholder(tf.float32, [1, height, width, 1], name="image") command = args.network + "(data, 2, is_bn=False)" logits = eval(command) logits = tf.nn.softmax(logits) # read image img = io.imread(image_path) img = np.reshape(img, [1, height, width, 1]) image_name = get_file_name(image_path) # 加载模型 saver = tf.train.Saver() init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) saver.restore(sess, model_file_path) print("The model is loaded successfully.") # predict ret = sess.run(logits, feed_dict={data: img}) ret[0][0] = ret[0][0] + 0.071823 ret[0][1] = ret[0][1] - 0.071823 result = np.argmax(ret, 1) if result == 1: print("%s: stego" % image_name) if result == 0: print("%s: cover" % image_name)
def stego_make_mp3stego(wav_files_path, mp3_files_path, bitrate, start_idx=0, end_idx=10000): if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, start_idx=start_idx, end_idx=end_idx) embedding_rates = ["01", "03", "05", "08", "10"] if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) for wav_file_path in wav_files_list: for embedding_rate in embedding_rates: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) command = "encode_HCM.exe -b " + bitrate + " -E " + embedding_file_path + "--ER" + embedding_rate + " " + wav_file_path + " " + mp3_file_path eval(command) print( "stego samples are made completely, bitrate %s, stego algorithm %s." % (bitrate, "HCM"))
def stego_make_mp3stego(wav_files_path, mp3_files_path, bitrate, embedding_rate="10", start_idx=None, end_idx=None): """ make stego samples via MP3Stego for 10s wav audio, secret messages of 1528 bits (191 Bytes) will be embedded, and the length of secret messages is independent of bitrate analysis unit: 50 frames (for 10s mp3 audio, there are 384 frames), 24.83 bytes messages will be embedded relative embedding rate secret messages length is_selected 10% 3 Bytes 1 20% 5 Bytes 30% 8 Bytes 1 40% 10 Bytes 50% 13 Bytes 1 60% 14 Bytes 70% 17 Bytes 80% 20 Bytes 1 90% 22 Bytes 100% 24 Bytes 1 in the process of MP3stego, the messages are compressed :param wav_files_path: path of wav audio files :param mp3_files_path:path of mp3 audio files :param bitrate: bitrate (128, 192, 256, 320) :param embedding_rate: embedding rate, default is "10" :param start_idx: the start index of audio files to be processed :param end_idx: the end index of audio files to be processed :return: NULL """ embedding_rates = ["1", "3", "5", "8", "10"] message_lengths = [3, 8, 13, 20, 24] if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, file_type="wav", start_idx=start_idx, end_idx=end_idx) if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) message_len = message_lengths[embedding_rates.index(embedding_rate)] embedding_file = message_random(embedding_file_path, message_len) for wav_file_path in wav_files_list: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) if not os.path.exists(mp3_file_path): command = "encode_MP3Stego.exe -b " + bitrate + " -E " + embedding_file + " -P pass " + wav_file_path + " " + mp3_file_path os.system(command) else: pass print( "stego samples are made completely, bitrate %s, stego algorithm %s." % (bitrate, "MP3Stego"))
def steganalysis_batch(args): # hyper parameters height, width, channel = args.height, args.width, args.channel # height and width of input matrix carrier = args.carrier # carrier (qmdct | audio | image) # path steganalysis_files_path = args.steganalysis_files_path # placeholder data = tf.placeholder(dtype=tf.float32, shape=(None, height, width, channel), name="data") is_bn = tf.placeholder(dtype=tf.bool, name="is_bn") # initialize the network if args.network not in networks: print("Network miss-match, please try again") return False # network command = args.network + "(data, args.class_num, is_bn)" logits = eval(command) logits = tf.nn.softmax(logits) model = tf.train.Saver() with tf.Session() as sess: # load model sess.run(tf.global_variables_initializer()) model_file_path = get_model_file_path(args.models_path) if model_file_path is None: print("No model is loaded successfully.") else: model.restore(sess, model_file_path) print("The model is loaded successfully, model file: %s" % model_file_path) file_list = get_files_list(steganalysis_files_path) print("files path: %s" % steganalysis_files_path) count_cover, count_stego = 0, 0 for file_path in file_list: steganalysis_data = get_data_batch([file_path], width=width, height=height, channel=channel, carrier=carrier) file_name = get_file_name(file_path) ret = sess.run(logits, feed_dict={data: steganalysis_data, is_bn: False}) result = np.argmax(ret, 1) prob = 100 * ret[0][result] if result[0] == 0: print("file name: %s, result: cover, prob of prediction: %.2f%%" % (file_name, prob)) count_cover += 1 if result[0] == 1: print("file name: %s, result: stego, prob of prediction: %.2f%%" % (file_name, prob)) count_stego += 1 print("Number of cover samples: %d" % count_cover) print("Number of stego samples: %d" % count_stego)
def steganalysis_batch(args): height, width = args.height, args.width model_file_path = args.model_file_path image_files_path = args.test_files_dir label_file_path = args.label_file_path image_list = get_files_list(image_files_path) image_num = len(image_list) data = tf.placeholder(tf.float32, [1, height, width, 1], name="image") if label_file_path is not None: label = list() with open(label_file_path) as file: for line in file.readlines(): label.append(line) else: label = np.zeros([image_num, 1]) command = args.network + "(data, 2, is_bn=False)" logits = eval(command) logits = tf.nn.softmax(logits) # 加载模型 saver = tf.train.Saver() init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) saver.restore(sess, model_file_path) print("The model is loaded successfully.") # read image count, i = 0, 0 for image_path in image_list: img = io.imread(image_path) img = np.reshape(img, [1, height, width, 1]) image_name = get_file_name(image_path) # predict ret = sess.run(logits, feed_dict={data: img}) ret[0][0] = ret[0][0] + 0.071823 ret[0][1] = ret[0][1] - 0.071823 result = np.argmax(ret, 1) if result == 1: print("%s: stego" % image_name) if int(label[i]) == 1: count = count + 1 if result == 0: print("%s: cover" % image_name) if int(label[i]) == 0: count = count + 1 i = i + 1 if label_file_path is not None: print("Accuracy = %.2f" % (count / image_num))
def stego_make_acs(wav_files_path, mp3_files_path, bitrate, width, height="7", embed=embedding_file_path, embedding_rate="10", frame_embedding_rate="10", region="2", threshold="2", start_idx=None, end_idx=None): """ make stego samples (ACS) :param wav_files_path: path of wav audio files :param mp3_files_path: path of mp3 audio files :param bitrate: bitrate :param width: width of parity-check matrix :param height: height of parity-check matrix, default is "7" :param embed: path of embedding file :param embedding_rate: embedding rate, default is "10" :param frame_embedding_rate: embedding rate in a frame, defualt is "10" :param region: embeding region, default is "2", "0": Big-Value region, "1": Count1 region, "2": All regions :param threshold: threshold value for embedding, embedded coefficients are in [-threshold, threshold], default is "2" :param start_idx: start index of audio files :param end_idx: end index of audio files :return: NULL """ if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, file_type="wav", start_idx=start_idx, end_idx=end_idx) if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) for wav_file_path in wav_files_list: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) if not os.path.exists(mp3_file_path): temp_secret_file_path = message_random(embed) key = random.randint(1000000, 9999999) command = "encode_ACS.exe -b " + bitrate + " -embed " + temp_secret_file_path + " -width " + width + " -height " + height + \ " -er " + embedding_rate + " -fer " + frame_embedding_rate + " -region " + region + " -threshold " + threshold + " -key " + key + \ wav_file_path + " " + mp3_file_path os.system(command) else: pass
def steganalysis_one(args): # hyper parameters height, width, channel = args.height, args.width, args.channel # height and width of input matrix carrier = args.carrier # carrier (qmdct | audio | image) # path steganalysis_file_path = args.steganalysis_file_path # placeholder tf.compat.v1.disable_eager_execution() data = tf.compat.v1.placeholder(dtype=tf.float32, shape=(1, height, width, channel), name="data") is_bn = tf.compat.v1.placeholder(dtype=tf.bool, name="is_bn") # initialize the network if args.network not in networks: print("Network miss-match, please try again") return False # network command = args.network + "(data, args.class_num, is_bn)" logits = eval(command) logits = tf.nn.softmax(logits) model = tf.compat.v1.train.Saver() with tf.compat.v1.Session() as sess: # load model sess.run(tf.compat.v1.global_variables_initializer()) model_file_path = get_model_file_path(args.model_path) print(model_file_path) if model_file_path is None: print("No model is loaded successfully.") else: model.restore(sess, model_file_path) print("The model is loaded successfully, model file: %s" % model_file_path) steganalysis_data = get_data_batch([steganalysis_file_path], width=width, height=height, channel=channel, carrier=carrier) if steganalysis_data is None: print("No model can be used for this carrier. (need image or audio)") else: file_name = get_file_name(steganalysis_file_path) ret = sess.run(logits, feed_dict={data: steganalysis_data, is_bn: False}) result = np.argmax(ret, 1) prob = 100 * ret[0][result] if result[0] == 0: print("file name: %s, result: cover, prob of prediction: %.2f%%" % (file_name, prob)) if result[0] == 1: print("file name: %s, result: stego, prob of prediction: %.2f%%" % (file_name, prob))
def stego_make_mp3stego(wav_files_path, mp3_files_path, bitrate, embedding_rate="10", start_idx=0, end_idx=10000): """ make stego samples via MP3Stego for 10s wav audio, secret messages of 1528 bits (191 Bytes) will be embedded, and the length of secret messages is independent of bitrate analysis unit: 50 frames (for 10s mp3 audio, there are 384 frames), 24.83 bytes messages will be embedded relative embedding rate secret messages length 10% 3 Bytes 30% 8 Bytes 50% 13 Bytes 80% 20 Bytes 100% 24 Bytes in the process of MP3stego, the messages are compressed :param wav_files_path: path of wav audio files :param mp3_files_path:path of mp3 audio files :param bitrate: bitrate (128, 192, 256, 320) :param embedding_rate: embedding rate, default is "10" :param start_idx: the start index of audio files to be processed :param end_idx: the end index of audio files to be processed :return: NULL """ if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, start_idx=start_idx, end_idx=end_idx) if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) embedding_file_name = "stego_0" + embedding_rate + ".txt" if len( embedding_rate) == 1 else "stego_" + embedding_rate + ".txt" embedding_file = fullfile(embedding_files_mp3stego_path, embedding_file_name) for wav_file_path in wav_files_list: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) if not os.path.exists(mp3_file_path): command = "encode_MP3Stego.exe -b " + bitrate + " -E " + embedding_file + " -P pass " + wav_file_path + " " + mp3_file_path os.system(command) else: pass print( "stego samples are made completely, bitrate %s, stego algorithm %s." % (bitrate, "MP3Stego"))
def stego_make_ahcm(wav_files_path, mp3_files_path, bitrate, width, height="7", embed=embedding_file_path, embedding_rate="10", start_idx=None, end_idx=None): """ make stego samples (AHCM) :param wav_files_path: path of wav audio files :param mp3_files_path: path of mp3 audio files :param bitrate: bitrate :param width: width of parity-check matrix :param height: height of parity-check matrix, default is "7" :param embed: path of embedding file :param embedding_rate: embedding rate, default is "10" :param start_idx: start index of audio files :param end_idx: end index of audio files :return: NULL """ if not os.path.exists(wav_files_path): print("The wav files path does not exist.") else: wav_files_list = get_files_list(file_dir=wav_files_path, file_type="wav", start_idx=start_idx, end_idx=end_idx) if not os.path.exists(mp3_files_path): os.mkdir(mp3_files_path) for wav_file_path in wav_files_list: file_name = get_file_name(wav_file_path) mp3_file_name = file_name.replace(".wav", ".mp3") mp3_file_path = fullfile(mp3_files_path, mp3_file_name) if not os.path.exists(mp3_file_path): temp_secret_file_path = message_random(embed) key = random.randint(1000000, 9999999) command = "encode_AHCM.exe -b " + bitrate + " -embed " + temp_secret_file_path + " -width " + width + " -height " + height + \ " -er " + embedding_rate + " -key " + key + \ wav_file_path + " " + mp3_file_path os.system(command) else: pass
def steganalysis_batch(args): # the info of carrier carrier = args.carrier test_files_path = args.test_files_path label_file_path = args.label_file_path # pre-process is_diff, order, direction = args.is_diff, args.order, args.direction # the height, width and channel of the QMDCT matrix height, width, channel = args.height, args.width, args.channel if is_diff is True and direction == 0: height_new, width_new = height - order, width elif is_diff is True and direction == 1: height_new, width_new = height, width - order else: height_new, width_new = height, width # placeholder data = tf.placeholder(tf.float32, [None, height_new, width_new, channel], name="QMDCTs") is_bn = tf.placeholder(dtype=tf.bool, name="is_bn") # network command = args.network + "(data, 2, is_bn)" logits = eval(command) logits = tf.nn.softmax(logits) model = tf.train.Saver() with tf.Session() as sess: # load model sess.run(tf.global_variables_initializer()) if args.model_file_path is None and args.model_files_path is not None: model_file_path = tf.train.latest_checkpoint(args.model_files_path) elif args.model_file_path is not None: model_file_path = args.model_file_path else: model_file_path = None if model_file_path is None: print("No model is loaded successfully.") else: model.restore(sess, model_file_path) print("The model is loaded successfully, model file: %s" % model_file_path) test_file_list = get_files_list(test_files_path) # get file label if label_file_path is not None: labels = list() with open(label_file_path) as file: for line in file.readlines(): labels.append(int(line)) else: labels = -np.ones([image_num, 1]) results, number = list(), 0 for test_file in test_file_list: # predict if carrier == "audio": media = read_text(test_file, width=width, is_diff=args.is_diff, order=args.order, direction=args.direction) elif carrier == "image": media = io.imread(test_file) else: media = None if media is None: print( "No model can be used for this carrier. (need image or audio)" ) else: media_name = get_file_name(test_file, sep="\\") media = np.reshape(media, [1, height_new, width_new, channel]) ret = sess.run(logits, feed_dict={ data: media, is_bn: False }) result = np.argmax(ret, 1) prob = 100 * ret[0][result] results.append(result[0]) media_label = labels[number] if result[0] == 0: print( "file name: %s, result: cover, label: %r, prob of prediction: %.2f%%" % (media_name, media_label, prob)) if result[0] == 1: print( "file name: %s, result: stego, label: %r, prob of prediction: %.2f%%" % (media_name, media_label, prob)) number += 1 accuracy = 100 * (np.count_nonzero(np.array(results) == labels) / len(results)) print("Accuracy = %.2f%%" % accuracy)