Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2008, 09:43 AM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default convert byte[] to hex value

Hi,
I have a function to encrypt the password which returns a byte[].
When saving to a text file it does not return the hex value but displays System.Byte[].After encrypting the password, i need the value in the file to be displayed as 0x0100CE6EE7655EC6B23C78209BADE0556A6F457E0B7CAA4B 67.

Following is the function used to Encrypt the password.

 public static byte[] EncryptPwd(string password)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            UTF8Encoding encoder = new UTF8Encoding();
            try
            {
                return (md5Hasher.ComputeHash(encoder.GetBytes(password)) );
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


Gunjan
__________________
Gunjan
 
Old January 23rd, 2008, 10:08 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

0x0100CE6EE7655EC6B23C78209BADE0556A6F457E0B7CAA4B 67 reads like one (very large) number, so if it has to be this exact format you'll probably have to do it yourself. The key is probably the ToString("X") method of the byte.

Something like this:

Code:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("0x");
foreach (byte b in bytes)
{
  sb.AppendFormat("{0:X2}", b);
}
Console.WriteLine(sb.ToString());

/- Sam Judson : Wrox Technical Editor -/
 
Old January 23rd, 2008, 11:30 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Actually on second thoughts you might be after this:

BitConverter.ToString(hashedBytes);

/- Sam Judson : Wrox Technical Editor -/
 
Old January 24th, 2008, 02:38 AM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

But using string.Format("{0:X2}",b) or BitConverter i get different values for the same data ....
Why is it ??


Gunjan
 
Old January 24th, 2008, 05:15 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

BitConverter does insert hyphens in it, but other than that should return the same hexadecimal values. If not then please show us your example code.

/- Sam Judson : Wrox Technical Editor -/




Similar Threads
Thread Thread Starter Forum Replies Last Post
how do I convert character to utf hex code? mondayisgreat Classic ASP Basics 1 January 24th, 2008 01:48 PM
Convert byte[] to char[] mega .NET Framework 2.0 1 December 29th, 2006 11:29 AM
convert byte[] to hex wkm1925 C# 1 December 14th, 2006 09:07 PM
convert byte to bitmap jlzmut C# 2 December 18th, 2005 05:18 PM
convert otb files to hex string ela Classic ASP Professional 1 October 14th, 2004 01:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.