⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aes.asp

📁 前几天刚下的一个ASP的AES源码 输入keySize密钥长度(128、192、256),Src明文字符串
💻 ASP
📖 第 1 页 / 共 2 页
字号:
<%
'**********************************************
' File:		aes.asp
' Version:	Advanced Encryption StAndard Class Version 1.0 Build20051017
' Author:	LessBug
' Email:	LessBug@gmail.com
' Date:		10/17/2005
' Comments:	The code For the Advanced Encryption StAndard.
'			This can free usage, but please
'			not To delete this copyright inFormation.
'			If you have a ModIfication version,
'			Please sEnd out a duplicate To me.
'           Thank you! ^-^
'**********************************************
' 文件名:	aes.asp
' 版本:		Advanced Encryption StAndard Class Version 1.0 Build20051017
' 作者:		LessBug(小虫)
' 电子邮件:	LessBug@gmail.com
' 日期:		2005年10月17日
' 声明:		AES数据加密/解密类
'			本代码可以自由使用,但请保留此版权声明信息
'			如果您对本上传类进行修改增强,
'			请发送一份给我。
'           谢谢!^-^
'**********************************************

Class aes_class
'**********************************************
' 输入:keySize密钥长度(128、192、256),Src明文字符串,Key密钥字符串
'       明文字符串不能超过 &HFFFF长度
' 输出:密文十六进制字符串
Public Function CipherStrToHexStr(keySize, Src, Key)
	SetNbNkNr keySize
	InitKey Key
	Dim i, Result, str32, input(15), output(15)
	Dim HexString, iLen, sLen
	iLen = Len(Src)
	sLen = CStr(Hex(iLen))
	sLen = String(4-Len(sLen), "0")&sLen
	HexString = sLen & HexStr(Src)
	Result = ""
	i = 0
	str32 = Mid(HexString, 1, 32)
	Do While Len(str32) > 0
		HexStrToArray str32, input
		FCipher input, output
		Result = Result + ArrayToHexStr(output)
		i = i + Len(str32)
		str32 = Mid(HexString, i + 1, 32)
	Loop
	CipherStrToHexStr = Result
End Function

'**********************************************
' 输入:keySize密钥长度(128、192、256),Src明文十六进制符串,Key密钥字符串
'       明文十六进制字符串不能超过 2 * &HFFFF长度
' 输出:密文十六进制字符串
Public Function CipherHexStrToHexStr(keySize, Src, Key)
	SetNbNkNr keySize
	InitKey Key
	Dim i, Result, str32, input(15), output(15)
	Dim HexString, iLen, sLen
	iLen = Len(Src) \ 2
	if iLen > 2 * &HFFFF then Src = Left(Src, 2 * &HFFFF)
	sLen = CStr(Hex(iLen))
	sLen = String(4-Len(sLen), "0")&sLen
	HexString = sLen & Src
	Result = ""
	i = 0
	str32 = Mid(HexString, 1, 32)
	Do While Len(str32) > 0
		HexStrToArray str32, input
		FCipher input, output
		Result = Result + ArrayToHexStr(output)
		i = i + Len(str32)
		str32 = Mid(HexString, i + 1, 32)
	Loop
	CipherHexStrToHexStr = Result
End Function

'**********************************************
' 输入:keySize密钥长度(128、192、256),Src密文十六进制符串,Key密钥字符串
' 输出:解密后的字符串
Public Function InvCipherHexStrToStr(keySize, Src, Key)
	SetNbNkNr keySize
	InitKey Key
	Dim i, Result, str32, input(15), output(15)
	Dim HexString, iLen, sLen, str
	HexString = Src
	Result = ""
	i = 0
	str32 = Mid(HexString, 1, 32)
	i = i + Len(str32)
	HexStrToArray str32, input
	FInvCipher input, output
	str = ArrayToHexStr(output)
	sLen = Left(str, 4)
	iLen = HexToLng(sLen)
	str = ArrayToStr(output)
	Result = Right(str, 7)
	str32 = Mid(HexString, i + 1, 32)	
	Do While Len(str32) > 0
		HexStrToArray str32, input
		FInvCipher input, output
		Result = Result + ArrayToStr(output)
		i = i + Len(str32)
		str32 = Mid(HexString, i + 1, 32)
	Loop
	InvCipherHexStrToStr = Left(Result, iLen)
End Function

'**********************************************
' 输入:keySize密钥长度(128、192、256),Src密文十六进制符串,Key密钥字符串
' 输出:解密后的十六进制字符串
Public Function InvCipherHexStrToHexStr(keySize, Src, Key)
	SetNbNkNr keySize
	InitKey Key
	Dim i, Result, str32, input(15), output(15)
	Dim HexString, iLen, sLen, str
	HexString = Src
	Result = ""
	i = 0
	str32 = Mid(HexString, 1, 32)
	i = i + Len(str32)
	HexStrToArray str32, input
	FInvCipher input, output
	str = ArrayToHexStr(output)
	sLen = Left(str, 4)
	iLen = HexToLng(sLen)
	Result = Right(str, 28)
	str32 = Mid(HexString, i + 1, 32)	
	Do While Len(str32) > 0
		HexStrToArray str32, input
		FInvCipher input, output
		Result = Result + ArrayToHexStr(output)
		i = i + Len(str32)
		str32 = Mid(HexString, i + 1, 32)
	Loop
	InvCipherHexStrToHexStr = Left(Result, iLen * 4)
End Function

'**********************************************
' 类的实现

Private FSBox(15, 15)
Private FIsBox(15, 15)
Private FRcon(10, 3)
Private FNb, FNk, FNr
Private FKey(31)
Private FW(59, 3)
Private FState(3, 3)

Private Function ArrayToHexStr(Src)
	Dim i, Result
	Result = ""
	For I = LBound(Src) To UBound(Src)
		Result = Result&CStr(MyHex(Src(I)))
	Next
	ArrayToHexStr = Result
End Function

Private Function ArrayToStr(Src)
	Dim i, Result
	Result = ""
	For I = LBound(Src) To UBound(Src) \ 2
		Result = Result&ChrW(Src(2 * I) + Src(2 * I + 1) * &H100)
	Next
	ArrayToStr = Result
End Function

Private Function HexStr(Src)
	Dim i, HexString
	For i = 0 To LenB(Src) - 1
		HexString = HexString&CStr(MyHex(AscB(MidB(Src, i + 1, 1))))
	Next
	HexStr = HexString
End Function

Private Function HexToLng(H)
	HexToLng = CLng(Cstr("&H" & H))
End Function

Private Sub HexStrToArray(Src, Out)
	If IsNull(Src) then Src = ""
	Dim W, i, j
	i = 0
	j = 0
	For i = 0 To Len(Src) \ 2 - 1
		Out(i) = HexToLng(Mid(Src, 2*i + 1, 2))
	Next
	For i = Len(Src) \ 2 To 15
		Out(i) = 0
	Next
End Sub

Private Function CByte(B)
	CByte = B And &H00FF
End Function

Private Function MyHex(B)
	If B < &H10 then
		MyHex = "0"&CStr(Hex(B))
	Else
		MyHex = CStr(Hex(B))
	End If
End Function

'**********************************************
' 初始化工作Key,如果Key中包含Unicode 字符,则仅取Unicode字符的低字节
Private Sub InitKey(key)
	Dim i
	Dim j
	Dim K
	For i = 0 To 31
		FKey(i) = 0
	Next
	If Len(key) > FNk * 4 then
		For i = 0 To FNk * 4 - 1
			K = AscW(Mid(key, i + 1, 1))
			If K > &HFF then K = CByte(K)
			FKey(i) = K
		Next
	Else
		For i = 0 To len(key) - 1
			K = AscW(Mid(key, i + 1, 1))
			If K > &HFF then K = CByte(K)
			FKey(i) = K
		Next		
	End If
	KeyExpansion
End Sub


Private Sub Class_Initialize
	BuildSBox()
	BuildIsBox()
	BuildRcon()
End Sub

Private Sub Class_Terminate	

End Sub

Private Sub SetNbNkNr(keySize)
	FNb = 4
	Select Case keySize
		Case 192
			FNk = 6
			FNr = 12
		Case 256 
			FNk = 8
			FNr = 14
		Case Else '别的都按128 处理
			FNk = 4
			FNr = 10
	End Select
End Sub

Private Sub AddRoundKey(around)
	Dim r, c
	For r = 0 To 3
		For c = 0 To 3
			FState(r, c) = CByte((CLng(FState(r, c)) Xor (Fw((around * 4) + c, r))))
		Next
	Next
End Sub

Private Sub KeyExpansion()
	Dim row
	Dim temp(3)
	Dim i
	For row = 0 To FNk - 1 
		FW(row, 0) = FKey(4 * row)
		FW(row, 1) = FKey(4 * row + 1)
		FW(row, 2) = FKey(4 * row + 2)
		FW(row, 3) = FKey(4 * row + 3)
	Next
	For row = FNk To FNb * (FNr + 1) - 1
		temp(0) = FW(row - 1, 0)
		temp(1) = FW(row - 1, 1)
		temp(2) = FW(row - 1, 2)
		temp(3) = FW(row - 1, 3)
		If row Mod FNk = 0 then
			RotWord temp(0), temp(1), temp(2), temp(3)
			SubWord temp(0), temp(1), temp(2), temp(3)
			temp(0) = CByte((CLng(temp(0))) Xor (CLng(FRcon(row \ FNk, 0))))
			temp(1) = CByte((CLng(temp(1))) Xor (CLng(FRcon(row \ FNk, 1))))
			temp(2) = CByte((CLng(temp(2))) Xor (CLng(FRcon(row \ FNk, 2))))
			temp(3) = CByte((CLng(temp(3))) Xor (CLng(FRcon(row \ FNk, 3))))
		Else 
			If (FNK > 6) And ((row Mod FNk) = 4) then
				SubWord temp(0), temp(1), temp(2), temp(3)
			End If
		End If
		FW(row, 0) = CByte((CLng(FW(row-FNk, 0))) Xor (CLng(temp(0))))
		FW(row, 1) = CByte((CLng(FW(row-FNk, 1))) Xor (CLng(temp(1))))
		FW(row, 2) = CByte((CLng(FW(row-FNk, 2))) Xor (CLng(temp(2))))
		FW(row, 3) = CByte((CLng(FW(row-FNk, 3))) Xor (CLng(temp(3))))
	Next
End Sub

Private Sub SubBytes()
	Dim r, c
	For r = 0 To 3
		For c = 0 To 3
			FState(r, c) = FSBox(FState(r, c) \ 16, FState(r, c) And &H0F)
		Next
	Next
End Sub

Private Sub InvSubBytes()
	Dim r, c
	For r = 0 To 3
		For c = 0 To 3
			FState(r, c) = FIsBox(FState(r, c) \ 16, FState(r, c) And &H0F)
		Next
	Next
End Sub

Private Sub ShIftRows()
	Dim temp(3, 3)
	Dim r, c
	For r = 0 To 3
		For c = 0 To 3
			temp(r, c) = FState(r, c)
		Next
	Next
	For r = 1 To 3
		For c = 0 To 3
			FState(r, c) = temp(r, (c + r) Mod FNb)
		Next
	Next
End Sub

Private Sub InvShIftRows()
	Dim temp(3, 3)
	Dim r, c
	For r = 0 To 3
		For c = 0 To 3
			temp(r, c) = FState(r, c)
		Next
	Next
	For r = 1 To 3
		For c = 0 To 3
			FState(r, (c + r) Mod FNb) = temp(r, c)
		Next
	Next
End Sub

Private Sub MixColumns()
	Dim temp(3, 3)
	Dim r, c
	For r = 0 To 3
		For c = 0 To 3
			temp(r, c) = FState(r, c)
		Next
	Next
	For c = 0 To 3
		FState(0, c) = CByte(CInt(gfmultby02(temp(0, c))) Xor CInt(gfmultby03(temp(1, c))) Xor CInt(gfmultby01(temp(2, c))) Xor CInt(gfmultby01(temp(3, c))))
		FState(1, c) = CByte(CInt(gfmultby01(temp(0, c))) Xor CInt(gfmultby02(temp(1, c))) Xor CInt(gfmultby03(temp(2, c))) Xor CInt(gfmultby01(temp(3, c))))
		FState(2, c) = CByte(CInt(gfmultby01(temp(0, c))) Xor CInt(gfmultby01(temp(1, c))) Xor CInt(gfmultby02(temp(2, c))) Xor CInt(gfmultby03(temp(3, c))))
		FState(3, c) = CByte(CInt(gfmultby03(temp(0, c))) Xor CInt(gfmultby01(temp(1, c))) Xor CInt(gfmultby01(temp(2, c))) Xor CInt(gfmultby02(temp(3, c))))
	Next
End Sub

Private Sub InvMixColumns()
	Dim temp(3, 3)
	Dim r, c
	For r = 0 To 3
		For c = 0 To 3
			temp(r, c) = FState(r, c)
		Next
	Next
	For c = 0 To 3
		FState(0, c) = CByte(CInt(gfmultby0e(temp(0, c))) Xor CInt(gfmultby0b(temp(1, c))) Xor CInt(gfmultby0d(temp(2, c))) Xor CInt(gfmultby09(temp(3, c))))
		FState(1, c) = CByte(CInt(gfmultby09(temp(0, c))) Xor CInt(gfmultby0e(temp(1, c))) Xor CInt(gfmultby0b(temp(2, c))) Xor CInt(gfmultby0d(temp(3, c))))
		FState(2, c) = CByte(CInt(gfmultby0d(temp(0, c))) Xor CInt(gfmultby09(temp(1, c))) Xor CInt(gfmultby0e(temp(2, c))) Xor CInt(gfmultby0b(temp(3, c))))
		FState(3, c) = CByte(CInt(gfmultby0b(temp(0, c))) Xor CInt(gfmultby0d(temp(1, c))) Xor CInt(gfmultby09(temp(2, c))) Xor CInt(gfmultby0e(temp(3, c))))
	Next
End Sub

Private Function gfmultby01(b)
	gfmultby01 = b
End Function

Private Function gfmultby02(b)
	If (b < &H80) then
		gfmultby02 = CByte(CInt(b * 2))
	Else
		gfmultby02 = CByte((CInt(b * 2)) Xor (CInt(&H1b)))
	End If
End Function

Private Function gfmultby03(b)
	gfmultby03 = CByte((CInt(gfmultby02(b))) Xor (CInt(b)))
End Function

Private Function gfmultby09(b)
	gfmultby09 = CByte((CInt(gfmultby02(gfmultby02(gfmultby02(b))))) Xor (CInt(b)))
End Function

Private Function gfmultby0b(b)
	gfmultby0b = CByte((CInt(gfmultby02(gfmultby02(gfmultby02(b))))) Xor (CInt(gfmultby02(b))) Xor (CInt(b)))
End Function

Private Function gfmultby0d(b)
	gfmultby0d = CByte((CInt(gfmultby02(gfmultby02(gfmultby02(b))))) Xor (CInt(gfmultby02(gfmultby02(b)))) Xor (CInt(b)))
End Function

Private Function gfmultby0e(b)
	gfmultby0e = CByte((CInt(gfmultby02(gfmultby02(gfmultby02(b))))) Xor (CInt(gfmultby02(gfmultby02(b)))) Xor (CInt(gfmultby02(b))))
End Function

Private Sub SubWord(B1, B2, B3, B4)
	B4 = FSbox(B4 \ 16, B4 And &H0f )
	B3 = FSbox(B3 \ 16, B3 And &H0f )
	B2 = FSbox(B2 \ 16, B2 And &H0f )
	B1 = FSbox(B1 \ 16, B1 And &H0f )
End Sub

Private Sub RotWord(B1, B2, B3, B4)
	Dim B
	B = B1
	B1 = B2
	B2 = B3
	B3 = B4
	B4 = B
End Sub

Private Sub FCipher(input, output)
	Dim i, around
	For i = 0 To 4 * FNb - 1
		FState(i Mod 4, i \ 4) = input(i)
	Next
	AddRoundKey 0
	For around = 1 To FNr - 1
		SubBytes()
		ShIftRows()
		MixColumns()
		AddRoundKey around
	Next
	SubBytes()
	ShIftRows()
	AddRoundKey FNr
	For i = 0 To FNb * 4 - 1
		output(i) = FState(i Mod 4, i \ 4)
	Next
End Sub

Private Sub FInvCipher(input, output)
	Dim i, around
	For i = 0 To 4 * FNb - 1
		FState(i Mod 4, i \ 4) = input(i)
	Next
	AddRoundKey FNr
	around = FNr - 1
	Do While around >= 1
		InvShIftRows()
		InvSubBytes()
		AddRoundKey around
		InvMixColumns()
		around = around -1
	Loop
	InvShIftRows()
	InvSubBytes()
	AddRoundKey 0	
	For i = 0 To FNb * 4 - 1
		output(i) = FState(i Mod 4, i \ 4)
	Next
End Sub

Private Function BuildSBox()
	FSBox(00, 00) = &H63
	FSBox(00, 01) = &H7C
	FSBox(00, 02) = &H77
	FSBox(00, 03) = &H7B
	FSBox(00, 04) = &HF2
	FSBox(00, 05) = &H6B
	FSBox(00, 06) = &H6F
	FSBox(00, 07) = &HC5
	FSBox(00, 08) = &H30
	FSBox(00, 09) = &H01
	FSBox(00, 10) = &H67
	FSBox(00, 11) = &H2B
	FSBox(00, 12) = &HFE
	FSBox(00, 13) = &HD7
	FSBox(00, 14) = &HAB
	FSBox(00, 15) = &H76
	FSBox(01, 00) = &HCA
	FSBox(01, 01) = &H82
	FSBox(01, 02) = &HC9
	FSBox(01, 03) = &H7D
	FSBox(01, 04) = &HFA
	FSBox(01, 05) = &H59
	FSBox(01, 06) = &H47
	FSBox(01, 07) = &HF0
	FSBox(01, 08) = &HAD
	FSBox(01, 09) = &HD4
	FSBox(01, 10) = &HA2
	FSBox(01, 11) = &HAF
	FSBox(01, 12) = &H9C
	FSBox(01, 13) = &HA4
	FSBox(01, 14) = &H72
	FSBox(01, 15) = &HC0
	FSBox(02, 00) = &HB7
	FSBox(02, 01) = &HFD
	FSBox(02, 02) = &H93
	FSBox(02, 03) = &H26
	FSBox(02, 04) = &H36
	FSBox(02, 05) = &H3F
	FSBox(02, 06) = &HF7
	FSBox(02, 07) = &HCC
	FSBox(02, 08) = &H34
	FSBox(02, 09) = &HA5
	FSBox(02, 10) = &HE5
	FSBox(02, 11) = &HF1
	FSBox(02, 12) = &H71
	FSBox(02, 13) = &HD8
	FSBox(02, 14) = &H31

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -