ASP 改良版MD5、SHA256多重加密类(二次及多次)(8)


Public Function SHA256(byVal sMessage, byVal p_Len)
Dim HASH(7)
Dim M
Dim W(63)
Dim a
Dim b
Dim c
Dim d
Dim e
Dim f
Dim g
Dim h
Dim i
Dim j
Dim T1
Dim T2
HASH(0) = &H6A09E667
HASH(1) = &HBB67AE85
HASH(2) = &H3C6EF372
HASH(3) = &HA54FF53A
HASH(4) = &H510E527F
HASH(5) = &H9B05688C
HASH(6) = &H1F83D9AB
HASH(7) = &H5BE0CD19
M = SHA256_ConvertToWordArray(sMessage)
For i = 0 To UBound(M) Step 16
a = HASH(0)
b = HASH(1)
c = HASH(2)
d = HASH(3)
e = HASH(4)
f = HASH(5)
g = HASH(6)
h = HASH(7)
For j = 0 To 63
If j < 16 Then
W(j) = M(j + i)
Else
W(j) = SHA256_AddUnsigned(SHA256_AddUnsigned(SHA256_AddUnsigned(SHA256_Gamma1(W(j - 2)), W(j - 7)), SHA256_Gamma0(W(j - 15))), W(j - 16))
End If
T1 = SHA256_AddUnsigned(SHA256_AddUnsigned(SHA256_AddUnsigned(SHA256_AddUnsigned(h, SHA256_Sigma1(e)), SHA256_CH(e, f, g)), SHA256_K(j)), W(j))
T2 = SHA256_AddUnsigned(SHA256_Sigma0(a), SHA256_Maj(a, b, c))
h = g
g = f
f = e
e = SHA256_AddUnsigned(d, T1)
d = c
c = b
b = a
a = SHA256_AddUnsigned(T1, T2)
Next
HASH(0) = SHA256_AddUnsigned(a, HASH(0))
HASH(1) = SHA256_AddUnsigned(b, HASH(1))
HASH(2) = SHA256_AddUnsigned(c, HASH(2))
HASH(3) = SHA256_AddUnsigned(d, HASH(3))
HASH(4) = SHA256_AddUnsigned(e, HASH(4))
HASH(5) = SHA256_AddUnsigned(f, HASH(5))
HASH(6) = SHA256_AddUnsigned(g, HASH(6))
HASH(7) = SHA256_AddUnsigned(h, HASH(7))
Next
SHA256 = ""
If p_Len >= 8 Then p_Len = 8
If p_Len <= 0 Then p_Len = 2
For i = 0 To p_Len - 1
SHA256 = SHA256 & Right("00000000" & Hex(HASH(i)), 8)
Next
SHA256 = LCase(SHA256)
End Function

Public Function EncryptMode(ByVal p_String, ByVal p_Type)
Dim E1s, E2s
E1s = 16 ' 第一次或者MD5算法加密的长度 16位MD5 加密长度
E2s = 2 ' 第二次或者SHA256算法加密的长度 2*8=16位SHA256 加密长度,最大加密长度为8*8=64位
If Not IsNumeric(p_Type) Then
p_Type = 1
ElseIf p_Type > 7 or p_Type < 0 Then '判断方案数,根据p_Type最大值判断
p_Type = 1
End If
If Not IsNumeric(E1s) Then
E1s = 16
ElseIf E1s <> 16 And E1s <> 32 Then
E1s = 16
End If
If Not IsNumeric(E2s) Then
E2s = 2
ElseIf E2s > 8 or E2s < 0 Then
E2s = 2
End If
' ==========================================
' 初次布置使用的时候最好调整加密方案,现在最多8种方案
' 加密后字符串长度为16位,使用中可自行调整
' ==========================================
Select Case p_Type
Case 0 ' MD5 算法
EncryptMode = MD5(p_String, E1s)
Case 1 ' SHA256 算法
EncryptMode = SHA256(p_String, E2s)
Case 2 ' 先 MD5 后 SHA256
EncryptMode = SHA256(MD5(p_String, E1s), E2s)
Case 3 ' 先 SHA256 后 MD5
EncryptMode = MD5(SHA256(p_String, E2s), E1s)
Case 4 ' 两次MD5
EncryptMode = MD5(MD5(p_String, E1s), E2s)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/2198.html