MachineKeyWrapper

using System;
using System.Collections;
using System.IO;
using System.Web.Configuration;
using System.Reflection;
using System.Security.Cryptography;
using Lerado.Framework.Configration;

namespace Lerado.Framework.Security
{
    
internal class MachineKeyWrapper
    {
        
private static Type machineKeyType;
        
private static MachineKeySection machineKey;
            
        
static MachineKeyWrapper()
        {
            MachineKeySection machineKeyConfig 
= Config.GetSection("system.web/machineKey"as MachineKeySection;
            
if (machineKey != null)
            {
                machineKey 
= machineKeyConfig;
                machineKeyType 
= machineKeyConfig.GetType();
            }
            
if (machineKeyType == null)
            {
                
// try to get asp.net 2.0 type
                machineKeyType = typeof(System.Web.Configuration.MachineKeySection);
            }
        }

        
internal  static MachineKeySection MachineKeySection
        {
            
get { return MachineKeyWrapper.machineKey; }
        }

        
internal static bool IsDecryptionKeyAutogenerated
        {
            
get
            {
                BindingFlags bf 
= BindingFlags.NonPublic | BindingFlags.Static;
                
return (bool)machineKeyType.GetProperty("IsDecryptionKeyAutogenerated", bf).GetValue(machineKey,null);
            }
        }

        
internal static byte[] EncryptOrDecryptData(bool fEncrypt, byte[] buf, byte[] modifier, int start, int length)
        {
            
return EncryptOrDecryptData(fEncrypt, buf, modifier, start, length, false);
        }

        
internal static byte[] EncryptOrDecryptData(bool fEncrypt, byte[] buf, byte[] modifier, int start, int length, bool useValidationSymAlgo)
        {
            Type enumType 
= Type.GetType("System.Web.Configuration.IVType,System.Web,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"true);
            FieldInfo enumItem1 
= enumType.GetField("None");
            
int enumValue = (int)enumItem1.GetValue(enumType);

            
object newEnumValue = Enum.ToObject(enumType, enumValue);
        

            BindingFlags bf 
= BindingFlags.NonPublic | BindingFlags.Static;
           
// Type[] paramTypes = new Type[] { typeof(bool), typeof(byte[]), typeof(byte[]), typeof(int), typeof(int),typeof(bool) };
            Type[] paramTypes = new Type[] { typeof(bool), typeof(byte[]), typeof(byte[]), typeof(int), typeof(int), enumType,typeof(bool), typeof(bool) };
            MethodInfo EncryptOrDecryptData 
= machineKeyType.GetMethod("EncryptOrDecryptData", bf,null ,paramTypes ,null);
            
            
//return (byte[])EncryptOrDecryptData.Invoke(machineKey, new object[] { fEncrypt, buf, modifier, start, length, useValidationSymAlgo });
            return (byte[])EncryptOrDecryptData.Invoke(machineKey, new object[] { fEncrypt, buf, modifier, start, length,newEnumValue, useValidationSymAlgo,false });
        }
    }
}


posted @ 2010-10-06 19:35  leslie116  阅读(263)  评论(0编辑  收藏  举报