Help with Random Password Class (From VB.NET to C#.NET)

  • Thread starter Thread starter learning_csharp
  • Start date Start date
L

learning_csharp

Can someone help me convert this to c#

----------------------------------------------------------------------------
-----------

Public Class ResetPassword

'Constants associated with check boxes
Private Const PASS_UPPERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private Const PASS_LOWERS As String = "abcdefghijklmnopqrstuvwxyz"
Private Const PASS_NUMBERS As String = "0123456789"
Private Const PASS_SPECIALS As String = "~`!@#$%^&*()_+=-{[}]|;:'<,>.?"

Public Function GeneratePassword(ByVal Uppers As Boolean, ByVal Lowers
As Boolean, ByVal Numbers As Boolean, ByVal Specials As Boolean, ByVal
passwordLength As Integer) As String

Dim strCharacters As String
Dim strNewPassword As String
Dim p As Integer

If Uppers = True Then
strCharacters = strCharacters & PASS_UPPERS
End If
If Lowers = True Then
strCharacters = strCharacters & PASS_LOWERS
End If
If Numbers = True Then
strCharacters = strCharacters & PASS_NUMBERS
End If
If Specials = True Then
strCharacters = strCharacters & PASS_SPECIALS
End If

Randomize()

For p = 0 To (passwordLength - 1)
strNewPassword = strNewPassword + Mid(strCharacters,
Len(strCharacters) * Rnd() + 1, 1)
Next

GeneratePassword = strNewPassword

End Function



End Class
 
Can someone help me convert this to c#

----------------------------------------------------------------------------
-----------

Public Class ResetPassword

'Constants associated with check boxes
Private Const PASS_UPPERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private Const PASS_LOWERS As String = "abcdefghijklmnopqrstuvwxyz"
Private Const PASS_NUMBERS As String = "0123456789"
Private Const PASS_SPECIALS As String = "~`!@#$%^&*()_+=-{[}]|;:'<,>.?"

Public Function GeneratePassword(ByVal Uppers As Boolean, ByVal Lowers
As Boolean, ByVal Numbers As Boolean, ByVal Specials As Boolean, ByVal
passwordLength As Integer) As String

Dim strCharacters As String
Dim strNewPassword As String
Dim p As Integer

If Uppers = True Then
strCharacters = strCharacters & PASS_UPPERS
End If
If Lowers = True Then
strCharacters = strCharacters & PASS_LOWERS
End If
If Numbers = True Then
strCharacters = strCharacters & PASS_NUMBERS
End If
If Specials = True Then
strCharacters = strCharacters & PASS_SPECIALS
End If

Randomize()

For p = 0 To (passwordLength - 1)
strNewPassword = strNewPassword + Mid(strCharacters,
Len(strCharacters) * Rnd() + 1, 1)
Next

GeneratePassword = strNewPassword

End Function

End Class

Here is one possible solution:

--------
public class ResetPassword {
public static Random rnd = new Random();

private const string UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string LOWER = "abcdefghijklmnopqrstuvwxyz";
private const string NUMBERS = "0123456789";
private const string SYMBOLS = "~`!@#$%^&*()_+=-{[}]|;:'<,>.?";

public string GeneratePassword(bool useUpper, bool userLower,
bool userNumbers, bool useSymbols, int passwordLength) {
System.Text.StringBuilder charPool = new System.Text.StringBuilder();
System.Text.StringBuilder newPass = new System.Text.StringBuilder();

if (useUpper)
charPool.Append(UPPER);

if (userLower)
charPool.Append(LOWER);

if (userNumbers)
charPool.Append(NUMBERS);

if (useSymbols)
charPool.Append(SYMBOLS);

int max = charPool.Length;

for (int x = 0; x < passwordLength; x++) {
newPass.Append(charPool[rnd.Next(max)]);
}

return newPass.ToString();
}
 
Thanks Tim, Works Great

Tim Smelser said:
Can someone help me convert this to c#

--------------------------------------------------------------------------
--
-----------

Public Class ResetPassword

'Constants associated with check boxes
Private Const PASS_UPPERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private Const PASS_LOWERS As String = "abcdefghijklmnopqrstuvwxyz"
Private Const PASS_NUMBERS As String = "0123456789"
Private Const PASS_SPECIALS As String =
~`!@#$%^&*()_+=-{[}]|;:' said:
Public Function GeneratePassword(ByVal Uppers As Boolean, ByVal Lowers
As Boolean, ByVal Numbers As Boolean, ByVal Specials As Boolean, ByVal
passwordLength As Integer) As String

Dim strCharacters As String
Dim strNewPassword As String
Dim p As Integer

If Uppers = True Then
strCharacters = strCharacters & PASS_UPPERS
End If
If Lowers = True Then
strCharacters = strCharacters & PASS_LOWERS
End If
If Numbers = True Then
strCharacters = strCharacters & PASS_NUMBERS
End If
If Specials = True Then
strCharacters = strCharacters & PASS_SPECIALS
End If

Randomize()

For p = 0 To (passwordLength - 1)
strNewPassword = strNewPassword + Mid(strCharacters,
Len(strCharacters) * Rnd() + 1, 1)
Next

GeneratePassword = strNewPassword

End Function

End Class

Here is one possible solution:

--------
public class ResetPassword {
public static Random rnd = new Random();

private const string UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private const string LOWER = "abcdefghijklmnopqrstuvwxyz";
private const string NUMBERS = "0123456789";
private const string SYMBOLS = "~`!@#$%^&*()_+=-{[}]|;:'<,>.?";

public string GeneratePassword(bool useUpper, bool userLower,
bool userNumbers, bool useSymbols, int passwordLength) {
System.Text.StringBuilder charPool = new System.Text.StringBuilder();
System.Text.StringBuilder newPass = new System.Text.StringBuilder();

if (useUpper)
charPool.Append(UPPER);

if (userLower)
charPool.Append(LOWER);

if (userNumbers)
charPool.Append(NUMBERS);

if (useSymbols)
charPool.Append(SYMBOLS);

int max = charPool.Length;

for (int x = 0; x < passwordLength; x++) {
newPass.Append(charPool[rnd.Next(max)]);
}

return newPass.ToString();
}
 
Back
Top