Asp中随机产生用户密码的代码
程序员文章站
2023-11-17 20:54:52
随机产生用户密码(good),说明:通过随机产生密码,然后将密码email给注册用户,你可以确认用户的email填写是否正确。 说明:通过随机产生密码,然后将密...
随机产生用户密码(good),说明:通过随机产生密码,然后将密码email给注册用户,你可以确认用户的email填写是否正确。
说明:通过随机产生密码,然后将密码email给注册用户,你可以确认用户的email填写是否正确。自动产生的密码往往安全性更高,同时,你可以过滤那些无效的用户。
把下面的代码保存为random.asp文件:
<%
sub strrandomize(strseed)
dim i, nseed
nseed = clng(0)
for i = 1 to len(strseed)
nseed = nseed xor ((256 * ((i - 1) mod 4) * ascb(mid(strseed, i, 1))))
next
randomize nseed
end sub
function generatepassword(nlength)
dim i, bmadeconsonant, c, nrnd
const strdoubleconsonants = "bdfglmnpst"
const strconsonants = "bcdfghklmnpqrstv"
const strvocal = "aeiou"
generatepassword = ""
bmadeconsonant = false
for i = 0 to nlength
nrnd = rnd
if generatepassword <> "" and (bmadeconsonant <> true) and (nrnd < 0.15) then
c = mid(strdoubleconsonants, int(len(strdoubleconsonants) * rnd + 1), 1)
c = c & c
i = i + 1
bmadeconsonant = true
else
if (bmadeconsonant <> true) and (nrnd < 0.95) then
c = mid(strconsonants, int(len(strconsonants) * rnd + 1), 1)
bmadeconsonant = true
else
c = mid(strvocal,int(len(strvocal) * rnd + 1), 1)
bmadeconsonant = false
end if
end if
generatepassword = generatepassword & c
next
if len(generatepassword) > nlength then
generatepassword = left(generatepassword, nlength)
end if
end function
%>
然后在你的目标程序中这样调用上面的代码,就可以实现密码的自动生成:(仅仅是一个例子,你可以把他们粘贴到一个test.asp的文件中,然后运行test.asp)
<!--include file="random.asp" -->
<%
'产生一个六位的密码
strrandomize cstr(now) & cstr(rnd)
response.write generatepassword(6)
%>
<br><br>
<%
'产生一个8位的密码
strrandomize cstr(now) & cstr(rnd)
response.write generatepassword(8)
%>
<br><br>
<%
'产生一个10位的密码
strrandomize cstr(now) & cstr(rnd)
response.write generatepassword(10)
%>
<br><br>
<%
'产生1000个密码
dim t, t2
for t = 1 to 500
for t2 = 1 to 661
strrandomize cstr(now) & cstr(rnd)
next
strrandomize cstr(now) & cstr(rnd)
response.write generatepassword(6)
response.write "<br>"
next
%>
说明:通过随机产生密码,然后将密码email给注册用户,你可以确认用户的email填写是否正确。自动产生的密码往往安全性更高,同时,你可以过滤那些无效的用户。
把下面的代码保存为random.asp文件:
复制代码 代码如下:
<%
sub strrandomize(strseed)
dim i, nseed
nseed = clng(0)
for i = 1 to len(strseed)
nseed = nseed xor ((256 * ((i - 1) mod 4) * ascb(mid(strseed, i, 1))))
next
randomize nseed
end sub
function generatepassword(nlength)
dim i, bmadeconsonant, c, nrnd
const strdoubleconsonants = "bdfglmnpst"
const strconsonants = "bcdfghklmnpqrstv"
const strvocal = "aeiou"
generatepassword = ""
bmadeconsonant = false
for i = 0 to nlength
nrnd = rnd
if generatepassword <> "" and (bmadeconsonant <> true) and (nrnd < 0.15) then
c = mid(strdoubleconsonants, int(len(strdoubleconsonants) * rnd + 1), 1)
c = c & c
i = i + 1
bmadeconsonant = true
else
if (bmadeconsonant <> true) and (nrnd < 0.95) then
c = mid(strconsonants, int(len(strconsonants) * rnd + 1), 1)
bmadeconsonant = true
else
c = mid(strvocal,int(len(strvocal) * rnd + 1), 1)
bmadeconsonant = false
end if
end if
generatepassword = generatepassword & c
next
if len(generatepassword) > nlength then
generatepassword = left(generatepassword, nlength)
end if
end function
%>
复制代码 代码如下:
<!--include file="random.asp" -->
<%
'产生一个六位的密码
strrandomize cstr(now) & cstr(rnd)
response.write generatepassword(6)
%>
<br><br>
<%
'产生一个8位的密码
strrandomize cstr(now) & cstr(rnd)
response.write generatepassword(8)
%>
<br><br>
<%
'产生一个10位的密码
strrandomize cstr(now) & cstr(rnd)
response.write generatepassword(10)
%>
<br><br>
<%
'产生1000个密码
dim t, t2
for t = 1 to 500
for t2 = 1 to 661
strrandomize cstr(now) & cstr(rnd)
next
strrandomize cstr(now) & cstr(rnd)
response.write generatepassword(6)
response.write "<br>"
next
%>