Reputation: 141
I am learning C# and im trying to create a random string(password) generator.. I feel like its fun because its not the simple Hello World application
I am doing this in a windows form application
I am trying to mix all the
public const string
and print it out to the TextBox2 in my project.. But the thing is I have no idea on how to actually mix them I was thinking about a for loop and check for every character.. Not really sure how I would do it..
Does anyone have any examples I could look over and try to understand how they are made? I saw one made with Linq here on SO but I couldnt really understand it because I am trying to involve check boxes
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace RandomCHaracterGenerator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class RandomPWDGenerator
{
public const string CapitilizedLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public const string NonCapitilizedLetters = "abcdefghijklmnopqrstuvwxyz";
public const string Numbers = "0123456789z";
public const string SpecialCharacters = "!@#$%^*()_+";
private static Random rnd = new Random();
}
static void Main()
{
StringBuilder password = new StringBuilder();
for (int i = 1; i <= 2; i++)
{
}
}
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,
int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void label1_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void topPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void generateLbl_MouseEnter(object sender, EventArgs e)
{
generatePanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#4d4d4d");
}
private void generateLbl_MouseLeave(object sender, EventArgs e)
{
generatePanel.BackColor = Color.Transparent;
}
private void generateLbl_Click(object sender, EventArgs e)
{
}
}
}
Upvotes: 0
Views: 336
Reputation: 2154
A great way to learn is to take a look on a real password generation.
You can take an open source password manager Keepass and see what's happening there.
See files KeePassLib\Cryptography\PasswordGenerator in the source from http://keepass.info/download.html
Upvotes: 0
Reputation: 19496
You can put the strings into a list if their box is checked:
List<string> charSets = new List<string>();
if (cbLowercase.Checked)
charSets.Add(NonCapitilizedLetters);
if (cbUppercase.Checked)
charSets.Add(CapitilizedLetters);
if (cbNumbers.Checked)
charSets.Add(Numbers);
if (cbSpecial.Checked)
charSets.Add(SpecialCharacters);
if (charSets.Count < 1)
// Tell them they need to check at least 1 box or whatever
int length = int.Parse(txtLength.Text);
StringBuilder sb = new StringBuilder();
while (lenth-- > 0)
{
int charSet = random.Next(charSets.Count);
int index = random.Next(charSets[charSet].Length);
sb.Append(charSets[charSet][index]);
}
string password = sb.ToString();
Random
should be avoided for creating a cryptographically strong password. Something like RNGCryptoServiceProvider should be used instead.Upvotes: 1