Random complex password

Use the following function to create a complex password. There are 4 tables with different characters used to create the password. To have at least one char from each table the password must be at least 4 char long.

  • Options:
    strPasswordLength = Password lenght
msgbox funRndPasswd(4) & vbCRLF & funRndPasswd(24)
 
Function funRndPasswd(strPasswordLength)
	'Description:	Creates random Password with values out of 4 table sets
	'Version:		1.1 OEM 2008-02-18
	'Options:		strPasswordLength = Length of Password required
	'Returns:		Password
 
	'On Error Resume Next
	Dim strCharacter(4), strCount, strCharWork, strPasswordLengthCount
	Dim strRoleCount, strRolePos, strCharacterTyp, strCharacterTypen, strPassword
	Dim strCharacterExist, strCharFor
	strCharacter(1) = "1234567890"			' Set 1 numbers
	strCharacter(2) = "qwertzuiopasdfghjkyxcvbnm"	' Set 2 lower case, avoid = l as it looks like 1
	strCharacter(3) = "QWERTZUIPASDFGHJKLYXCVBNM"	' Set 3 upper case, avoid = O and I as they look like 0 or 1
	strCharacter(4) = "+()-!$"			' Set 4 special
	strPassword = ""
	strCharacterTypen = ""
	For  strPasswordLengthCount  = 1 To strPasswordLength Step 1
		Randomize
		strCharacterTyp = (Int((Rnd * 4)+1))' 1=Number, 2=Lower, 3=Upper, 4=Special
		If strPasswordLengthCount > strPasswordLength-4 Then ' ensure complexity , from each table one
			strCharacterExist = 0
			For strCharFor = 1 To 4
				strCharacterExist = InStr(1, strCharacterTypen, strCharFor, 1)
				If strCharacterExist = 0 then
					strCharacterTyp = strCharFor
				End If
			Next
		End If
		strCharWork = strCharacter(strCharacterTyp)
		strCount = Len(strCharWork)
		Randomize	' extends the possibility to have an uniqe Password
		Randomize
		strRolePos = (Int((Rnd * strCount)+1))
		strPassword = strPassword &  Mid(strCharWork, strRolePos, 1)
		strCharacterTypen = strCharacterTypen & strCharacterTyp
	Next
	funRndPasswd = strPassword
End Function
This entry was posted in Information Technology and tagged , , . Bookmark the permalink.

Comments are closed.