python手记(51)
python通过声音将文件内容隐藏,实现原理是将文件的内容分别插入到声音文件的不同位置中做为当次采样的数据,目前是对英文文本文档加解密
#!/usr/bin/env python # -*- coding: utf-8 -*- #http://blog.csdn.net/myhaspl #code:myhaspl@qq.com #将文件隐藏在声音之中 import wave import pylab as pl import numpy as np print 'http://blog.csdn.net/myhaspl' print 'myhaspl@qq.com' print print 'working...' #编码 print u'正在将文件编码进声音' print "generate wav data...." # 打开文档 fo = wave.open(r"pltest.wav", "wb") file_object = open('test.txt') try: all_the_text = file_object.read( ) finally: file_object.close( ) wdata=map(ord,all_the_text) wdata=np.array(wdata) lwdata=len(wdata) # 设置波形参数 #采样率 framerate = 44100 #声道数 nchannels=2 #每位宽度 sampwidth=2 #长度 nframes =framerate*4 #振幅 base_amplitude = 200 max_amplitude=128*base_amplitude #每个字符的间隔次数 interval=(nframes-10)/lwdata #每周期样本数 wave_data=np.zeros((nframes), dtype=np.short) count=0 myrand=np.random.rand(nframes) for curpos in xrange(0,nframes): if curpos % interval==0 and count<lwdata: possamp=wdata[count]*base_amplitude-64*base_amplitude count+=1 elif curpos%60==0: possamp=int(myrand[curpos]*max_amplitude-max_amplitude/2) else: possamp=0 wave_data[curpos]=possamp #写波形数据参数 print "save new wav files...." str_data=wave_data.tostring() fo.setnchannels(nchannels) fo.setframerate(framerate) fo.setsampwidth(sampwidth) fo.setnframes(nframes) fo.writeframes(str_data) fo.close() # 绘制波形 wave_data.shape = -1, 2 wave_data = wave_data.T time = np.arange(0, nframes/2) pl.subplot(211) pl.plot(time, wave_data[0], c="r") pl.subplot(212) pl.plot(time, wave_data[1], c="g") pl.xlabel("time (seconds)") #解码 new_wdata=[] print u'正在从声音解码文件' fi = wave.open(r"pltest.wav", "rb") fi_params=fi.getparams() fi_nframes = fi_params[3] fi_str_data=fi.readframes(fi_nframes) fi_wave_data= np.fromstring(fi_str_data, dtype=np.short) count=0 for curpos in xrange(0,nframes): if curpos % interval==0 and count<lwdata: possamp=(fi_wave_data[curpos]+64*base_amplitude)/base_amplitude new_wdata.append(possamp) count+=1 my_the_text="".join(map(chr,new_wdata)) file_object = open('mytext.txt', 'w') file_object.write(my_the_text) file_object.close( )
本博客所有内容是原创,未经书面许可,严禁任何形式的转载
http://blog.csdn.net/u010255642
>>> runfile(r'K:\book_prog\audio_hy.py', wdir=r'K:\book_prog')
http://blog.csdn.net/myhaspl
myhaspl@qq.com
working...
正在将文件编码进声音
generate wav data....
save new wav files....
正在从声音解码文件
>>>
上图是对一段python代码文件的加密后形成的声音波形,加密的python代码文件内容如下:
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#code:myhaspl@qq.com
import cv2
import numpy as np
fn="test1.jpg"
print 'http://blog.csdn.net/myhaspl'
print 'myhaspl@qq.com'
print
print 'loading %s ...' % fn
print 'working',
img = cv2.imread(fn)
w=img.shape[1]
h=img.shape[0]
sz1=w
sz0=h
EVENTS = ['CV_EVENT_MOUSEMOVE', 'CV_EVENT_LBUTTONDOWN', 'CV_EVENT_RBUTTONDOWN', 'CV_EVENT_MBUTTONDOWN', 'CV_EVENT_LBUTTONUP',
'CV_EVENT_RBUTTONUP', 'CV_EVENT_MBUTTONUP' , 'CV_EVENT_LBUTTONDBLCLK','CV_EVENT_RBUTTONDBLCLK','CV_EVENT_MBUTTONDBLCLK']
def callback_function(event,x,y,flag,param):
global EVENTS
global img
if EVENTS[event]=='CV_EVENT_LBUTTONDOWN':
print "(%d,%d):"%(y,x)
print img[y,x,:]
cv2.imshow('img', img)
return
def getdistance(color1,color2):
return np.sqrt(sum((color1-color2)*(color1-color2)))
cv2.namedWindow('img')
cv2.setMouseCallback('img', callback_function)
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()