我想将图像从服务器发送到客户端,因此我执行了以下操作:
服务器端的* :
我将图像转换为字节数组,然后使用md5加密数组并将其发送到客户端
这是代码:
byte[] byte1;
Bitmap mg = new Bitmap(@"D:\ffffffffff13.jpg");
Image im = mg;
MemoryStream ms = new MemoryStream();
im.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] bytes = ms.ToArray();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
TripleDESCryptoServiceProvider tDES = new
TripleDESCryptoServiceProvider();
tDES.Key = md5.ComputeHash(Encoding.ASCII.GetBytes("die"));
tDES.Mode = CipherMode.ECB;
tDES.Padding = PaddingMode.PKCS7;
ICryptoTransform trans = tDES.CreateEncryptor();
byte1 = trans.TransformFinalBlock(bytes, 0, bytes.Length);
socket.BeginSend(byte1, 0, byte1.Length, SocketFlags.None, new
AsyncCallback(SendCallback), socket);
客户端* :
解密从服务器接收的字节数组,然后将其转换为图像
这是代码:
byte[] receivedBuff = new byte[4000];
int receiv = clientSocket.Receive(receivedBuff);
byte[] data = new byte[receiv];
Array.Copy(receivedBuff, data, receiv);
MD5CryptoServiceProvider md51 = new MD5CryptoServiceProvider();
TripleDESCryptoServiceProvider tDES1 = new TripleDESCryptoServiceProvider();
tDES1.Key = md51.ComputeHash(Encoding.ASCII.GetBytes("die"));
tDES1.Mode = CipherMode.ECB;
tDES1.Padding = PaddingMode.PKCS7;
ICryptoTransform trans1 = tDES1.CreateDecryptor();
bytes2 = trans1.TransformFinalBlock(data, 0, data.Length);
MemoryStream ms1 = new MemoryStream(bytes2);
Image returnImage = Image.FromStream(ms1);
pictureBox2.Image = returnImage;
但是当客户端从客户端收到字节数组时,此错误中出现错误错误数据
bytes2 = trans1.TransformFinalBlock(data, 0, data.Length);
请帮助