





















Author:水如烟
.NET的安全性问题是一个综合性的问题,这不是我现在能学习好理解透的,我估计也不是一般的.NET的编码者能够把握住应用好的。
至少,在代码集中,.NET的全局变量,或者说受Private修饰保护的对象,是不安全的。
在现实中有一个传话游戏,其要义是,信息在信息链的传递过程中会失真。.NET也一样,很难保证一个关键的信息在传递的过程中保持“纯洁”而不被潜伏中的攻击而受污染。特别是在大规模的代码集中。
SecureString类
在.NET的设计中,SecureString类应该是安全系数比较高的。它有两个作用,一是赋值后MakeReadOnly以便在后续的传递中不致修改,二是在必要的时候进行销毁。但是用它来传递关键信息,比如密码,还是不安全的。这个类里头有一个m_readOnly As Boolean的Private变量来保存当前实例可否修改的状态,你可以重置该值为False,便可以对它重新修改,MakeReadOnly也就失去了它本身的作用。
ReadOnlyCollection类
它的本义是不能对当前实例的项目进行增删。在它的设计中,用一个list的Private变量来存储当前集合。事实并不能保证它的作用。你甚至可以用自己的list替换了它。
测试代码
Imports System.Runtime.InteropServices
Imports System.Collections.ObjectModelPublic Class NoSecurePublic Sub TestSecureString()
Dim t As New Security.SecureString
t.AppendChar("A"c)
t.AppendChar("B"c)
t.MakeReadOnly()
Console.WriteLine(Marshal.PtrToStringAuto(Marshal.SecureStringToBSTR(t)))
Dim h As New LzmTW.uSystem.uReflection.TypeHelper(t)t.AppendChar(
"C"c)Console.WriteLine(Marshal.PtrToStringAuto(Marshal.SecureStringToBSTR(t)))
End SubPublic Sub TestReadonlyCollection()Dim t As New ReadOnlyCollection(Of String)(New String() {"A", "B", "C"})Console.WriteLine()
Dim h As New LzmTW.uSystem.uReflection.TypeHelper(t)TypeHelper.Methods.vb
Imports System.ReflectionNamespace LzmTW.uSystem.uReflection
Partial
Class TypeHelperPublic Sub SetCurrentObj(ByVal obj As Object)TypeHelper.vb
Imports System.ReflectionNamespace LzmTW.uSystem.uReflectionPublic Class TypeHelperPrivate gType As Type
Private gCurrentObjct As ObjectPublic ReadOnly Property CurrentType() As Type
Get
Return gType
End Get
End PropertyPublic ReadOnly Property CurrentObject() As Object
Get
Return gCurrentObjct
End Get
End PropertySub New(ByVal referrenceObj As Object)
If Me.IsType(referrenceObj) ThenMe.gType = CType(referrenceObj, Type)
ElseMe.gType = referrenceObj.GetType
Me.gCurrentObjct = referrenceObj
End If
End SubSub New(ByVal assembly As Assembly, ByVal fullTypeName As String)
Me.InternalCreate(assembly, fullTypeName)Me.InternalCheckIsValid(fullTypeName, True)
End SubSub New(ByVal referrenceType As Type, ByVal typeName As String, Optional ByVal isNestedType As Boolean = False)Dim mAssembly As Assembly = referrenceType.AssemblyMe.InternalCreate(mAssembly, typeName)If Me.InternalCheckIsValid(typeName, False) Then ReturnDim mFullTypeName As String = GetFullTypeName(referrenceType, typeName, isNestedType)Me.InternalCreate(mAssembly, mFullTypeName)Me.InternalCheckIsValid(mFullTypeName, True)
End SubPrivate Sub InternalCreate(ByVal ass As Assembly, ByVal fulltypename As String)
gType = ass.GetType(fulltypename, False, True)
End SubPrivate Function InternalCheckIsValid(ByVal typename As String, ByVal throwOnError As Boolean) As Boolean
If gType Is Nothing Then
If throwOnError Then
Throw New ArgumentException(String.Format("typeName: {0} 不存在", typename))
Else
Return False
End If
End IfReturn True
End FunctionPrivate Function GetFullTypeName(ByVal referrenceType As Type, ByVal typeName As String, ByVal isNestedType As Boolean) As String
Dim mFullName As String = NothingDim mRefFullName As String = referrenceType.FullNameIf isNestedType Then
mFullName
= String.Concat(mRefFullName, "+", typeName)mFullName
= String.Concat(mRefFullName.Substring(0, mLastIndex), typeName)此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。