Ejemplo n.º 1
0
        /// <summary>
        /// 根据加密类型加密
        /// </summary>
        /// <param name="inputBytes"> 需要加密的正文 </param>
        /// <param name="randomBytes">随机填充</param>
        /// <param name="aesKeyStatic">静态密钥</param>
        /// <param name="aesKeyDynamic">动态密钥</param>
        /// <param name="type">加密类型</param>
        /// <param name="ms">内存流</param>
        private static void InternalWriteEncryptDataByType(byte[] inputBytes, byte[] randomBytes, string aesKeyStatic, string aesKeyDynamic, EncryptType type, MemoryStream ms)
        {
            byte[] encBytes = inputBytes;
            int    intType  = (int)type;


            //客户端密钥更新强制优先检查
            if ((intType & ((int)EncryptType.ResetKey)) != 0)
            {
                InternalWriteKeyUpdate(aesKeyStatic, randomBytes, ms);
            }



            int         maxType = 16;
            int         child;
            EncryptType childType;
            int         encryptCount = 0;

            while (maxType > 0)
            {
                maxType = maxType >> 1;
                child   = maxType & intType;
                if (child == 0)
                {
                    continue;
                }
                encryptCount++;
                childType = (EncryptType)child;

                switch (childType)
                {
                case EncryptType.ResetKey:
                    break;

                case EncryptType.AES:
                    encBytes = Cipher_Aes.EncryptToBytes(encBytes, aesKeyDynamic);
                    break;

                case EncryptType.Gzip:
                    encBytes = Cipher_Gzip.Compress(encBytes);
                    break;

                case EncryptType.PT:
                    break;

                default:
                    throw new Exception("未受支持的加密类型");
                }
            }

            if (encryptCount < 0)
            {
                throw new Exception("未识别的加密类型");
            }
            if (encBytes != null)
            {
                ms.Write(encBytes, 0, encBytes.Length);
            }
        }
Ejemplo n.º 2
0
 public static string ToGzip(this string input) => Convert.ToBase64String(Cipher_Gzip.Compress(Encoding.UTF8.GetBytes(input)));