Example #1
0
 public static string HexString(this byte[] bytes, int len)
 {
     return(DataFormatProcessor.BytesToHexString(bytes, len));
 }
        /// <summary>
        /// 加密任务结果内容
        /// </summary>
        /// <param name="filestr"></param>
        /// <returns></returns>
        private static String EncryptInfo(String filestr)
        {
            //使用 DataFormatProcessor获取 4字节
            Byte[] validdata = DataFormatProcessor.StringToBytes(filestr);
            if (validdata == null || validdata.Length < 1)
            {
                return(null);
            }
            //总长度
            Int32 allLen = validdata.Length;

            //原文
            Byte[] rawbytes = new Byte[Const_AESEncryptLen];
            //密文
            Byte[] endata     = null;
            String Ciphertext = String.Empty;

            if (allLen <= Const_AESEncryptLen) //小于等于16字节
            {
                Array.Copy(validdata, 0, rawbytes, 0, validdata.Length);
                //AES加密
                m_aes.Encrypt(rawbytes, ref endata);

                //转换为HEX string
                Ciphertext = DataFormatProcessor.BytesToHexString(endata);

                return(Ciphertext);
            }
            //原始数据大于16字节,分段传输
            //已发送的长度
            Int32 sentDataTableLen = 0;
            //剩余需要发送的长度
            Int32 lastDataTableLen = allLen;

            rawbytes = new Byte[] { };
            List <String> CiphertextList = new List <string> {
            };

            //检查当前剩下还没发送的数据长度,是否超过32K
            while (lastDataTableLen > Const_AESEncryptLen) //超过时,分段发送
            {
                endata   = null;
                rawbytes = new Byte[Const_AESEncryptLen];
                Array.Copy(validdata, sentDataTableLen, rawbytes, 0, Const_AESEncryptLen);
                //随后 已发送长度增加32K
                sentDataTableLen += Const_AESEncryptLen;
                lastDataTableLen -= Const_AESEncryptLen;

                //AES加密
                m_aes.Encrypt(rawbytes, ref endata);
                //转换为HEX string
                Ciphertext = DataFormatProcessor.BytesToHexString(endata);
                CiphertextList.Add(Ciphertext);
            }
            endata = null;
            //如果剩余不足32K, 直接全部发送,并且通知客户端已经发送完毕
            rawbytes = new Byte[Const_AESEncryptLen];
            Array.Copy(validdata, sentDataTableLen, rawbytes, 0, lastDataTableLen);
            //AES加密
            m_aes.Encrypt(rawbytes, ref endata);

            //转换为HEX string
            Ciphertext = DataFormatProcessor.BytesToHexString(endata);
            CiphertextList.Add(Ciphertext);


            return(DataFormatProcessor.ListToString(CiphertextList, ""));
        }