PDA

View Full Version : Is this code ASP?


marhanen
06-21-2006, 07:17 PM
OK i have this code but i'm not sure if its asp.net all he said was that it runs in .net and if it is how would i be able to run it.


using System;
using System.Collections;
using System.Security.Cryptography;
using System.Text;

public class PasswordGenerator
{
private const int DefaultMaximum = 25;
private const int DefaultMinimum = 25;
private string exclusionSet;
private int maxSize;
private int minSize;
private char[] pwdCharArray = "BCDFGHJKLMNPQRSTVWXYZ0123456789".ToCharArray();

public PasswordGenerator()
{
Minimum = DefaultMinimum;
Maximum = DefaultMaximum;
Exclusions = null;
}


private char[] GetAvailableChars()
{
ArrayList result = new ArrayList(pwdCharArray);
char[] exclusions = String.Empty.ToCharArray();
if (exclusionSet != null)
{
exclusions = exclusionSet.ToCharArray();
}
foreach (char exclusion in exclusions)
{
result.Remove(exclusion);
}

return (char[]) result.ToArray(typeof(char));
}

public string Generate()
{
int size = maxSize;
byte[] data = new byte[1];
char[] availableChars = GetAvailableChars();

RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = (data[0] % (maxSize - minSize)) + minSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);

foreach(byte datum in data)
{
result.Append(availableChars[datum % (availableChars.Length - 1)]);
}

return result.ToString();
}


public string Exclusions
{
get { return exclusionSet; }

set { exclusionSet = value; }

}

public int Maximum
{
get { return this.maxSize; }

set
{
this.maxSize = value;
if (this.minSize >= this.maxSize)
{
this.maxSize = PasswordGenerator.DefaultMaximum;
}
}

}

public int Minimum
{
get { return this.minSize; }

set
{
this.minSize = value;
if (PasswordGenerator.DefaultMinimum > this.minSize)
{
this.minSize = PasswordGenerator.DefaultMinimum;
}
}

}
}


its kind of old so i cant ask him myself

thanks
Marhanen

oracleguy
06-21-2006, 07:48 PM
It looks to be C# code which does run under .net; you should be able to get it to run on a .net page.

marhanen
06-21-2006, 08:00 PM
ok so how would i put it on a .net page.

otaku149
06-21-2006, 08:38 PM
ok so how would i put it on a .net page.
You need to create a class PasswordGenerator.cs and place that file into your App_Code folder (ASP.Net 2.0). Then you need to create an object of that class, something like below:

PasswordGenerator pwd = new PasswordGenerator();

//display the password into a label for testing
lblMsg.Text = pwd.Generate();

otaku149
06-21-2006, 09:13 PM
If your goal is to generate a password, you can simply use the The GUID method instead:

C#

string pwd = System.Guid.NewGuid().ToString().Substring(0, 12).Replace("-", "").ToUpper();
Response.Write(pwd);


VB.Net

Dim pwd As String = System.Guid.NewGuid().ToString().Substring(0, 12).Replace("-", "").ToUpper()
Response.Write(pwd)

marhanen
06-21-2006, 09:21 PM
its kind of like a key generator i wanted to try and get something that would randomly choose a number or letter from a list and have it so its so long and then have it paste it in a file andhave no duplicates

i want it to not use certain letters and numbers and i would want it to be like 25 choices

a78fd-7ajf3-as4r2-a84hd-as74h

i want to be able to tell the program to only use these numbers and letters

i have another code which is .vb that i'll post in the other forum.

otaku149
06-21-2006, 09:50 PM
The PasswordGenerator Class will do the job. Minimum and Maximum values are 25 to get your desired length. Enter letters and numbers that you don't want into the pwd.Exclusions:


PasswordGenerator pwd = new PasswordGenerator();
pwd.Minimum = 25;
pwd.Maximum = 25;
pwd.Exclusions = "0, 1, 2, 3, A, B, C, D";
Response.Write(pwd.Generate());


This will generate a 25 length key without Exclusions, something like:

5ZL6QRGJZHM8MVKYWFYRSGHZT

otaku149
06-21-2006, 10:21 PM
And if you wish to add separator like below:

V6HRJ-XKQ56-LWQRJ-Z7JQN-QF4PV

You will need to modify the class:

PasswordGenerator.cs

using System;
using System.Collections;
using System.Security.Cryptography;
using System.Text;

public class PasswordGenerator
{
private const int DefaultMaximum = 24;
private const int DefaultMinimum = 24;
private string exclusionSet;
private int maxSize;
private int minSize;
private char[] pwdCharArray = "ABCDFGHJKLMNPQRSTVWXYZ0123456789".ToCharArray();

public PasswordGenerator()
{
Minimum = DefaultMinimum;
Maximum = DefaultMaximum;
Exclusions = null;
}


private char[] GetAvailableChars()
{
ArrayList result = new ArrayList(pwdCharArray);
char[] exclusions = String.Empty.ToCharArray();
if (exclusionSet != null)
{
exclusions = exclusionSet.ToCharArray();
}
foreach (char exclusion in exclusions)
{
result.Remove(exclusion);
}

return (char[])result.ToArray(typeof(char));
}

public string Generate()
{
int size = maxSize;
byte[] data = new byte[1];
char[] availableChars = GetAvailableChars();

RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = (data[0] % (maxSize - minSize)) + minSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);

foreach (byte datum in data)
{
result.Append(availableChars[datum % (availableChars.Length - 1)]);
}

return InsertSeparatorEvery(result.ToString(), "-", 5);
}

public string InsertSeparatorEvery(string String, string Separator, int Count)
{
StringBuilder final = new StringBuilder();
for (int i = 0; i <= String.Length - 1; i += Count)
{
if (!(i + Count > String.Length))
{
final.Append(String.Substring(i, Count) + Separator);
}
else
{
final.Append(String.Substring(i) + Separator);
}
}
return final.ToString().Substring(0, final.Length - 1);
}

public string Exclusions
{
get { return exclusionSet; }

set { exclusionSet = value; }

}

public int Maximum
{
get { return this.maxSize; }

set
{
this.maxSize = value;
if (this.minSize >= this.maxSize)
{
this.maxSize = PasswordGenerator.DefaultMaximum;
}
}

}

public int Minimum
{
get { return this.minSize; }

set
{
this.minSize = value;
if (PasswordGenerator.DefaultMinimum > this.minSize)
{
this.minSize = PasswordGenerator.DefaultMinimum;
}
}

}
}



Generate the key

protected void Page_Load(object sender, EventArgs e)
{
PasswordGenerator pwd = new PasswordGenerator();
pwd.Minimum = 25;
pwd.Maximum = 25;
pwd.Exclusions = "0, 1, 2, 3, A, B, C, D";
Response.Write(pwd.Generate());
}

marhanen
06-26-2006, 01:23 AM
so running it i would put PasswordGenerator.cs as a file and then have the generate a key on the website like in a form?

otaku149
06-26-2006, 01:38 AM
so running it i would put PasswordGenerator.cs as a file and then have the generate a key on the website like in a form?
Yes, create a class PasswordGenerator.cs with the code I gave you and place that file into your folder App_Code. In your .aspx page, create an instance of that class like below:

PasswordGenerator pwd = new PasswordGenerator();
pwd.Minimum = 25;
pwd.Maximum = 25;
pwd.Exclusions = "0, 1, 2, 3, A, B, C, D";
lblMsg.Text = pwd.Generate();

In the above example, pwd.Generate() will generate the key, I have placed the generated key into a label but place it whatever you want.

marhanen
06-27-2006, 12:48 AM
thanks man that really helped.

otaku149
06-27-2006, 01:11 AM
you are welcome :)