/// <summary> /// 拆包 /// </summary> /// <param name="buff">源文件字节数组</param> /// <returns>原始文件字节数组</returns> public static byte[] Unpackage(byte[] buff) { try { if (Encoding.UTF8.GetString(buff, 0, _key.Length) != _key) { return(null); } ulong l = BitConverter.ToUInt64(buff, _key.Length + 19); byte[] mbuf = new byte[l]; Buffer.BlockCopy(buff, 15 + 19, mbuf, 0, (int)l); byte[] desbuff = DesByte.Decrypt(mbuf); byte[] outbuff = new byte[desbuff.Length + buff.Length - 15 - 19 - (int)l]; Buffer.BlockCopy(desbuff, 0, outbuff, 0, desbuff.Length); Buffer.BlockCopy(buff, 15 + 19 + (int)l, outbuff, desbuff.Length, buff.Length - 15 - 19 - (int)l); return(outbuff); } catch (Exception ex) { throw ex; } }
/// <summary> /// 打包 /// </summary> /// <param name="buff">源文件字节数组</param> /// <param name="lastWritenTime">源文件最后修改日期</param> /// <returns>打包后的字节数组</returns> public static byte[] Package(byte[] buff, string lastWritenTime) { byte[] keyBuff = Encoding.UTF8.GetBytes(_key); byte[] desbuff = null; byte[] otherbuff = null; byte[] tmBuff = Encoding.UTF8.GetBytes(lastWritenTime); if (buff.Length < _stampLength) { desbuff = DesByte.Encrypt(buff); } else { byte[] cutbuff = new byte[_stampLength]; Buffer.BlockCopy(buff, 0, cutbuff, 0, _stampLength); desbuff = DesByte.Encrypt(cutbuff); otherbuff = new byte[buff.Length - _stampLength]; Buffer.BlockCopy(buff, _stampLength, otherbuff, 0, otherbuff.Length); } byte[] bodyLengthBuff = BitConverter.GetBytes((ulong)desbuff.Length); //to combine byte[] outBuff = new byte[keyBuff.Length + tmBuff.Length + bodyLengthBuff.Length + desbuff.Length + (otherbuff == null ? 0 : otherbuff.Length)]; Buffer.BlockCopy(keyBuff, 0, outBuff, 0, keyBuff.Length); Buffer.BlockCopy(tmBuff, 0, outBuff, keyBuff.Length, tmBuff.Length); Buffer.BlockCopy(bodyLengthBuff, 0, outBuff, keyBuff.Length + tmBuff.Length, bodyLengthBuff.Length); Buffer.BlockCopy(desbuff, 0, outBuff, keyBuff.Length + tmBuff.Length + bodyLengthBuff.Length, desbuff.Length); if (otherbuff != null) { Buffer.BlockCopy(otherbuff, 0, outBuff, keyBuff.Length + tmBuff.Length + bodyLengthBuff.Length + desbuff.Length, otherbuff.Length); } return(outBuff); }