Generate password using for loop

I create a password generator that generates a random number, then I converted it to a letter using ascii. Inside the for loop, I need letters to convert a string instead of a list. It works, but it just displays random letters in a list.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; class MainClass { static void Main() { int x = 1; int length; string a = "Press any key to continue"; object num; while (x == 1) { Console.WriteLine("How many Characters would you like the Password to be? (Press -1 to Stop)"); length = Convert.ToInt32(Console.ReadLine()); try { for (int i = 0; i < length; i++) { int num1 = Number(); Int32 ASCII = num1; num = (char)num1; if (length > 0) { Console.WriteLine(num); } } } catch { Console.WriteLine(a); } if (length == -1) break; } } static Random _r = new Random(); static int Number() { return _r.Next(65, 90); // decimal } } 
+6
source share
7 answers

you can try this

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PasswordSample { class Program { static void Main(string[] args) { Console.WriteLine("Generated password: {0}", GeneratePassword(12, true)); } /// <summary> /// Generate a random password /// </summary> /// <param name="pwdLenght">Password lenght</param> /// <param name="nonAlphaNumericChars">Indicates if password will include non alpha-numeric</param> /// <returns>Return a password</returns> private static String GeneratePassword(int pwdLenght, bool nonAlphaNumericChars) { // Allowed characters String allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"; if (nonAlphaNumericChars) { // Add non-alphanumeric chars allowedChars += "-&@#%!*$?_"; } char[] passwordChars = new char[pwdLenght]; Random rnd = new Random(); // Generate a random password for (int i = 0; i < pwdLenght; i++) passwordChars[i] = allowedChars[rnd.Next(0, allowedChars.Length)]; return new String(passwordChars); } } } 
+2
source
 StringBuilder sb = new StringBuilder(); for( int i = 0; i < length; i++ ) { int num1 = Number(); Int32 ASCII = num1; num = (char)num1; sb.Append( num ); } Console.WriteLine( sb.ToString() ); 

This is not how I will create a password and how I will generate random text, but it will give you a line and answer the original question.

As for how I would accomplish this task:

 System.Security.Cryptography.RNGCryptoServiceProvider _crypto = new System.Security.Cryptography.RNGCryptoServiceProvider(); byte[] bytes = new byte[8]; // this array can be larger if desired _crypto.GetBytes( bytes ); ulong randomNumber = (ulong)BitConverter.ToInt64( bytes, 0 ); // convert to a string with the encoding of your choice; I prefer Base 62 

For completeness, the Base62 algorithm that I use is used here. Base62 has an advantage over the more commonly used Base64 in that it does not contain any special characters, so it is easy to use in query strings, HTML and JavaScript (with a few minor caveats). Of course, passwords should not be used in any of these places, and you may want to include special characters to make the password more complex.

No matter how I convert random numbers to Base62.

 private static readonly char[] _base62Characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); public static string ToBase62String( long value ) { if( value < 0L ) { throw new ArgumentException( "Number must be zero or greater." ); } if( value == 0 ) { return "0"; } string retVal = ""; while( value > 0 ) { retVal = _base62Characters[value % 62] + retVal; value = value / 62; } return retVal; } 

Finally, I want to point out that passwords are very rarely created for any purpose, because this means that they are distributed in one form or another. Passwords must be hashed and salted; password reset should be based on random, expired security tokens, allowing the user to reset at once. Passwords should never be emailed to the user; passwords should never be stored in clear text or in any reversible format.

To generate a password token, the reset code that I provided may work well, because it creates a large cryptographically random number encoded in an Internet-safe format. But even a hashed GUID could do the trick in this case.

+16
source
 var sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.Append((char)Number()); } string password = sb.ToString(); Console.WriteLine(password ); 

But I would change my Number () method to:

 private static char GetRandomChar() { return (char)_r.Next(65, 90); } 

and then replace the line inside the loop:

 sb.Append(GetRandomChar()); 
+6
source
 //You have to append the values generated by the RandomNumber in to your password variable class MainClass { static void Main() { int x = 1; int length; string a = "Press any key to continue"; string num=string.Empty; while (x == 1) { Console.WriteLine("How many Characters would you like the Password to be? (Press -1 to Stop)"); length = Convert.ToInt32(Console.ReadLine()); try { for (int i = 0; i < length; i++) { int num1 = Number(); Int32 ASCII = num1; num =num+ ((char)num1); } Console.WriteLine(num); } catch { Console.WriteLine(a); } if (length == -1) break; } } static Random _r = new Random(); static int Number() { return _r.Next(65, 90); // decimal } } 
+3
source

just define the line next to int x ... as String Password = "";

and in the if expression add the keyword.

 if (length > 0) { Console.WriteLine(num); Password+=num } 
+1
source

I found the following message helpful when I had to create a random password generation method: fooobar.com/questions/7797 / ...

However, when I wanted to create a short code for the verification style, which would be sent to the user by email to confirm their data, I would like to limit the verification code to only 8 characters.

For this, I used the following:

string validationCoce = Guid.NewGuid (). ToString (). Substring (0, 8);

This will be stored in a database in which the email of users has also been stored, encrypted before storage for security purposes.

To verify a user account, both an email address and a verification code were required.

Hope any of the above help?

Regards, Wayne

0
source

use Console.Write () instead of Console.WriteLine (), otherwise we attach to some line and print outside the loop

-1
source

Source: https://habr.com/ru/post/903871/


All Articles