Exemplo n.º 1
0
        /// <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);
        }