您的位置:首页 > 移动开发 > 微信开发

一个生成随机密码的WPF小程序

2011-12-09 15:41 507 查看
实现环境:Visual Studio 2010

MainWindow.xaml

<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="密码生成器" Height="93" Width="229"
WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid Width="203" Height="57">
<ComboBox Height="23" HorizontalAlignment="Left" Name="comboBox1"
VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="8" />
<ComboBoxItem Content="9" />
<ComboBoxItem Content="10" />
<ComboBoxItem Content="11" />
<ComboBoxItem Content="12" />
</ComboBox>
<Button Content="生成" Height="23" HorizontalAlignment="Left"
Margin="126,0,0,0" Name="button1" VerticalAlignment="Top"
Width="75" Click="button1_Click" />
<Label Content="Label" Height="28" HorizontalAlignment="Left"
Margin="0,29,0,0" Name="label1" VerticalAlignment="Top"
Width="201" />
</Grid>
</Window>


MainWindow.xmal.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int lcc = 0;
private int ucc = 0;
private int ncc = 0;
private int scc1 = 0;
private int scc2 = 0;
private int scc3 = 0;
private int scc4 = 0;
private int srmc = 0;
public MainWindow()
{
InitializeComponent();
comboBox1.SelectedIndex = 0;
label1.Content = string.Empty;
}

private void button1_Click(object sender, RoutedEventArgs e)
{
int length = int.Parse(comboBox1.Text);
char[] key = new char[length];
int lc = -1, uc = -2, nc = -3, sc = -4;
Random lr = new Random(DateTime.Now.Millisecond);
lc = lr.Next(0, length - 1);
while (true)
{
Random ur = new Random(DateTime.Now.Millisecond);
uc = ur.Next(0, length - 1);
if (uc != lc)
break;
}
while (true)
{
Random nr = new Random(DateTime.Now.Millisecond);
nc = nr.Next(0, length - 1);
if (nc != lc && nc != uc)
break;
}
while (true)
{
Random sr = new Random(DateTime.Now.Millisecond);
sc = sr.Next(0, length - 1);
if (sc != lc && sc != uc && sc != nc)
break;
}
for (int i = 0; i < length; i++)
{
if (i == lc)
{
key[i] = generatelowercase();
}
else if (i == uc)
{
key[i] = generateupercase();
}
else if (i == nc)
{
key[i] = generateNumber();
}
else if (i == sc)
{
key[i] = generatespeificcharacter();
}
else
{
Random selector = new Random(DateTime.Now.Millisecond);
switch (selector.Next(0, 3))
{
case 0:
key[i] = generatelowercase();
break;
case 1:
key[i] = generateupercase();
break;
case 2:
key[i] = generateNumber();
break;
case 3:
key[i] = generatespeificcharacter();
break;
}
}
}
string result = new string(key);
label1.Content = result;
}

private char generatelowercase()
{
Random rm = new Random(DateTime.Now.Millisecond+lcc);
lcc++;
return (char)rm.Next(97, 122);
}

private char generateupercase()
{
Random rm = new Random(DateTime.Now.Millisecond+ucc);
ucc++;
return (char)rm.Next(65, 90);
}

private char generateNumber()
{
Random rm = new Random(DateTime.Now.Millisecond+ncc);
ncc++;
return (char)rm.Next(48, 57);
}
private char generatespeificcharacter()
{
char result = new char();
Random srm = new Random(DateTime.Now.Millisecond+srmc);
srmc++;
switch (srm.Next(0, 3))
{
case 0:
Random rm = new Random(DateTime.Now.Millisecond+scc1);
scc1++;
result = (char)rm.Next(33, 47);
break;
case 1:
Random rm1 = new Random(DateTime.Now.Millisecond+scc2);
scc2++;
result = (char)rm1.Next(58, 64);
break;
case 2:
Random rm2 = new Random(DateTime.Now.Millisecond+scc3);
scc3++;
result = (char)rm2.Next(91, 96);
break;
case 3:
Random rm3 = new Random(DateTime.Now.Millisecond+scc4);
scc4++;
result = (char)rm3.Next(123, 126);
break;
}
return result;
}
}
}
资源下载:http://download.csdn.net/detail/tx_officedev/3900485
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐