ASP 改良版MD5、SHA256多重加密类(二次及多次)
程序员文章站
2022-04-14 19:01:19
目前可实现:md5算法、sha256算法、先md5后sha256、先sha256后md5、两次md5、两次sha256、前8位md5算法后8位sha256算法、前8位sha...
目前可实现:md5算法、sha256算法、先md5后sha256、先sha256后md5、两次md5、两次sha256、前8位md5算法后8位sha256算法、前8位sha256算法后8位md5算法、取密码最后一个字符的ascw值与8的余数作为加密方案系数、16位md5加密、32位md5加密、8位sha256加密、16位sha256加密、24位sha256加密、32位sha256加密、40位sha256加密、48位sha256加密、56位sha256加密、64位sha256加密等算法,看过源码之后,更可以自定义更多加密组合及加密算法,跟暴力破解md5算法说拜拜吧~~
cls_encrypt.asp 源码:
<%
private const bits_to_a_byte = 8
private const bytes_to_a_word = 4
private const bits_to_a_word = 32
class cls_encrypt
private md5_m_lonbits(30)
private md5_m_l2power(30)
private sha256_m_lonbits(30)
private sha256_m_l2power(30)
private sha256_k(63)
private sub class_initialize
md5_m_lonbits(0) = clng(1)
md5_m_lonbits(1) = clng(3)
md5_m_lonbits(2) = clng(7)
md5_m_lonbits(3) = clng(15)
md5_m_lonbits(4) = clng(31)
md5_m_lonbits(5) = clng(63)
md5_m_lonbits(6) = clng(127)
md5_m_lonbits(7) = clng(255)
md5_m_lonbits(8) = clng(511)
md5_m_lonbits(9) = clng(1023)
md5_m_lonbits(10) = clng(2047)
md5_m_lonbits(11) = clng(4095)
md5_m_lonbits(12) = clng(8191)
md5_m_lonbits(13) = clng(16383)
md5_m_lonbits(14) = clng(32767)
md5_m_lonbits(15) = clng(65535)
md5_m_lonbits(16) = clng(131071)
md5_m_lonbits(17) = clng(262143)
md5_m_lonbits(18) = clng(524287)
md5_m_lonbits(19) = clng(1048575)
md5_m_lonbits(20) = clng(2097151)
md5_m_lonbits(21) = clng(4194303)
md5_m_lonbits(22) = clng(8388607)
md5_m_lonbits(23) = clng(16777215)
md5_m_lonbits(24) = clng(33554431)
md5_m_lonbits(25) = clng(67108863)
md5_m_lonbits(26) = clng(134217727)
md5_m_lonbits(27) = clng(268435455)
md5_m_lonbits(28) = clng(536870911)
md5_m_lonbits(29) = clng(1073741823)
md5_m_lonbits(30) = clng(2147483647)
md5_m_l2power(0) = clng(1)
md5_m_l2power(1) = clng(2)
md5_m_l2power(2) = clng(4)
md5_m_l2power(3) = clng(8)
md5_m_l2power(4) = clng(16)
md5_m_l2power(5) = clng(32)
md5_m_l2power(6) = clng(64)
md5_m_l2power(7) = clng(128)
md5_m_l2power(8) = clng(256)
md5_m_l2power(9) = clng(512)
md5_m_l2power(10) = clng(1024)
md5_m_l2power(11) = clng(2048)
md5_m_l2power(12) = clng(4096)
md5_m_l2power(13) = clng(8192)
md5_m_l2power(14) = clng(16384)
md5_m_l2power(15) = clng(32768)
md5_m_l2power(16) = clng(65536)
md5_m_l2power(17) = clng(131072)
md5_m_l2power(18) = clng(262144)
md5_m_l2power(19) = clng(524288)
md5_m_l2power(20) = clng(1048576)
md5_m_l2power(21) = clng(2097152)
md5_m_l2power(22) = clng(4194304)
md5_m_l2power(23) = clng(8388608)
md5_m_l2power(24) = clng(16777216)
md5_m_l2power(25) = clng(33554432)
md5_m_l2power(26) = clng(67108864)
md5_m_l2power(27) = clng(134217728)
md5_m_l2power(28) = clng(268435456)
md5_m_l2power(29) = clng(536870912)
md5_m_l2power(30) = clng(1073741824)
sha256_m_lonbits(0) = clng(1)
sha256_m_lonbits(1) = clng(3)
sha256_m_lonbits(2) = clng(7)
sha256_m_lonbits(3) = clng(15)
sha256_m_lonbits(4) = clng(31)
sha256_m_lonbits(5) = clng(63)
sha256_m_lonbits(6) = clng(127)
sha256_m_lonbits(7) = clng(255)
sha256_m_lonbits(8) = clng(511)
sha256_m_lonbits(9) = clng(1023)
sha256_m_lonbits(10) = clng(2047)
sha256_m_lonbits(11) = clng(4095)
sha256_m_lonbits(12) = clng(8191)
sha256_m_lonbits(13) = clng(16383)
sha256_m_lonbits(14) = clng(32767)
sha256_m_lonbits(15) = clng(65535)
sha256_m_lonbits(16) = clng(131071)
sha256_m_lonbits(17) = clng(262143)
sha256_m_lonbits(18) = clng(524287)
sha256_m_lonbits(19) = clng(1048575)
sha256_m_lonbits(20) = clng(2097151)
sha256_m_lonbits(21) = clng(4194303)
sha256_m_lonbits(22) = clng(8388607)
sha256_m_lonbits(23) = clng(16777215)
sha256_m_lonbits(24) = clng(33554431)
sha256_m_lonbits(25) = clng(67108863)
sha256_m_lonbits(26) = clng(134217727)
sha256_m_lonbits(27) = clng(268435455)
sha256_m_lonbits(28) = clng(536870911)
sha256_m_lonbits(29) = clng(1073741823)
sha256_m_lonbits(30) = clng(2147483647)
sha256_m_l2power(0) = clng(1)
sha256_m_l2power(1) = clng(2)
sha256_m_l2power(2) = clng(4)
sha256_m_l2power(3) = clng(8)
sha256_m_l2power(4) = clng(16)
sha256_m_l2power(5) = clng(32)
sha256_m_l2power(6) = clng(64)
sha256_m_l2power(7) = clng(128)
sha256_m_l2power(8) = clng(256)
sha256_m_l2power(9) = clng(512)
sha256_m_l2power(10) = clng(1024)
sha256_m_l2power(11) = clng(2048)
sha256_m_l2power(12) = clng(4096)
sha256_m_l2power(13) = clng(8192)
sha256_m_l2power(14) = clng(16384)
sha256_m_l2power(15) = clng(32768)
sha256_m_l2power(16) = clng(65536)
sha256_m_l2power(17) = clng(131072)
sha256_m_l2power(18) = clng(262144)
sha256_m_l2power(19) = clng(524288)
sha256_m_l2power(20) = clng(1048576)
sha256_m_l2power(21) = clng(2097152)
sha256_m_l2power(22) = clng(4194304)
sha256_m_l2power(23) = clng(8388608)
sha256_m_l2power(24) = clng(16777216)
sha256_m_l2power(25) = clng(33554432)
sha256_m_l2power(26) = clng(67108864)
sha256_m_l2power(27) = clng(134217728)
sha256_m_l2power(28) = clng(268435456)
sha256_m_l2power(29) = clng(536870912)
sha256_m_l2power(30) = clng(1073741824)
sha256_k(0) = &h428a2f98
sha256_k(1) = &h71374491
sha256_k(2) = &hb5c0fbcf
sha256_k(3) = &he9b5dba5
sha256_k(4) = &h3956c25b
sha256_k(5) = &h59f111f1
sha256_k(6) = &h923f82a4
sha256_k(7) = &hab1c5ed5
sha256_k(8) = &hd807aa98
sha256_k(9) = &h12835b01
sha256_k(10) = &h243185be
sha256_k(11) = &h550c7dc3
sha256_k(12) = &h72be5d74
sha256_k(13) = &h80deb1fe
sha256_k(14) = &h9bdc06a7
sha256_k(15) = &hc19bf174
sha256_k(16) = &he49b69c1
sha256_k(17) = &hefbe4786
sha256_k(18) = &hfc19dc6
sha256_k(19) = &h240ca1cc
sha256_k(20) = &h2de92c6f
sha256_k(21) = &h4a7484aa
sha256_k(22) = &h5cb0a9dc
sha256_k(23) = &h76f988da
sha256_k(24) = &h983e5152
sha256_k(25) = &ha831c66d
sha256_k(26) = &hb00327c8
sha256_k(27) = &hbf597fc7
sha256_k(28) = &hc6e00bf3
sha256_k(29) = &hd5a79147
sha256_k(30) = &h6ca6351
sha256_k(31) = &h14292967
sha256_k(32) = &h27b70a85
sha256_k(33) = &h2e1b2138
sha256_k(34) = &h4d2c6dfc
sha256_k(35) = &h53380d13
sha256_k(36) = &h650a7354
sha256_k(37) = &h766a0abb
sha256_k(38) = &h81c2c92e
sha256_k(39) = &h92722c85
sha256_k(40) = &ha2bfe8a1
sha256_k(41) = &ha81a664b
sha256_k(42) = &hc24b8b70
sha256_k(43) = &hc76c51a3
sha256_k(44) = &hd192e819
sha256_k(45) = &hd6990624
sha256_k(46) = &hf40e3585
sha256_k(47) = &h106aa070
sha256_k(48) = &h19a4c116
sha256_k(49) = &h1e376c08
sha256_k(50) = &h2748774c
sha256_k(51) = &h34b0bcb5
sha256_k(52) = &h391c0cb3
sha256_k(53) = &h4ed8aa4a
sha256_k(54) = &h5b9cca4f
sha256_k(55) = &h682e6ff3
sha256_k(56) = &h748f82ee
sha256_k(57) = &h78a5636f
sha256_k(58) = &h84c87814
sha256_k(59) = &h8cc70208
sha256_k(60) = &h90befffa
sha256_k(61) = &ha4506ceb
sha256_k(62) = &hbef9a3f7
sha256_k(63) = &hc67178f2
end sub
private sub class_terminate
end sub
'字符传转换数组函数
private function converttowordarray(byval smessage)
dim lmessagelength
dim lnumberofwords
dim lwordarray()
dim lbyteposition
dim lbytecount
dim lwordcount
const modulus_bits = 512
const congruent_bits = 448
lmessagelength = len(smessage)
lnumberofwords = (((lmessagelength + ((modulus_bits - congruent_bits) \ bits_to_a_byte)) \ (modulus_bits \ bits_to_a_byte)) + 1) * (modulus_bits \ bits_to_a_word)
redim lwordarray(lnumberofwords - 1)
lbyteposition = 0
lbytecount = 0
do until lbytecount >= lmessagelength
lwordcount = lbytecount \ bytes_to_a_word
lbyteposition = (lbytecount mod bytes_to_a_word) * bits_to_a_byte
lwordarray(lwordcount) = lwordarray(lwordcount) or lshift(asc(mid(smessage, lbytecount + 1, 1)), lbyteposition)
lbytecount = lbytecount + 1
loop
lwordcount = lbytecount \ bytes_to_a_word
lbyteposition = (lbytecount mod bytes_to_a_word) * bits_to_a_byte
lwordarray(lwordcount) = lwordarray(lwordcount) or lshift(&h80, lbyteposition)
lwordarray(lnumberofwords - 2) = lshift(lmessagelength, 3)
lwordarray(lnumberofwords - 1) = rshift(lmessagelength, 29)
converttowordarray = lwordarray
end function
'字符串左移位主函数
private function lshift(byval lvalue, byval ishiftbits)
if ishiftbits = 0 then
lshift = lvalue
exit function
elseif ishiftbits = 31 then
if lvalue and 1 then
lshift = &h80000000
else
lshift = 0
end if
exit function
elseif ishiftbits < 0 or ishiftbits > 31 then
err.raise 6
end if
if (lvalue and md5_m_l2power(31 - ishiftbits)) then
lshift = ((lvalue and md5_m_lonbits(31 - (ishiftbits + 1))) * md5_m_l2power(ishiftbits)) or &h80000000
else
lshift = ((lvalue and md5_m_lonbits(31 - ishiftbits)) * md5_m_l2power(ishiftbits))
end if
end function
'字符串右移位主函数
private function rshift(byval lvalue, byval ishiftbits)
if ishiftbits = 0 then
rshift = lvalue
exit function
elseif ishiftbits = 31 then
if lvalue and &h80000000 then
rshift = 1
else
rshift = 0
end if
exit function
elseif ishiftbits < 0 or ishiftbits > 31 then
err.raise 6
end if
rshift = (lvalue and &h7ffffffe) \ md5_m_l2power(ishiftbits)
if (lvalue and &h80000000) then
rshift = (rshift or (&h40000000 \ md5_m_l2power(ishiftbits - 1)))
end if
end function
'字符串偏移转换,通过左右移位函数实现
private function rotateleft(byval lvalue, byval ishiftbits)
rotateleft = lshift(lvalue, ishiftbits) or rshift(lvalue, (32 - ishiftbits))
end function
'追加无符号16进制编码
private function addunsigned(byval lx, byval ly)
dim lx4
dim ly4
dim lx8
dim ly8
dim lresult
lx8 = lx and &h80000000
ly8 = ly and &h80000000
lx4 = lx and &h40000000
ly4 = ly and &h40000000
lresult = (lx and &h3fffffff) + (ly and &h3fffffff)
if lx4 and ly4 then
lresult = lresult xor &h80000000 xor lx8 xor ly8
elseif lx4 or ly4 then
if lresult and &h40000000 then
lresult = lresult xor &hc0000000 xor lx8 xor ly8
else
lresult = lresult xor &h40000000 xor lx8 xor ly8
end if
else
lresult = lresult xor lx8 xor ly8
end if
addunsigned = lresult
end function
'md5_ff,md5_gg,md5_hh,md5_ii md5主编码函数
private sub md5_ff(a, b, c, d, x, s, ac)
a = addunsigned(a, addunsigned(addunsigned(md5_f(b, c, d), x), ac))
a = rotateleft(a, s)
a = addunsigned(a, b)
end sub
private sub md5_gg(a, b, c, d, x, s, ac)
a = addunsigned(a, addunsigned(addunsigned(md5_g(b, c, d), x), ac))
a = rotateleft(a, s)
a = addunsigned(a, b)
end sub
private sub md5_hh(a, b, c, d, x, s, ac)
a = addunsigned(a, addunsigned(addunsigned(md5_h(b, c, d), x), ac))
a = rotateleft(a, s)
a = addunsigned(a, b)
end sub
private sub md5_ii(a, b, c, d, x, s, ac)
a = addunsigned(a, addunsigned(addunsigned(md5_i(b, c, d), x), ac))
a = rotateleft(a, s)
a = addunsigned(a, b)
end sub
'md5_f,md5_g,md5_h,md5_i md5辅编码函数
private function md5_f(x, y, z)
md5_f = (x and y) or ((not x) and z)
end function
private function md5_g(x, y, z)
md5_g = (x and z) or (y and (not z))
end function
private function md5_h(x, y, z)
md5_h = (x xor y xor z)
end function
private function md5_i(x, y, z)
md5_i = (y xor (x or (not z)))
end function
'16进制编码转换
private function wordtohex(byval lvalue)
dim lbyte
dim lcount
for lcount = 0 to 3
lbyte = rshift(lvalue, lcount * bits_to_a_byte) and md5_m_lonbits(bits_to_a_byte - 1)
wordtohex = wordtohex & right("0" & hex(lbyte), 2)
next
end function
'md5主函数 md5(字符串,编码位)
public function md5(byval smessage, byval stype)
dim x
dim k
dim aa
dim bb
dim cc
dim dd
dim a
dim b
dim c
dim d
const s11 = 7
const s12 = 12
const s13 = 17
const s14 = 22
const s21 = 5
const s22 = 9
const s23 = 14
const s24 = 20
const s31 = 4
const s32 = 11
const s33 = 16
const s34 = 23
const s41 = 6
const s42 = 10
const s43 = 15
const s44 = 21
x = converttowordarray(smessage) '加密步骤1:数组转换
a = &h67452301
b = &hefcdab89
c = &h98badcfe
d = &h10325476
for k = 0 to ubound(x) step 16
aa = a
bb = b
cc = c
dd = d
'加密步骤2:编码转换
md5_ff a, b, c, d, x(k + 0), s11, &hd76aa478
md5_ff d, a, b, c, x(k + 1), s12, &he8c7b756
md5_ff c, d, a, b, x(k + 2), s13, &h242070db
md5_ff b, c, d, a, x(k + 3), s14, &hc1bdceee
md5_ff a, b, c, d, x(k + 4), s11, &hf57c0faf
md5_ff d, a, b, c, x(k + 5), s12, &h4787c62a
md5_ff c, d, a, b, x(k + 6), s13, &ha8304613
md5_ff b, c, d, a, x(k + 7), s14, &hfd469501
md5_ff a, b, c, d, x(k + 8), s11, &h698098d8
md5_ff d, a, b, c, x(k + 9), s12, &h8b44f7af
md5_ff c, d, a, b, x(k + 10), s13, &hffff5bb1
md5_ff b, c, d, a, x(k + 11), s14, &h895cd7be
md5_ff a, b, c, d, x(k + 12), s11, &h6b901122
md5_ff d, a, b, c, x(k + 13), s12, &hfd987193
md5_ff c, d, a, b, x(k + 14), s13, &ha679438e
md5_ff b, c, d, a, x(k + 15), s14, &h49b40821
md5_gg a, b, c, d, x(k + 1), s21, &hf61e2562
md5_gg d, a, b, c, x(k + 6), s22, &hc040b340
md5_gg c, d, a, b, x(k + 11), s23, &h265e5a51
md5_gg b, c, d, a, x(k + 0), s24, &he9b6c7aa
md5_gg a, b, c, d, x(k + 5), s21, &hd62f105d
md5_gg d, a, b, c, x(k + 10), s22, &h2441453
md5_gg c, d, a, b, x(k + 15), s23, &hd8a1e681
md5_gg b, c, d, a, x(k + 4), s24, &he7d3fbc8
md5_gg a, b, c, d, x(k + 9), s21, &h21e1cde6
md5_gg d, a, b, c, x(k + 14), s22, &hc33707d6
md5_gg c, d, a, b, x(k + 3), s23, &hf4d50d87
md5_gg b, c, d, a, x(k + 8), s24, &h455a14ed
md5_gg a, b, c, d, x(k + 13), s21, &ha9e3e905
md5_gg d, a, b, c, x(k + 2), s22, &hfcefa3f8
md5_gg c, d, a, b, x(k + 7), s23, &h676f02d9
md5_gg b, c, d, a, x(k + 12), s24, &h8d2a4c8a
md5_hh a, b, c, d, x(k + 5), s31, &hfffa3942
md5_hh d, a, b, c, x(k + 8), s32, &h8771f681
md5_hh c, d, a, b, x(k + 11), s33, &h6d9d6122
md5_hh b, c, d, a, x(k + 14), s34, &hfde5380c
md5_hh a, b, c, d, x(k + 1), s31, &ha4beea44
md5_hh d, a, b, c, x(k + 4), s32, &h4bdecfa9
md5_hh c, d, a, b, x(k + 7), s33, &hf6bb4b60
md5_hh b, c, d, a, x(k + 10), s34, &hbebfbc70
md5_hh a, b, c, d, x(k + 13), s31, &h289b7ec6
md5_hh d, a, b, c, x(k + 0), s32, &heaa127fa
md5_hh c, d, a, b, x(k + 3), s33, &hd4ef3085
md5_hh b, c, d, a, x(k + 6), s34, &h4881d05
md5_hh a, b, c, d, x(k + 9), s31, &hd9d4d039
md5_hh d, a, b, c, x(k + 12), s32, &he6db99e5
md5_hh c, d, a, b, x(k + 15), s33, &h1fa27cf8
md5_hh b, c, d, a, x(k + 2), s34, &hc4ac5665
md5_ii a, b, c, d, x(k + 0), s41, &hf4292244
md5_ii d, a, b, c, x(k + 7), s42, &h432aff97
md5_ii c, d, a, b, x(k + 14), s43, &hab9423a7
md5_ii b, c, d, a, x(k + 5), s44, &hfc93a039
md5_ii a, b, c, d, x(k + 12), s41, &h655b59c3
md5_ii d, a, b, c, x(k + 3), s42, &h8f0ccc92
md5_ii c, d, a, b, x(k + 10), s43, &hffeff47d
md5_ii b, c, d, a, x(k + 1), s44, &h85845dd1
md5_ii a, b, c, d, x(k + 8), s41, &h6fa87e4f
md5_ii d, a, b, c, x(k + 15), s42, &hfe2ce6e0
md5_ii c, d, a, b, x(k + 6), s43, &ha3014314
md5_ii b, c, d, a, x(k + 13), s44, &h4e0811a1
md5_ii a, b, c, d, x(k + 4), s41, &hf7537e82
md5_ii d, a, b, c, x(k + 11), s42, &hbd3af235
md5_ii c, d, a, b, x(k + 2), s43, &h2ad7d2bb
md5_ii b, c, d, a, x(k + 9), s44, &heb86d391
'加密步骤3:追加无符号转换
a = addunsigned(a, aa)
b = addunsigned(b, bb)
c = addunsigned(c, cc)
d = addunsigned(d, dd)
next
if stype = 32 then
'加密步骤4:分割合并各8位16进制转换
md5 = lcase(wordtohex(a) & wordtohex(b) & wordtohex(c) & wordtohex(d))
else
md5 = lcase(wordtohex(b) & wordtohex(c))
end if
end function
private function sha256_lshift(byval lvalue, byval ishiftbits)
if ishiftbits = 0 then
sha256_lshift = lvalue
exit function
elseif ishiftbits = 31 then
if lvalue and 1 then
sha256_lshift = &h80000000
else
sha256_lshift = 0
end if
exit function
elseif ishiftbits < 0 or ishiftbits > 31 then
err.raise 6
end if
if (lvalue and sha256_m_l2power(31 - ishiftbits)) then
sha256_lshift = ((lvalue and sha256_m_lonbits(31 - (ishiftbits + 1))) * sha256_m_l2power(ishiftbits)) or &h80000000
else
sha256_lshift = ((lvalue and sha256_m_lonbits(31 - ishiftbits)) * sha256_m_l2power(ishiftbits))
end if
end function
private function sha256_rshift(byval lvalue, byval ishiftbits)
if ishiftbits = 0 then
sha256_rshift = lvalue
exit function
elseif ishiftbits = 31 then
if lvalue and &h80000000 then
sha256_rshift = 1
else
sha256_rshift = 0
end if
exit function
elseif ishiftbits < 0 or ishiftbits > 31 then
err.raise 6
end if
sha256_rshift = (lvalue and &h7ffffffe) \ sha256_m_l2power(ishiftbits)
if (lvalue and &h80000000) then
sha256_rshift = (sha256_rshift or (&h40000000 \ sha256_m_l2power(ishiftbits - 1)))
end if
end function
private function sha256_addunsigned(byval lx, byval ly)
dim lx4
dim ly4
dim lx8
dim ly8
dim lresult
lx8 = lx and &h80000000
ly8 = ly and &h80000000
lx4 = lx and &h40000000
ly4 = ly and &h40000000
lresult = (lx and &h3fffffff) + (ly and &h3fffffff)
if lx4 and ly4 then
lresult = lresult xor &h80000000 xor lx8 xor ly8
elseif lx4 or ly4 then
if lresult and &h40000000 then
lresult = lresult xor &hc0000000 xor lx8 xor ly8
else
lresult = lresult xor &h40000000 xor lx8 xor ly8
end if
else
lresult = lresult xor lx8 xor ly8
end if
sha256_addunsigned = lresult
end function
private function sha256_ch(x, y, z)
sha256_ch = ((x and y) xor ((not x) and z))
end function
private function sha256_maj(x, y, z)
sha256_maj = ((x and y) xor (x and z) xor (y and z))
end function
private function sha256_s(x, n)
sha256_s = (sha256_rshift(x, (n and sha256_m_lonbits(4))) or sha256_lshift(x, (32 - (n and sha256_m_lonbits(4)))))
end function
private function sha256_r(x, n)
sha256_r = sha256_rshift(x, clng(n and sha256_m_lonbits(4)))
end function
private function sha256_sigma0(x)
sha256_sigma0 = (sha256_s(x, 2) xor sha256_s(x, 13) xor sha256_s(x, 22))
end function
private function sha256_sigma1(x)
sha256_sigma1 = (sha256_s(x, 6) xor sha256_s(x, 11) xor sha256_s(x, 25))
end function
private function sha256_gamma0(x)
sha256_gamma0 = (sha256_s(x, 7) xor sha256_s(x, 18) xor sha256_r(x, 3))
end function
private function sha256_gamma1(x)
sha256_gamma1 = (sha256_s(x, 17) xor sha256_s(x, 19) xor sha256_r(x, 10))
end function
private function sha256_converttowordarray(byval smessage)
dim lmessagelength
dim lnumberofwords
dim lwordarray()
dim lbyteposition
dim lbytecount
dim lwordcount
dim lbyte
const modulus_bits = 512
const congruent_bits = 448
lmessagelength = len(smessage)
lnumberofwords = (((lmessagelength + ((modulus_bits - congruent_bits) \ bits_to_a_byte)) \ (modulus_bits \ bits_to_a_byte)) + 1) * (modulus_bits \ bits_to_a_word)
redim lwordarray(lnumberofwords - 1)
lbyteposition = 0
lbytecount = 0
do until lbytecount >= lmessagelength
lwordcount = lbytecount \ bytes_to_a_word
lbyteposition = (3 - (lbytecount mod bytes_to_a_word)) * bits_to_a_byte
lbyte = ascb(mid(smessage, lbytecount + 1, 1))
lwordarray(lwordcount) = lwordarray(lwordcount) or sha256_lshift(lbyte, lbyteposition)
lbytecount = lbytecount + 1
loop
lwordcount = lbytecount \ bytes_to_a_word
lbyteposition = (3 - (lbytecount mod bytes_to_a_word)) * bits_to_a_byte
lwordarray(lwordcount) = lwordarray(lwordcount) or sha256_lshift(&h80, lbyteposition)
lwordarray(lnumberofwords - 1) = sha256_lshift(lmessagelength, 3)
lwordarray(lnumberofwords - 2) = sha256_rshift(lmessagelength, 29)
sha256_converttowordarray = lwordarray
end function
' ==========================================================================================
' sha256 加密算法
' ========================================================================================
' 调用方法:
' sha256(smessage, p_len)
' p_len控制生成字符串长度,1为8位,2为16位,最大取值为8,即加密后长度为64位
' 控制 8 位\16 位\24 位\32 位\40 位\48 位\56 位\64 位加密
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)
case 5 ' 两次sha256
encryptmode = sha256(sha256(p_string, e1s), e2s)
case 6 ' 前8位 md5 算法 后8位 sha256 算法
encryptmode = left(md5(p_string, e1s), 8) & right(sha256(p_string, e2s), 8)
case 7 ' 前8位 sha256 算法 后8位 md5 算法
encryptmode = left(sha256(p_string, e2s), 8) & right(md5(p_string, e1s), 8)
end select
end function
' ============================================
' 全站密码类通用加密函数
' ============================================
public function passwordent(byval p_string)
if p_string = "" or isnull(p_string) then p_string = 1
' 取密码最后一个字符的ascw值与8的余数作为加密方案系数
passwordent = encryptmode(p_string, ascw(lcase(right(p_string, 1))) mod 8)
end function
end class
%>
演示:
<!--#include file="cls_encrypt.asp"-->
<%
dim o
set o = new cls_encrypt
response.write "md5算法:" & o.encryptmode("admin",0) & "<br />"
response.write "sha256算法:" & o.encryptmode("admin",1) & "<br />"
response.write "先md5后sha256:" & o.encryptmode("admin",2) & "<br />"
response.write "先sha256后md5:" & o.encryptmode("admin",3) & "<br />"
response.write "两次md5:" & o.encryptmode("admin",4) & "<br />"
response.write "两次sha256:" & o.encryptmode("admin",5) & "<br />"
response.write "前8位md5算法后8位sha256算法:" & o.encryptmode("admin",6) & "<br />"
response.write "前8位sha256算法后8位md5算法:" & o.encryptmode("admin",7) & "<br /><hr />"
response.write "取密码最后一个字符的ascw值与8的余数作为加密方案系数:" & o.passwordent("admin") & "<br /><hr />"
response.write "16位md5加密:" & o.md5("admin",16) & "<br />"
response.write "32位md5加密:" & o.md5("admin",32) & "<br /><hr />"
response.write "8位sha256加密:" & o.sha256("admin",1) & "<br />"
response.write "16位sha256加密:" & o.sha256("admin",2) & "<br />"
response.write "24位sha256加密:" & o.sha256("admin",3) & "<br />"
response.write "32位sha256加密:" & o.sha256("admin",4) & "<br />"
response.write "40位sha256加密:" & o.sha256("admin",5) & "<br />"
response.write "48位sha256加密:" & o.sha256("admin",6) & "<br />"
response.write "56位sha256加密:" & o.sha256("admin",7) & "<br />"
response.write "64位sha256加密:" & o.sha256("admin",8) & "<br />"
set o = nothing
%>
cls_encrypt.asp 源码:
复制代码 代码如下:
<%
private const bits_to_a_byte = 8
private const bytes_to_a_word = 4
private const bits_to_a_word = 32
class cls_encrypt
private md5_m_lonbits(30)
private md5_m_l2power(30)
private sha256_m_lonbits(30)
private sha256_m_l2power(30)
private sha256_k(63)
private sub class_initialize
md5_m_lonbits(0) = clng(1)
md5_m_lonbits(1) = clng(3)
md5_m_lonbits(2) = clng(7)
md5_m_lonbits(3) = clng(15)
md5_m_lonbits(4) = clng(31)
md5_m_lonbits(5) = clng(63)
md5_m_lonbits(6) = clng(127)
md5_m_lonbits(7) = clng(255)
md5_m_lonbits(8) = clng(511)
md5_m_lonbits(9) = clng(1023)
md5_m_lonbits(10) = clng(2047)
md5_m_lonbits(11) = clng(4095)
md5_m_lonbits(12) = clng(8191)
md5_m_lonbits(13) = clng(16383)
md5_m_lonbits(14) = clng(32767)
md5_m_lonbits(15) = clng(65535)
md5_m_lonbits(16) = clng(131071)
md5_m_lonbits(17) = clng(262143)
md5_m_lonbits(18) = clng(524287)
md5_m_lonbits(19) = clng(1048575)
md5_m_lonbits(20) = clng(2097151)
md5_m_lonbits(21) = clng(4194303)
md5_m_lonbits(22) = clng(8388607)
md5_m_lonbits(23) = clng(16777215)
md5_m_lonbits(24) = clng(33554431)
md5_m_lonbits(25) = clng(67108863)
md5_m_lonbits(26) = clng(134217727)
md5_m_lonbits(27) = clng(268435455)
md5_m_lonbits(28) = clng(536870911)
md5_m_lonbits(29) = clng(1073741823)
md5_m_lonbits(30) = clng(2147483647)
md5_m_l2power(0) = clng(1)
md5_m_l2power(1) = clng(2)
md5_m_l2power(2) = clng(4)
md5_m_l2power(3) = clng(8)
md5_m_l2power(4) = clng(16)
md5_m_l2power(5) = clng(32)
md5_m_l2power(6) = clng(64)
md5_m_l2power(7) = clng(128)
md5_m_l2power(8) = clng(256)
md5_m_l2power(9) = clng(512)
md5_m_l2power(10) = clng(1024)
md5_m_l2power(11) = clng(2048)
md5_m_l2power(12) = clng(4096)
md5_m_l2power(13) = clng(8192)
md5_m_l2power(14) = clng(16384)
md5_m_l2power(15) = clng(32768)
md5_m_l2power(16) = clng(65536)
md5_m_l2power(17) = clng(131072)
md5_m_l2power(18) = clng(262144)
md5_m_l2power(19) = clng(524288)
md5_m_l2power(20) = clng(1048576)
md5_m_l2power(21) = clng(2097152)
md5_m_l2power(22) = clng(4194304)
md5_m_l2power(23) = clng(8388608)
md5_m_l2power(24) = clng(16777216)
md5_m_l2power(25) = clng(33554432)
md5_m_l2power(26) = clng(67108864)
md5_m_l2power(27) = clng(134217728)
md5_m_l2power(28) = clng(268435456)
md5_m_l2power(29) = clng(536870912)
md5_m_l2power(30) = clng(1073741824)
sha256_m_lonbits(0) = clng(1)
sha256_m_lonbits(1) = clng(3)
sha256_m_lonbits(2) = clng(7)
sha256_m_lonbits(3) = clng(15)
sha256_m_lonbits(4) = clng(31)
sha256_m_lonbits(5) = clng(63)
sha256_m_lonbits(6) = clng(127)
sha256_m_lonbits(7) = clng(255)
sha256_m_lonbits(8) = clng(511)
sha256_m_lonbits(9) = clng(1023)
sha256_m_lonbits(10) = clng(2047)
sha256_m_lonbits(11) = clng(4095)
sha256_m_lonbits(12) = clng(8191)
sha256_m_lonbits(13) = clng(16383)
sha256_m_lonbits(14) = clng(32767)
sha256_m_lonbits(15) = clng(65535)
sha256_m_lonbits(16) = clng(131071)
sha256_m_lonbits(17) = clng(262143)
sha256_m_lonbits(18) = clng(524287)
sha256_m_lonbits(19) = clng(1048575)
sha256_m_lonbits(20) = clng(2097151)
sha256_m_lonbits(21) = clng(4194303)
sha256_m_lonbits(22) = clng(8388607)
sha256_m_lonbits(23) = clng(16777215)
sha256_m_lonbits(24) = clng(33554431)
sha256_m_lonbits(25) = clng(67108863)
sha256_m_lonbits(26) = clng(134217727)
sha256_m_lonbits(27) = clng(268435455)
sha256_m_lonbits(28) = clng(536870911)
sha256_m_lonbits(29) = clng(1073741823)
sha256_m_lonbits(30) = clng(2147483647)
sha256_m_l2power(0) = clng(1)
sha256_m_l2power(1) = clng(2)
sha256_m_l2power(2) = clng(4)
sha256_m_l2power(3) = clng(8)
sha256_m_l2power(4) = clng(16)
sha256_m_l2power(5) = clng(32)
sha256_m_l2power(6) = clng(64)
sha256_m_l2power(7) = clng(128)
sha256_m_l2power(8) = clng(256)
sha256_m_l2power(9) = clng(512)
sha256_m_l2power(10) = clng(1024)
sha256_m_l2power(11) = clng(2048)
sha256_m_l2power(12) = clng(4096)
sha256_m_l2power(13) = clng(8192)
sha256_m_l2power(14) = clng(16384)
sha256_m_l2power(15) = clng(32768)
sha256_m_l2power(16) = clng(65536)
sha256_m_l2power(17) = clng(131072)
sha256_m_l2power(18) = clng(262144)
sha256_m_l2power(19) = clng(524288)
sha256_m_l2power(20) = clng(1048576)
sha256_m_l2power(21) = clng(2097152)
sha256_m_l2power(22) = clng(4194304)
sha256_m_l2power(23) = clng(8388608)
sha256_m_l2power(24) = clng(16777216)
sha256_m_l2power(25) = clng(33554432)
sha256_m_l2power(26) = clng(67108864)
sha256_m_l2power(27) = clng(134217728)
sha256_m_l2power(28) = clng(268435456)
sha256_m_l2power(29) = clng(536870912)
sha256_m_l2power(30) = clng(1073741824)
sha256_k(0) = &h428a2f98
sha256_k(1) = &h71374491
sha256_k(2) = &hb5c0fbcf
sha256_k(3) = &he9b5dba5
sha256_k(4) = &h3956c25b
sha256_k(5) = &h59f111f1
sha256_k(6) = &h923f82a4
sha256_k(7) = &hab1c5ed5
sha256_k(8) = &hd807aa98
sha256_k(9) = &h12835b01
sha256_k(10) = &h243185be
sha256_k(11) = &h550c7dc3
sha256_k(12) = &h72be5d74
sha256_k(13) = &h80deb1fe
sha256_k(14) = &h9bdc06a7
sha256_k(15) = &hc19bf174
sha256_k(16) = &he49b69c1
sha256_k(17) = &hefbe4786
sha256_k(18) = &hfc19dc6
sha256_k(19) = &h240ca1cc
sha256_k(20) = &h2de92c6f
sha256_k(21) = &h4a7484aa
sha256_k(22) = &h5cb0a9dc
sha256_k(23) = &h76f988da
sha256_k(24) = &h983e5152
sha256_k(25) = &ha831c66d
sha256_k(26) = &hb00327c8
sha256_k(27) = &hbf597fc7
sha256_k(28) = &hc6e00bf3
sha256_k(29) = &hd5a79147
sha256_k(30) = &h6ca6351
sha256_k(31) = &h14292967
sha256_k(32) = &h27b70a85
sha256_k(33) = &h2e1b2138
sha256_k(34) = &h4d2c6dfc
sha256_k(35) = &h53380d13
sha256_k(36) = &h650a7354
sha256_k(37) = &h766a0abb
sha256_k(38) = &h81c2c92e
sha256_k(39) = &h92722c85
sha256_k(40) = &ha2bfe8a1
sha256_k(41) = &ha81a664b
sha256_k(42) = &hc24b8b70
sha256_k(43) = &hc76c51a3
sha256_k(44) = &hd192e819
sha256_k(45) = &hd6990624
sha256_k(46) = &hf40e3585
sha256_k(47) = &h106aa070
sha256_k(48) = &h19a4c116
sha256_k(49) = &h1e376c08
sha256_k(50) = &h2748774c
sha256_k(51) = &h34b0bcb5
sha256_k(52) = &h391c0cb3
sha256_k(53) = &h4ed8aa4a
sha256_k(54) = &h5b9cca4f
sha256_k(55) = &h682e6ff3
sha256_k(56) = &h748f82ee
sha256_k(57) = &h78a5636f
sha256_k(58) = &h84c87814
sha256_k(59) = &h8cc70208
sha256_k(60) = &h90befffa
sha256_k(61) = &ha4506ceb
sha256_k(62) = &hbef9a3f7
sha256_k(63) = &hc67178f2
end sub
private sub class_terminate
end sub
'字符传转换数组函数
private function converttowordarray(byval smessage)
dim lmessagelength
dim lnumberofwords
dim lwordarray()
dim lbyteposition
dim lbytecount
dim lwordcount
const modulus_bits = 512
const congruent_bits = 448
lmessagelength = len(smessage)
lnumberofwords = (((lmessagelength + ((modulus_bits - congruent_bits) \ bits_to_a_byte)) \ (modulus_bits \ bits_to_a_byte)) + 1) * (modulus_bits \ bits_to_a_word)
redim lwordarray(lnumberofwords - 1)
lbyteposition = 0
lbytecount = 0
do until lbytecount >= lmessagelength
lwordcount = lbytecount \ bytes_to_a_word
lbyteposition = (lbytecount mod bytes_to_a_word) * bits_to_a_byte
lwordarray(lwordcount) = lwordarray(lwordcount) or lshift(asc(mid(smessage, lbytecount + 1, 1)), lbyteposition)
lbytecount = lbytecount + 1
loop
lwordcount = lbytecount \ bytes_to_a_word
lbyteposition = (lbytecount mod bytes_to_a_word) * bits_to_a_byte
lwordarray(lwordcount) = lwordarray(lwordcount) or lshift(&h80, lbyteposition)
lwordarray(lnumberofwords - 2) = lshift(lmessagelength, 3)
lwordarray(lnumberofwords - 1) = rshift(lmessagelength, 29)
converttowordarray = lwordarray
end function
'字符串左移位主函数
private function lshift(byval lvalue, byval ishiftbits)
if ishiftbits = 0 then
lshift = lvalue
exit function
elseif ishiftbits = 31 then
if lvalue and 1 then
lshift = &h80000000
else
lshift = 0
end if
exit function
elseif ishiftbits < 0 or ishiftbits > 31 then
err.raise 6
end if
if (lvalue and md5_m_l2power(31 - ishiftbits)) then
lshift = ((lvalue and md5_m_lonbits(31 - (ishiftbits + 1))) * md5_m_l2power(ishiftbits)) or &h80000000
else
lshift = ((lvalue and md5_m_lonbits(31 - ishiftbits)) * md5_m_l2power(ishiftbits))
end if
end function
'字符串右移位主函数
private function rshift(byval lvalue, byval ishiftbits)
if ishiftbits = 0 then
rshift = lvalue
exit function
elseif ishiftbits = 31 then
if lvalue and &h80000000 then
rshift = 1
else
rshift = 0
end if
exit function
elseif ishiftbits < 0 or ishiftbits > 31 then
err.raise 6
end if
rshift = (lvalue and &h7ffffffe) \ md5_m_l2power(ishiftbits)
if (lvalue and &h80000000) then
rshift = (rshift or (&h40000000 \ md5_m_l2power(ishiftbits - 1)))
end if
end function
'字符串偏移转换,通过左右移位函数实现
private function rotateleft(byval lvalue, byval ishiftbits)
rotateleft = lshift(lvalue, ishiftbits) or rshift(lvalue, (32 - ishiftbits))
end function
'追加无符号16进制编码
private function addunsigned(byval lx, byval ly)
dim lx4
dim ly4
dim lx8
dim ly8
dim lresult
lx8 = lx and &h80000000
ly8 = ly and &h80000000
lx4 = lx and &h40000000
ly4 = ly and &h40000000
lresult = (lx and &h3fffffff) + (ly and &h3fffffff)
if lx4 and ly4 then
lresult = lresult xor &h80000000 xor lx8 xor ly8
elseif lx4 or ly4 then
if lresult and &h40000000 then
lresult = lresult xor &hc0000000 xor lx8 xor ly8
else
lresult = lresult xor &h40000000 xor lx8 xor ly8
end if
else
lresult = lresult xor lx8 xor ly8
end if
addunsigned = lresult
end function
'md5_ff,md5_gg,md5_hh,md5_ii md5主编码函数
private sub md5_ff(a, b, c, d, x, s, ac)
a = addunsigned(a, addunsigned(addunsigned(md5_f(b, c, d), x), ac))
a = rotateleft(a, s)
a = addunsigned(a, b)
end sub
private sub md5_gg(a, b, c, d, x, s, ac)
a = addunsigned(a, addunsigned(addunsigned(md5_g(b, c, d), x), ac))
a = rotateleft(a, s)
a = addunsigned(a, b)
end sub
private sub md5_hh(a, b, c, d, x, s, ac)
a = addunsigned(a, addunsigned(addunsigned(md5_h(b, c, d), x), ac))
a = rotateleft(a, s)
a = addunsigned(a, b)
end sub
private sub md5_ii(a, b, c, d, x, s, ac)
a = addunsigned(a, addunsigned(addunsigned(md5_i(b, c, d), x), ac))
a = rotateleft(a, s)
a = addunsigned(a, b)
end sub
'md5_f,md5_g,md5_h,md5_i md5辅编码函数
private function md5_f(x, y, z)
md5_f = (x and y) or ((not x) and z)
end function
private function md5_g(x, y, z)
md5_g = (x and z) or (y and (not z))
end function
private function md5_h(x, y, z)
md5_h = (x xor y xor z)
end function
private function md5_i(x, y, z)
md5_i = (y xor (x or (not z)))
end function
'16进制编码转换
private function wordtohex(byval lvalue)
dim lbyte
dim lcount
for lcount = 0 to 3
lbyte = rshift(lvalue, lcount * bits_to_a_byte) and md5_m_lonbits(bits_to_a_byte - 1)
wordtohex = wordtohex & right("0" & hex(lbyte), 2)
next
end function
'md5主函数 md5(字符串,编码位)
public function md5(byval smessage, byval stype)
dim x
dim k
dim aa
dim bb
dim cc
dim dd
dim a
dim b
dim c
dim d
const s11 = 7
const s12 = 12
const s13 = 17
const s14 = 22
const s21 = 5
const s22 = 9
const s23 = 14
const s24 = 20
const s31 = 4
const s32 = 11
const s33 = 16
const s34 = 23
const s41 = 6
const s42 = 10
const s43 = 15
const s44 = 21
x = converttowordarray(smessage) '加密步骤1:数组转换
a = &h67452301
b = &hefcdab89
c = &h98badcfe
d = &h10325476
for k = 0 to ubound(x) step 16
aa = a
bb = b
cc = c
dd = d
'加密步骤2:编码转换
md5_ff a, b, c, d, x(k + 0), s11, &hd76aa478
md5_ff d, a, b, c, x(k + 1), s12, &he8c7b756
md5_ff c, d, a, b, x(k + 2), s13, &h242070db
md5_ff b, c, d, a, x(k + 3), s14, &hc1bdceee
md5_ff a, b, c, d, x(k + 4), s11, &hf57c0faf
md5_ff d, a, b, c, x(k + 5), s12, &h4787c62a
md5_ff c, d, a, b, x(k + 6), s13, &ha8304613
md5_ff b, c, d, a, x(k + 7), s14, &hfd469501
md5_ff a, b, c, d, x(k + 8), s11, &h698098d8
md5_ff d, a, b, c, x(k + 9), s12, &h8b44f7af
md5_ff c, d, a, b, x(k + 10), s13, &hffff5bb1
md5_ff b, c, d, a, x(k + 11), s14, &h895cd7be
md5_ff a, b, c, d, x(k + 12), s11, &h6b901122
md5_ff d, a, b, c, x(k + 13), s12, &hfd987193
md5_ff c, d, a, b, x(k + 14), s13, &ha679438e
md5_ff b, c, d, a, x(k + 15), s14, &h49b40821
md5_gg a, b, c, d, x(k + 1), s21, &hf61e2562
md5_gg d, a, b, c, x(k + 6), s22, &hc040b340
md5_gg c, d, a, b, x(k + 11), s23, &h265e5a51
md5_gg b, c, d, a, x(k + 0), s24, &he9b6c7aa
md5_gg a, b, c, d, x(k + 5), s21, &hd62f105d
md5_gg d, a, b, c, x(k + 10), s22, &h2441453
md5_gg c, d, a, b, x(k + 15), s23, &hd8a1e681
md5_gg b, c, d, a, x(k + 4), s24, &he7d3fbc8
md5_gg a, b, c, d, x(k + 9), s21, &h21e1cde6
md5_gg d, a, b, c, x(k + 14), s22, &hc33707d6
md5_gg c, d, a, b, x(k + 3), s23, &hf4d50d87
md5_gg b, c, d, a, x(k + 8), s24, &h455a14ed
md5_gg a, b, c, d, x(k + 13), s21, &ha9e3e905
md5_gg d, a, b, c, x(k + 2), s22, &hfcefa3f8
md5_gg c, d, a, b, x(k + 7), s23, &h676f02d9
md5_gg b, c, d, a, x(k + 12), s24, &h8d2a4c8a
md5_hh a, b, c, d, x(k + 5), s31, &hfffa3942
md5_hh d, a, b, c, x(k + 8), s32, &h8771f681
md5_hh c, d, a, b, x(k + 11), s33, &h6d9d6122
md5_hh b, c, d, a, x(k + 14), s34, &hfde5380c
md5_hh a, b, c, d, x(k + 1), s31, &ha4beea44
md5_hh d, a, b, c, x(k + 4), s32, &h4bdecfa9
md5_hh c, d, a, b, x(k + 7), s33, &hf6bb4b60
md5_hh b, c, d, a, x(k + 10), s34, &hbebfbc70
md5_hh a, b, c, d, x(k + 13), s31, &h289b7ec6
md5_hh d, a, b, c, x(k + 0), s32, &heaa127fa
md5_hh c, d, a, b, x(k + 3), s33, &hd4ef3085
md5_hh b, c, d, a, x(k + 6), s34, &h4881d05
md5_hh a, b, c, d, x(k + 9), s31, &hd9d4d039
md5_hh d, a, b, c, x(k + 12), s32, &he6db99e5
md5_hh c, d, a, b, x(k + 15), s33, &h1fa27cf8
md5_hh b, c, d, a, x(k + 2), s34, &hc4ac5665
md5_ii a, b, c, d, x(k + 0), s41, &hf4292244
md5_ii d, a, b, c, x(k + 7), s42, &h432aff97
md5_ii c, d, a, b, x(k + 14), s43, &hab9423a7
md5_ii b, c, d, a, x(k + 5), s44, &hfc93a039
md5_ii a, b, c, d, x(k + 12), s41, &h655b59c3
md5_ii d, a, b, c, x(k + 3), s42, &h8f0ccc92
md5_ii c, d, a, b, x(k + 10), s43, &hffeff47d
md5_ii b, c, d, a, x(k + 1), s44, &h85845dd1
md5_ii a, b, c, d, x(k + 8), s41, &h6fa87e4f
md5_ii d, a, b, c, x(k + 15), s42, &hfe2ce6e0
md5_ii c, d, a, b, x(k + 6), s43, &ha3014314
md5_ii b, c, d, a, x(k + 13), s44, &h4e0811a1
md5_ii a, b, c, d, x(k + 4), s41, &hf7537e82
md5_ii d, a, b, c, x(k + 11), s42, &hbd3af235
md5_ii c, d, a, b, x(k + 2), s43, &h2ad7d2bb
md5_ii b, c, d, a, x(k + 9), s44, &heb86d391
'加密步骤3:追加无符号转换
a = addunsigned(a, aa)
b = addunsigned(b, bb)
c = addunsigned(c, cc)
d = addunsigned(d, dd)
next
if stype = 32 then
'加密步骤4:分割合并各8位16进制转换
md5 = lcase(wordtohex(a) & wordtohex(b) & wordtohex(c) & wordtohex(d))
else
md5 = lcase(wordtohex(b) & wordtohex(c))
end if
end function
private function sha256_lshift(byval lvalue, byval ishiftbits)
if ishiftbits = 0 then
sha256_lshift = lvalue
exit function
elseif ishiftbits = 31 then
if lvalue and 1 then
sha256_lshift = &h80000000
else
sha256_lshift = 0
end if
exit function
elseif ishiftbits < 0 or ishiftbits > 31 then
err.raise 6
end if
if (lvalue and sha256_m_l2power(31 - ishiftbits)) then
sha256_lshift = ((lvalue and sha256_m_lonbits(31 - (ishiftbits + 1))) * sha256_m_l2power(ishiftbits)) or &h80000000
else
sha256_lshift = ((lvalue and sha256_m_lonbits(31 - ishiftbits)) * sha256_m_l2power(ishiftbits))
end if
end function
private function sha256_rshift(byval lvalue, byval ishiftbits)
if ishiftbits = 0 then
sha256_rshift = lvalue
exit function
elseif ishiftbits = 31 then
if lvalue and &h80000000 then
sha256_rshift = 1
else
sha256_rshift = 0
end if
exit function
elseif ishiftbits < 0 or ishiftbits > 31 then
err.raise 6
end if
sha256_rshift = (lvalue and &h7ffffffe) \ sha256_m_l2power(ishiftbits)
if (lvalue and &h80000000) then
sha256_rshift = (sha256_rshift or (&h40000000 \ sha256_m_l2power(ishiftbits - 1)))
end if
end function
private function sha256_addunsigned(byval lx, byval ly)
dim lx4
dim ly4
dim lx8
dim ly8
dim lresult
lx8 = lx and &h80000000
ly8 = ly and &h80000000
lx4 = lx and &h40000000
ly4 = ly and &h40000000
lresult = (lx and &h3fffffff) + (ly and &h3fffffff)
if lx4 and ly4 then
lresult = lresult xor &h80000000 xor lx8 xor ly8
elseif lx4 or ly4 then
if lresult and &h40000000 then
lresult = lresult xor &hc0000000 xor lx8 xor ly8
else
lresult = lresult xor &h40000000 xor lx8 xor ly8
end if
else
lresult = lresult xor lx8 xor ly8
end if
sha256_addunsigned = lresult
end function
private function sha256_ch(x, y, z)
sha256_ch = ((x and y) xor ((not x) and z))
end function
private function sha256_maj(x, y, z)
sha256_maj = ((x and y) xor (x and z) xor (y and z))
end function
private function sha256_s(x, n)
sha256_s = (sha256_rshift(x, (n and sha256_m_lonbits(4))) or sha256_lshift(x, (32 - (n and sha256_m_lonbits(4)))))
end function
private function sha256_r(x, n)
sha256_r = sha256_rshift(x, clng(n and sha256_m_lonbits(4)))
end function
private function sha256_sigma0(x)
sha256_sigma0 = (sha256_s(x, 2) xor sha256_s(x, 13) xor sha256_s(x, 22))
end function
private function sha256_sigma1(x)
sha256_sigma1 = (sha256_s(x, 6) xor sha256_s(x, 11) xor sha256_s(x, 25))
end function
private function sha256_gamma0(x)
sha256_gamma0 = (sha256_s(x, 7) xor sha256_s(x, 18) xor sha256_r(x, 3))
end function
private function sha256_gamma1(x)
sha256_gamma1 = (sha256_s(x, 17) xor sha256_s(x, 19) xor sha256_r(x, 10))
end function
private function sha256_converttowordarray(byval smessage)
dim lmessagelength
dim lnumberofwords
dim lwordarray()
dim lbyteposition
dim lbytecount
dim lwordcount
dim lbyte
const modulus_bits = 512
const congruent_bits = 448
lmessagelength = len(smessage)
lnumberofwords = (((lmessagelength + ((modulus_bits - congruent_bits) \ bits_to_a_byte)) \ (modulus_bits \ bits_to_a_byte)) + 1) * (modulus_bits \ bits_to_a_word)
redim lwordarray(lnumberofwords - 1)
lbyteposition = 0
lbytecount = 0
do until lbytecount >= lmessagelength
lwordcount = lbytecount \ bytes_to_a_word
lbyteposition = (3 - (lbytecount mod bytes_to_a_word)) * bits_to_a_byte
lbyte = ascb(mid(smessage, lbytecount + 1, 1))
lwordarray(lwordcount) = lwordarray(lwordcount) or sha256_lshift(lbyte, lbyteposition)
lbytecount = lbytecount + 1
loop
lwordcount = lbytecount \ bytes_to_a_word
lbyteposition = (3 - (lbytecount mod bytes_to_a_word)) * bits_to_a_byte
lwordarray(lwordcount) = lwordarray(lwordcount) or sha256_lshift(&h80, lbyteposition)
lwordarray(lnumberofwords - 1) = sha256_lshift(lmessagelength, 3)
lwordarray(lnumberofwords - 2) = sha256_rshift(lmessagelength, 29)
sha256_converttowordarray = lwordarray
end function
' ==========================================================================================
' sha256 加密算法
' ========================================================================================
' 调用方法:
' sha256(smessage, p_len)
' p_len控制生成字符串长度,1为8位,2为16位,最大取值为8,即加密后长度为64位
' 控制 8 位\16 位\24 位\32 位\40 位\48 位\56 位\64 位加密
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)
case 5 ' 两次sha256
encryptmode = sha256(sha256(p_string, e1s), e2s)
case 6 ' 前8位 md5 算法 后8位 sha256 算法
encryptmode = left(md5(p_string, e1s), 8) & right(sha256(p_string, e2s), 8)
case 7 ' 前8位 sha256 算法 后8位 md5 算法
encryptmode = left(sha256(p_string, e2s), 8) & right(md5(p_string, e1s), 8)
end select
end function
' ============================================
' 全站密码类通用加密函数
' ============================================
public function passwordent(byval p_string)
if p_string = "" or isnull(p_string) then p_string = 1
' 取密码最后一个字符的ascw值与8的余数作为加密方案系数
passwordent = encryptmode(p_string, ascw(lcase(right(p_string, 1))) mod 8)
end function
end class
%>
演示:
复制代码 代码如下:
<!--#include file="cls_encrypt.asp"-->
<%
dim o
set o = new cls_encrypt
response.write "md5算法:" & o.encryptmode("admin",0) & "<br />"
response.write "sha256算法:" & o.encryptmode("admin",1) & "<br />"
response.write "先md5后sha256:" & o.encryptmode("admin",2) & "<br />"
response.write "先sha256后md5:" & o.encryptmode("admin",3) & "<br />"
response.write "两次md5:" & o.encryptmode("admin",4) & "<br />"
response.write "两次sha256:" & o.encryptmode("admin",5) & "<br />"
response.write "前8位md5算法后8位sha256算法:" & o.encryptmode("admin",6) & "<br />"
response.write "前8位sha256算法后8位md5算法:" & o.encryptmode("admin",7) & "<br /><hr />"
response.write "取密码最后一个字符的ascw值与8的余数作为加密方案系数:" & o.passwordent("admin") & "<br /><hr />"
response.write "16位md5加密:" & o.md5("admin",16) & "<br />"
response.write "32位md5加密:" & o.md5("admin",32) & "<br /><hr />"
response.write "8位sha256加密:" & o.sha256("admin",1) & "<br />"
response.write "16位sha256加密:" & o.sha256("admin",2) & "<br />"
response.write "24位sha256加密:" & o.sha256("admin",3) & "<br />"
response.write "32位sha256加密:" & o.sha256("admin",4) & "<br />"
response.write "40位sha256加密:" & o.sha256("admin",5) & "<br />"
response.write "48位sha256加密:" & o.sha256("admin",6) & "<br />"
response.write "56位sha256加密:" & o.sha256("admin",7) & "<br />"
response.write "64位sha256加密:" & o.sha256("admin",8) & "<br />"
set o = nothing
%>