头头上我还可以定义吗

asp实现md5、aes、hmac、base64 支持utf-8

以前被asp网上各种类坑,连一个md5都有各种写法,带上编码又有各种写法。

现在直接用这个类就好了,可以和php, .net出来的效果完全一样,主要是稳 。

fff

Class AspFramework
	Private codingname
	
	Private Sub Class_Initialize()
		codingname = "gbk"
	End Sub

	Public Property Let Charset(value)
		If inArray(Array("utf-8","utf8","gbk","gb2312","ascii","unicode"),value) Then
			codingname = LCase(value)
		End If
	End Property
	
	Function MD5(message)
		MD5 = BaseCrypt("md5",message)
	End Function

	Function MD5_16(message)
		MD5_16 = Mid(MD5(message),9,16)
	End Function

	Function Base64(message)
		texthash = ConvertBytes(message,True)
		Base64 = ConvertBase64(texthash,true)
	End Function

	Function FromeBase64(b64code)
		bytes = ConvertBase64(b64code,False)
		FromeBase64 = ConvertBytes(bytes,False)		
	End Function
	
	Function HmacSha1(message,seckey)
		HmacSha1 = HmacCrypt("sha1",message,seckey)
	End Function

	Function AES(message,isencode)
		AES = AESCrypt(message,"","",isencode)
	End Function

	Function HmacCrypt(tp,message,seckey)
		Dim spname,enc,texthash,bytes
		If InStr(tp,"sha1")>0 Then
			spname = "HMACSHA1"
		ElseIf InStr(tp,"sha2")>0 Then
			spname = "HMACSHA256"
		ElseIf InStr(tp,"sha3")>0 Then
			spname = "HMACSHA384"
		ElseIf InStr(tp,"sha5")>0 Then
			spname = "HMACSHA512"
		ElseIf InStr(tp,"md5")>0 Then
			spname = "HMACMD5"
		End If
		Set enc = CreateObject("System.Security.Cryptography." & spname)
		texthash = ConvertBytes(message,True)
		seckeyhash = ConvertBytes(seckey,True)
		enc.Key = (seckeyhash)
		bytes = enc.ComputeHash_2((texthash))
		If InStr(tp,"base64")>0 Then
			HmacCrypt = ConvertBase64(bytes,true)
		Else
			HmacCrypt = HexToString(bytes)
		End If
		Set enc=Nothing
	End Function

	Function BaseCrypt(tp,message)
		Dim spname,enc,texthash,bytes
		If InStr(tp,"sha1")>0 Then
			spname = "SHA1Managed"
		ElseIf InStr(tp,"sha2")>0 Then
			spname = "SHA256Managed"
		ElseIf InStr(tp,"sha3")>0 Then
			spname = "SHA384Managed"
		ElseIf InStr(tp,"sha5")>0 Then
			spname = "SHA512Managed"
		ElseIf InStr(tp,"md5")>0 Then
			spname = "MD5CryptoServiceProvider"
		End If
		Set enc = CreateObject("System.Security.Cryptography." & spname)
		texthash = ConvertBytes(message,True)
		bytes = enc.ComputeHash_2((texthash))
		If InStr(tp,"base64")>0 Then
			BaseCrypt = ConvertBase64(bytes,true)
		Else
			BaseCrypt = HexToString(bytes)
		End If
		Set enc=Nothing
	End Function

	Function AESCrypt(message,ByVal key,ByVal iv,isencode)
		Dim enc,dec,bytes,bytec,byted
		AESCrypt = ""
		On Error Resume Next : Err.clear
		With CreateObject("System.Security.Cryptography.RijndaelManaged") 
			.KeySize = 128 : .BlockSize   = 128 
			.Mode    = 1' 1=CBC ,2=ECB  ,填充模式固定pkcs5padding
			.IV  = ConvertBytes(iv,True)
			.Key = ConvertBytes(key,True)
			set enc = .CreateEncryptor()
			set dec = .CreateDecryptor()
		End With
		If isencode Then
			bytes = ConvertBytes(message,True)
			bytec = enc.TransformFinalBlock((bytes),0,lenb(bytes))
			AESCrypt = ConvertBase64((bytec),true) 
		Else
			bytes = ConvertBase64(message,False)
			byted = dec.TransformFinalBlock((bytes),0,lenb(bytes)) 
			AESCrypt = ConvertBytes((byted),False)
		End If
	End Function
	'-------------------
	Function GetEncodecom()
		GetEncodecom = "System.Text." & Replace(codingname,"-","") & "Encoding"
	End Function

	Function ConvertBytes(value,isencode)
		If codingname="gbk" Or codingname="gb2312" Then
			With CreateObject("ADODB.Stream")
				If isencode Then
					.Mode = 3:.Type = 2:.open:.Charset = codingname
					.WriteText value
					.position = 0:.Type = 1
					ConvertBytes = .Read
				Else
					.Mode = 3:.Type = 1:.open:.Write value
					.position = 0:.Type = 2:.Charset = codingname
					ConvertBytes = .ReadText
				End If
				.close
			End With
		Else
			If isencode Then
				ConvertBytes = CreateObject(GetEncodecom).GetBytes_4(value)
			Else
				ConvertBytes = CreateObject(GetEncodecom).GetString((value))
			End If
		End If
	End Function
	
	Function HexToString(vIn)
		With CreateObject("MSXML2.DomDocument").CreateElement("root")
			.dataType = "bin.Hex"
			.nodeTypedValue = vIn
			HexToString = Replace(.Text,vblf,"")
		End With
	End Function
	
	Function ConvertBase64(value,isencode)
		With CreateObject("MSXML2.DomDocument").CreateElement("root")
			.dataType = "bin.base64"
			If isencode Then 
				.nodeTypedValue = value
				ConvertBase64 = Replace(.Text,vblf,"")
			Else
				.Text = value
				ConvertBase64 = .nodeTypedValue
			End If
		End With
	End Function
End Class

  

posted @ 2020-06-11 17:13  sky毛毛虫  阅读(938)  评论(0)    收藏  举报
页脚我还可以定义吗