Friday, April 18, 2008

C# MD5 Encrypt and Random password generator

I think these functions can come handy for anyone who is developing C# webpages.

First function generates an random password with the given length:


public string GetRandomPasswordUsingGUID(int length)
{
// Get the GUID
string guidResult = System.Guid.NewGuid().ToString();
// Remove the hyphens
guidResult = guidResult.Replace("-", string.Empty);
// Make sure length is valid
if (length <= 0 length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
// Return the first length bytes
return guidResult.Substring(0, length);
}


Second function Encodes the given string to an MD5 string.


public string EncodePassword(string originalPassword)
{
//Declarations
Byte[] originalBytes;
Byte[] encodedBytes;
MD5 md5;
//Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
md5 = new MD5CryptoServiceProvider();
originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
encodedBytes = md5.ComputeHash(originalBytes);
//Convert encoded bytes back to a 'readable' string
return BitConverter.ToString(encodedBytes);
}


Last function i ripped of an other site, im not sure which one it was. Credits for him/her :-)

Goodluck,
Sjoerd

Thursday, April 17, 2008

Compile .resx to .resources files from .NET 1.0 projects.

I found out today that old .resx files from .Net 1.0 projects wont compile automatically. You have to do this by your self after a change in it!

resgen.exe will do this for you. This little program is somewhere in your visual studio folder:

resgen.exe test.resx test.resources

This will compile the resx file to a binairy resources file.

You can add this to your tools in Visual Studio (i used 2005). So you dont need to open an command prompt window each time:

Tools -> External Tools -> Add ->

Title: Build .resource file
Command: C:\Program Files\Microsoft Visual Studio 8\SmartDevices\SDK\SDKTools\resgen.exe
Arguments: $(ItemPath)
Initial directory: $(ItemDir)
[v] use output window

This will compile the current opend resx file to a resource file in the same directory.

Here's my output:

Read in 150 resources from 'D:\Projecten\***\TextRes-EN.resx'
Writing resource file... Done.

Goodluck!

C# System.Configuration.ConfigurationSettings.AppSettings is obsolete !

Today, when i was building an old project, i got the following warning:

Warning 10 'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'

With the following line of code:

SmtpClient emailClient = new SmtpClient(ConfigurationSettings.AppSettings["SMTPServer"]);

Oke so just replace it right? ..Not working:

SmtpClient emailClient = new SmtpClient(System.Configuration.ConfigurationManager.AppSettings["SMTPServer"]);

ConfigurationManager not found...

The thing is they packed the ConfigurationManager in another dll file, which you need to add as reference.

right-click References -> add reference -> select System.Configuration in .NET tab

Et voila following code works:

SmtpClient emailClient = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"]);


Hope this will help anyone.

Tuesday, April 1, 2008

UUencode and UUdecode in C#.net

I was looking today for a new UUencode function and my colleague ended up with the following class which works like a charm. This one is really fast.


using System;
using System.Text;
public static class Codecs
{
static
readonly byte[] UUEncMap = new byte[]
{
0x60, 0x21, 0x22, 0x23, 0x24,
0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
0x30,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,
0x3E, 0x3F,
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49,
0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
0x57,
0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F
};
static readonly
byte[] UUDecMap = new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A,
0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
0x20, 0x21, 0x22,
0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
0x2F,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0x3A,
0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00
};
public static void UUDecode(System.IO.Stream input,
System.IO.Stream output)
{
//if (input == null)
// throw new
ArgumentNullException("input");
//if (output == null)
// throw new
ArgumentNullException("output");
long len = input.Length;
if (len ==
0)
return;
long didx = 0;
int nextByte = input.ReadByte();
while
(nextByte >= 0)
{
// get line length (in number of encoded
octets)
int line_len = UUDecMap[nextByte];
// ascii printable to 0-63 and
4-byte to 3-byte conversion
long end = didx + line_len;
byte A, B, C,
D;
if (end > 2)
{
while (didx < a =" UUDecMap[input.ReadByte()];" b =" UUDecMap[input.ReadByte()];" c =" UUDecMap[input.ReadByte()];" d =" UUDecMap[input.ReadByte()];">> 4) & 3)));
output.WriteByte((byte)(((B <<>> 2) & 15)));
output.WriteByte((byte)(((C
<< a =" UUDecMap[input.ReadByte()];" b =" UUDecMap[input.ReadByte()];">> 4) & 3)));
didx++;
}
if (didx < b =" UUDecMap[input.ReadByte()];" c =" UUDecMap[input.ReadByte()];">> 2) & 15)));
didx++;
}
// skip
padding
do
{
nextByte = input.ReadByte();
}
while (nextByte >=
0 && nextByte != '\n' && nextByte != '\r');
// skip end of
line
do
{
nextByte = input.ReadByte();
}
while (nextByte >= 0
&& (nextByte == '\n' nextByte == '\r'));
}
}
public static
void UUEncode(System.IO.Stream input, System.IO.Stream output)
{
//if
(input == null)
// throw new ArgumentNullException("input");
//if (output
== null)
// throw new ArgumentNullException("output");
long len =
input.Length;
if (len == 0)
return;
int sidx = 0;
int line_len =
45;
byte[] nl = Encoding.ASCII.GetBytes(Environment.NewLine);
byte A, B,
C;
// split into lines, adding line-length and line terminator
while (sidx
+ line_len < end =" sidx" a =" (byte)input.ReadByte();" b =" (byte)input.ReadByte();" c =" (byte)input.ReadByte();">> 2) &
63]);
output.WriteByte(UUEncMap[(B >> 4) & 15 (A <<>> 6) & 3 (B << idx =" 0;" a =" (byte)input.ReadByte();" b =" (byte)input.ReadByte();" c =" (byte)input.ReadByte();">> 2) &
63]);
output.WriteByte(UUEncMap[(B >> 4) & 15 (A <<>> 6) & 3 (B << a =" (byte)input.ReadByte();" b =" (byte)input.ReadByte();">> 2) &
63]);
output.WriteByte(UUEncMap[(B >> 4) & 15 (A << a =" (byte)input.ReadByte();">> 2) & 63]);
output.WriteByte(UUEncMap[(A << idx =" 0;">


I call my function like this:

//First write to memory
MemoryStream
mmsStream = new MemoryStream();
StreamWriter srwTemp = new
StreamWriter(mmsStream);
//sBuffer is my encoded
text.
srwTemp.Write(sBuffer);
srwTemp.Flush();
mmsStream.Position =
0;
//Write to IO..filename is a localvar which
has the location of the file.
System.IO.FileStream stmStreamOut = new
System.IO.FileStream(filename, FileMode.OpenOrCreate);
//Calling
UUDecode
Codecs.UUDecode(mmsStream,
stmStreamOut);
stmStreamOut.Flush();
stmStreamOut.Close();
//Done


Goodluck.