我尝试用python记录音频原始数据,加密数据并将其发送到.NET服务器,在那里我解密接收到的数据并转换为字节数组。当我接收到这个字节数组时编码.ASCII.GetBytes(解密数据)一切都快好了。但最大字节值是63,发送数据的最大字节值是255。发送和接收数据示例:
发送的数据
3,0,3,0,3,0,4,0,4,0,2,0,252,255,1,0,255,255,1,0,0,0。。。
接收到的数据
3,0,3,0,3,0,4,0,4,0,2,0,63,63,1,0,63,63,1,0,0,0。。。
当我像这样把接收到的数据转换成字节数组时编码.UTF8.GetBytes(DecryptData(aes,data))一切正常。但高价值是不同的。发送和接收数据示例:
发送的数据
6,0,8,0,250,255,255,255,3,0,6,0,2,0,4,0,3,0,6,0,3,0。。。
接收到的数据
6、0、8、0、239、191、189、239、191、189、239、191、189、3、0、6、0、2、0、4、0、3、0、6、3、0。。。
似乎转换会产生更多的变量。我不知道。
以下是记录、加密和发送数据的python代码:
import pyaudio
import sys
import socket
import struct
import base64
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
key = b"bpjAqrgO2N8q7Rfj8IHzeRxmP1W4HwUTWCRi7DQgyDc="
iv = b"Ta6e1cZAWQMM0QI66JC74w=="
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
chunk = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
pya = pyaudio.PyAudio()
stream = pya.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, output=True, frames_per_buffer=chunk)
print ("recording")
for i in range(0, 44100 // chunk * RECORD_SECONDS):
data = stream.read(chunk)
print (struct.unpack('{}B'.format(len(data)), data))
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cipher_suite = AES.new(base64.urlsafe_b64decode(key), AES.MODE_CBC, base64.urlsafe_b64decode(iv))
cipher_text = cipher_suite.encrypt(pad(data, 16))
sock.sendto(cipher_text, (UDP_IP, UDP_PORT))
input ()
# check for silence here by comparing the level with 0 (or some threshold) for
# the contents of data.
# then write data or not to a file
print ("done")
stream.stop_stream()
stream.close()
pya.terminate()
以下是接收、解密和转换数据的C#代码:
^{pr2}$
好吧,我用埃尔贡佐的建议解决了我的问题。 当然,我从数据中打印每个字节的数据,而不需要像这样用python进行任何转换或解包:
在.NET应用程序中,只需使用Rijndael对输入字节[]进行加密和解密,并将byte[]作为输出,如下所示:
^{pr2}$再次感谢您,祝您今天愉快:)
相关问题 更多 >
编程相关推荐