我想将图像从服务器发送到客户端,所以我做了以下操作:
*在服务器端:
我将图像转换为字节数组,然后用 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;
但是当客户端从客户端接收到字节数组时,这个 ligne saing Bad data会发生错误
bytes2 = trans1.TransformFinalBlock(data, 0, data.Length);
请帮忙