我正在尝试在python中记录音频原始数据,对数据进行加密,然后将其发送到.NET服务器,在其中解密接收到的数据并转换为字节数组。当我像这样Encoding.ASCII.GetBytes(decryptedData)将接收到的数据转换为字节数组时,一切都差不多了。但是最大字节值为63,最大发送数据字节值为255。发送和接收的数据示例:
发送数据
3、0、3、0、3、0、4、0、4、0、2、0、252、255、1、0、255、255、1、0、0、0 ... < / p>
收到的数据
3,0,3,0,3,0,4,0,4,0,2,0,63,63,1,0,63,63,1,0,0,0 ... < / p>
当我像这样Encoding.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 ... < / p>
收到的数据
6,0,8,0,239,191,189,239,191,189,239,191,189,239,191,189,3,0,6,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#代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace DOTNETServer
{
class Program
{
private static string IP = "127.0.0.1";
private static int Port = 5005;
private static string Key = "bpjAqrgO2N8q7Rfj8IHzeRxmP1W4HwUTWCRi7DQgyDc=";
private static string IV = "Ta6e1cZAWQMM0QI66JC74w==";
static void Main(string[] args)
{
UDPServer(IPAddress.Parse(IP), Port);
Console.ReadKey();
}
private static void UDPServer(IPAddress IP, int Port)
{
byte[] data = new byte[32768];
IPEndPoint endPoint = new IPEndPoint(IP, Port);
UdpClient client = new UdpClient(endPoint);
SymmetricAlgorithm aes = new AesManaged();
aes.KeySize = 256;
aes.Key = Convert.FromBase64String(Key);
aes.IV = Convert.FromBase64String(IV);
while (true)
{
Console.WriteLine("Waiting for data.");
data = client.Receive(ref endPoint);
var convertedReceivedData = Encoding.ASCII.GetBytes(DecryptData(aes, data));
Console.Write("(");
foreach(var item in convertedReceivedData)
{
Console.Write(item + ", ");
}
Console.Write(")");
}
}
static byte[] EncryptText(SymmetricAlgorithm aesAlgorithm, string text)
{
ICryptoTransform encryptor = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV);
byte[] data = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(cs))
{
writer.Write(text);
}
}
data = ms.ToArray();
}
return data;
}
static string DecryptData(SymmetricAlgorithm aes, byte[] data)
{
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
byte[] encryptedDataBuffer = data;
string plainText = "";
using (MemoryStream ms = new MemoryStream(encryptedDataBuffer))
{
using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read))
{
using (StreamReader reader = new StreamReader(cs))
{
plainText = reader.ReadToEnd();
}
}
}
return plainText;
}
}
}
答案 0 :(得分:1)
好的,我用elgonzo的建议解决了我的问题。 可以肯定的是,我从数据中打印出每个字节的数据字节,而无需像这样在python中进行任何转换或拆包:
data = stream.read(chunk)
for item in data:
print (item)
在.NET应用程序中,只需使用Rijndael进行加密和解密,使用输入byte []并使用byte []作为输出,如下所示:
public static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael alg = Rijndael.Create();
alg.Key = Key;
alg.IV = IV;
CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(cipherData, 0, cipherData.Length);
cs.Close();
return ms.ToArray();
}
再次感谢您,祝您愉快:)