产生随机密码的函数

      脚本笔记 2005-4-6 15:46
由系统随机产生的密码通过EMAIL发给注册用户,可以提高安全性,并且过滤那些无效用户.
1.
<%
response.write makePassword(16)

function makePassword(byVal maxLen)

Dim strNewPass
Dim whatsNext, upper, lower, intCounter
Randomize

For intCounter = 1 To maxLen
  whatsNext = Int((1 - 0 + 1) * Rnd + 0)
If whatsNext = 0 Then
‘character
    upper = 90
    lower = 65
Else
    upper = 57
    lower = 48
End If
    strNewPass = strNewPass & Chr(Int((upper - lower + 1) * Rnd + lower))
Next
    makePassword = strNewPass

end function
%> 
2.
把下面的代码保存为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)

%>



<%

'产生一个8位的密码
StrRandomize CStr(Now) & CStr(Rnd)
response.write GeneratePassword(8)

%>



<%
'产生一个10位的密码
StrRandomize CStr(Now) & CStr(Rnd)
response.write GeneratePassword(10)
%>



<%

'产生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 "
"
next
%>
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}