public string GetToken()
        {
            ParamEncoder paramEncoder = new ParamEncoder(this.PrivateKey);
            Type         type         = base.GetType();

            FieldInfo[] fields = type.GetFields();
            foreach (FieldInfo fieldInfo in fields)
            {
                object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(EncryptedParamAttribute), false);
                if (customAttributes.Length == 1)
                {
                    EncryptedParamAttribute encryptedParamAttribute = customAttributes[0] as EncryptedParamAttribute;
                    EncryptedParamContainer.AddParam(paramEncoder, encryptedParamAttribute.Key, fieldInfo.GetValue(this), encryptedParamAttribute.Unicode);
                }
            }
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo propertyInfo in properties)
            {
                object[] customAttributes2 = propertyInfo.GetCustomAttributes(typeof(EncryptedParamAttribute), false);
                if (customAttributes2.Length == 1)
                {
                    EncryptedParamAttribute encryptedParamAttribute2 = customAttributes2[0] as EncryptedParamAttribute;
                    EncryptedParamContainer.AddParam(paramEncoder, encryptedParamAttribute2.Key, propertyInfo.GetValue(this, null), encryptedParamAttribute2.Unicode);
                }
            }
            return(paramEncoder.GetToken());
        }
示例#2
0
        private bool Decrypt(string sToken)
        {
            byte[] array = new byte[2048];
            this.Clear();
            int num  = sToken.Length;
            int num2 = 0;

            for (int i = 0; i < num; i++)
            {
                int    num3   = num2 >> 3;
                int    num4   = num2 & 7;
                int    num5   = (int)ScrambleTable.ReverseText[(int)sToken[i]];
                int    num6   = 6 - Math.Min(6, 8 - num4);
                byte[] array2 = array;
                int    num7   = num3;
                array2[num7] |= (byte)(num5 << 2 >> num4);
                if (num6 > 0)
                {
                    byte[] array3 = array;
                    int    num8   = num3 + 1;
                    array3[num8] |= (byte)(num5 << 8 - num6 & 255);
                }
                num2 += 6;
            }
            num = num2 + 1 >> 3;
            int num9 = ((int)array[0] << 8 | (int)array[1]) ^ (int)this.m_uPrivateKey;

            for (int i = 2; i < num; i += 2)
            {
                int num10 = (int)array[i] << 8 | (int)array[i + 1];
                array[i]     = (byte)(ScrambleTable.Reverse16[num10] >> 8 & 255);
                array[i + 1] = (byte)(ScrambleTable.Reverse16[num10] & 255);
                int num11 = (int)array[i] << 8 | (int)array[i + 1];
                array[i]     = (byte)((num11 ^ (int)ScrambleTable.Convert16[num9 + 17 & 65535]) >> 8 & 255);
                array[i + 1] = (byte)((num11 ^ (int)ScrambleTable.Convert16[num9 + 17 & 65535]) & 255);
                num9         = num10;
            }
            num = ((int)array[2] << 8 | (int)array[3]);
            int num12 = (num + 1) / 2 * 2;

            this.m_nHashValue = (long)((ulong)array[num12 + 4]);
            this.m_nHashValue = (this.m_nHashValue << 8 | (long)((ulong)array[num12 + 5]));
            this.m_nHashValue = (this.m_nHashValue << 8 | (long)((ulong)array[num12 + 6]));
            this.m_nHashValue = (this.m_nHashValue << 8 | (long)((ulong)array[num12 + 7]));
            this.m_nHashValue = (this.m_nHashValue << 8 | (long)((ulong)array[num12 + 8]));
            this.m_nHashValue = (this.m_nHashValue << 8 | (long)((ulong)array[num12 + 9]));
            this.m_nHashValue = (this.m_nHashValue << 8 | (long)((ulong)array[num12 + 10]));
            this.m_nHashValue = (this.m_nHashValue << 8 | (long)((ulong)array[num12 + 11]));
            if (this.m_nHashValue == ParamEncoder.GetHashValue(array, 4, num12))
            {
                Array.Copy(array, 4, this.m_aBuffer, 0, num12);
                this.m_nBufferSize = num;
                return(true);
            }
            return(false);
        }
        private static void AddParam(ParamEncoder encoder, string name, object value, bool unicode)
        {
            if (value == null)
            {
                return;
            }
            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.SByte:
                encoder.AddParam(name, (sbyte)value);
                return;

            case TypeCode.Byte:
                encoder.AddParam(name, (byte)value);
                return;

            case TypeCode.Int16:
                encoder.AddParam(name, (short)value);
                return;

            case TypeCode.UInt16:
                encoder.AddParam(name, (ushort)value);
                return;

            case TypeCode.Int32:
                encoder.AddParam(name, (int)value);
                return;

            case TypeCode.UInt32:
                encoder.AddParam(name, (uint)value);
                return;

            case TypeCode.Int64:
                encoder.AddParam(name, (long)value);
                return;

            case TypeCode.UInt64:
                encoder.AddParam(name, (ulong)value);
                return;

            case TypeCode.String:
                encoder.AddParam(name, (string)value, unicode);
                return;
            }
            throw new EncryptedParamException("Unsupported type : " + value.GetType().ToString(), null);
        }
        public virtual void SetToken(string token)
        {
            this.Reset();
            if (token == null)
            {
                throw new EncryptedParamException("NullToken", null);
            }
            ParamEncoder paramEncoder = new ParamEncoder(this.PrivateKey);

            try
            {
                if (!paramEncoder.SetToken(token))
                {
                    throw new EncryptedParamException("InvalidToken", null);
                }
            }
            catch (Exception exception)
            {
                throw new EncryptedParamException("DecryptingTokenFailed", exception);
            }
            Type type = this.GetType();

            FieldInfo[]    fields         = type.GetFields();
            PropertyInfo[] properties     = type.GetProperties();
            FieldInfo[]    fieldInfoArray = fields;
            for (int i = 0; i < (int)fieldInfoArray.Length; i++)
            {
                FieldInfo fieldInfo        = fieldInfoArray[i];
                object[]  customAttributes = fieldInfo.GetCustomAttributes(typeof(EncryptedParamAttribute), false);
                if ((int)customAttributes.Length == 1)
                {
                    EncryptedParamAttribute encryptedParamAttribute = customAttributes[0] as EncryptedParamAttribute;
                    object paramValue = paramEncoder.GetParamValue(encryptedParamAttribute.Key);
                    if (paramValue != null)
                    {
                        try
                        {
                            fieldInfo.SetValue(this, paramValue);
                        }
                        catch (FieldAccessException)
                        {
                            throw new EncryptedParamException(string.Concat("FieldAccessDenied : ", fieldInfo.Name));
                        }
                        catch (ArgumentException)
                        {
                            throw new EncryptedParamException(string.Concat("ConvertingFailed : ", fieldInfo.Name));
                        }
                        catch (Exception exception2)
                        {
                            Exception exception1 = exception2;
                            throw new EncryptedParamException(string.Concat("DecodingFieldsFailed : ", fieldInfo.Name), exception1);
                        }
                    }
                    else if (!encryptedParamAttribute.Optional)
                    {
                        string[] name = new string[] { "ParamNotExists : ", fieldInfo.Name, "[", encryptedParamAttribute.Key, "]" };
                        throw new EncryptedParamException(string.Concat(name));
                    }
                }
            }
            PropertyInfo[] propertyInfoArray = properties;
            for (int j = 0; j < (int)propertyInfoArray.Length; j++)
            {
                PropertyInfo propertyInfo = propertyInfoArray[j];
                object[]     objArray     = propertyInfo.GetCustomAttributes(typeof(EncryptedParamAttribute), false);
                if ((int)objArray.Length == 1)
                {
                    EncryptedParamAttribute encryptedParamAttribute1 = objArray[0] as EncryptedParamAttribute;
                    object obj = paramEncoder.GetParamValue(encryptedParamAttribute1.Key);
                    if (obj != null)
                    {
                        try
                        {
                            propertyInfo.SetValue(this, obj, null);
                        }
                        catch (MethodAccessException)
                        {
                            throw new EncryptedParamException(string.Concat("FieldAccessDenied : ", propertyInfo.Name));
                        }
                        catch (ArgumentException)
                        {
                            throw new EncryptedParamException(string.Concat("ConvertingFailed : ", propertyInfo.Name));
                        }
                        catch (Exception exception4)
                        {
                            Exception exception3 = exception4;
                            throw new EncryptedParamException(string.Concat("DecodingFieldsFailed : ", propertyInfo.Name), exception3);
                        }
                    }
                    else if (!encryptedParamAttribute1.Optional)
                    {
                        string[] strArrays = new string[] { "ParamNotExists : ", propertyInfo.Name, "[", encryptedParamAttribute1.Key, "]" };
                        throw new EncryptedParamException(string.Concat(strArrays));
                    }
                }
            }
            this._isValid = true;
        }
示例#5
0
        private string Encrypt()
        {
            byte[] array  = new byte[4096];
            Random random = new Random((int)DateTime.Now.Ticks);
            int    i      = 0;
            int    num    = 0;

            Encoding.ASCII.GetString(this.m_aBuffer);
            int num2 = (this.m_nBufferSize + 1) / 2 * 2;

            this.m_nHashValue = ParamEncoder.GetHashValue(this.m_aBuffer, 0, num2);
            int num3 = random.Next(0, 65535);

            array[0] = (byte)(num3 >> 8 & 255);
            array[1] = (byte)(num3 & 255);
            array[2] = (byte)(this.m_nBufferSize >> 8 & 255);
            array[3] = (byte)(this.m_nBufferSize & 255);
            Array.Copy(this.m_aBuffer, 0, array, 4, num2);
            array[num2 + 4]  = (byte)(this.m_nHashValue >> 56 & 255L);
            array[num2 + 5]  = (byte)(this.m_nHashValue >> 48 & 255L);
            array[num2 + 6]  = (byte)(this.m_nHashValue >> 40 & 255L);
            array[num2 + 7]  = (byte)(this.m_nHashValue >> 32 & 255L);
            array[num2 + 8]  = (byte)(this.m_nHashValue >> 24 & 255L);
            array[num2 + 9]  = (byte)(this.m_nHashValue >> 16 & 255L);
            array[num2 + 10] = (byte)(this.m_nHashValue >> 8 & 255L);
            array[num2 + 11] = (byte)(this.m_nHashValue & 255L);
            array[num2 + 12] = 0;
            for (int j = 2; j < num2 + 12; j += 2)
            {
                int num4 = ((int)array[j - 2] << 8 | (int)array[j - 1]) + 17 & 65535;
                int num5 = (int)array[j] << 8 | (int)array[j + 1];
                num5        ^= (int)ScrambleTable.Convert16[num4];
                array[j]     = (byte)(ScrambleTable.Convert16[num5] >> 8 & 255);
                array[j + 1] = (byte)(ScrambleTable.Convert16[num5] & 255);
            }
            array[0] = (byte)((int)array[0] ^ (this.m_uPrivateKey >> 8 & 255));
            array[1] = (byte)((ushort)array[1] ^ (this.m_uPrivateKey & 255));
            int num6 = num2 + 12 << 3;

            char[] array2 = new char[((num2 + 12) * 8 + 5) / 6];
            while (i < num6)
            {
                int num7  = i >> 3;
                int num8  = i & 7;
                int num9  = 6 - Math.Min(6, 8 - num8);
                int num10 = (int)array[num7] << num8 & 255;
                num10 >>= 2;
                if (num9 > 0)
                {
                    num10 |= array[num7 + 1] >> 8 - num9;
                }
                if (num10 > 64)
                {
                    return(null);
                }
                array2[num] = ScrambleTable.ConvertText[num10];
                num++;
                i += 6;
            }
            return(new string(array2));
        }