Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
  #2 (permalink)  
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 -/
  #3 (permalink)  
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 -/
  #4 (permalink)  
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
  #5 (permalink)  
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 -/
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:36 AM.


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