Click here to Skip to main content
16,012,316 members
Home / Discussions / C#
   

C#

 
GeneralRe: deserializing in c#.net Pin
fatidarya22-Jun-04 3:00
fatidarya22-Jun-04 3:00 
GeneralRe: deserializing in c#.net Pin
Heath Stewart22-Jun-04 4:07
protectorHeath Stewart22-Jun-04 4:07 
GeneralRe: deserializing in c#.net Pin
fatidarya22-Jun-04 19:12
fatidarya22-Jun-04 19:12 
GeneralRe: deserializing in c#.net Pin
Heath Stewart22-Jun-04 19:19
protectorHeath Stewart22-Jun-04 19:19 
GeneralRe: serializing in c#.net Pin
Stefan Troschuetz21-Jun-04 20:44
Stefan Troschuetz21-Jun-04 20:44 
GeneralJava script in c# Pin
_Searcher_21-Jun-04 5:53
_Searcher_21-Jun-04 5:53 
GeneralRe: Java script in c# Pin
Dave Kreskowiak21-Jun-04 6:10
mveDave Kreskowiak21-Jun-04 6:10 
GeneralAttempting to implement Keyed MD-5 Hash Pin
selil21-Jun-04 5:29
selil21-Jun-04 5:29 
Code project C# guru’s Confused | :confused: ,

I am attempting to implement RFC 2095 ( http://www.ietf.org/rfc/rfc2095.txt ) I’ve found what I believe is an error in the crypto library for C#.

Now be kind I’ve been coding C# for exactly 1 week OMG | :OMG: , and it has been about 7 years since I coded in production Eek! | :eek: . The last time I coded was in VI on Unix. This is just one of the methods I’m attempting to implement in this project. The rest of the project includes an SMTP server, and verification schema.

I’m implementing the procedures in a C# program found in RFC 2095, and using the CRAM algorithm found in http://www.ietf.org/rfc/rfc2104.txt . The test data strings found in RFC 2095 include a share key string “tanstaaftanstaaf” and the challenge “<1896.697170952@postoffice.reston.mci.net>” these test values should produce according to the test in the RFC the hashed hex output “b9 13 a6 02 c7 ed a7 a4 95 b4 e6 e7 33 4d 38 90” .

In the following program from the MSDN code library (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconextendingkeyedhashalgorithmclass.htm) I get the following output from the output string B9 13 A6 2 C7 ED A7 A4 95 B4 E6 E7 33 4D 38 90 on the test case. And, of course the fourth hex pair has dropped the leading zero D'Oh! | :doh: .

That would be wrong according to the Keyed MD-5 hash algorithm Mad | :mad: . The leading zero should be left in place. From my feeble attempts at debugging it looks like it is either the output of the HEX according to Microsoft or internal to the crypto library. Either way how do I make sure that it does not drop that leading zero Dead | X| ?

Thanks in advance,

Sam


From the MSDN Code Library with RFC 2095 Edits
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Security.Cryptography;

public class TestHMACMD5
{
static private void PrintByteArray(Byte[] arr)
{
int i;
Console.WriteLine("Length: " + arr.Length);
for (i=0; i<arr.length; i++)=""
="" {
="" console.write("{0:x}",="" arr[i]);
="" console.write("="" ");
="" if="" (="" (i+9)%8="=" 0="" )="" console.writeline();
="" }
="" (i%8="" !="0)" public="" static="" void="" main()="" create="" a="" key.
="" byte[]="" key1="{0x0b," 0x0b,="" 0x0b};
="" pass="" the="" key="" to="" constructor="" of="" hmacmd5="" class.="" hmac1="new" hmacmd5(key1);

="" another="" added="" test="" case="" for="" rfc="" 2095

="" key2="System.Text.Encoding.ASCII.GetBytes("tanstaaftanstaaf");" hmac2="new" hmacmd5(key2);

="" encode="" string="" into="" byte="" array,="" hash="" array,
="" and="" print="" screen.
="" data1="System.Text.Encoding.ASCII.GetBytes("KeyString" printbytearray(hmac1.computehash(data1));

="" 2095
="" data2="System.Text.Encoding.ASCII.GetBytes("<1896.697170952@postoffice.reston.mci.net">");
PrintByteArray(hmac2.ComputeHash(data2));
}
}
public class HMACMD5 : KeyedHashAlgorithm
{
private MD5 hash1;
private MD5 hash2;
private bool bHashing = false;

private byte[] rgbInner = new byte[64];
private byte[] rgbOuter = new byte[64];

public HMACMD5 (byte[] rgbKey)
{
HashSizeValue = 128;
// Create the hash algorithms.
hash1 = MD5.Create();
hash2 = MD5.Create();
// Get the key.
if (rgbKey.Length > 64)
{
KeyValue = hash1.ComputeHash(rgbKey);
// No need to call Initialize, ComputeHash does it automatically.
}
else
{
KeyValue = (byte[]) rgbKey.Clone();
}
// Compute rgbInner and rgbOuter.
int i = 0;
for (i=0; i<64; i++)
{
rgbInner[i] = 0x36;
rgbOuter[i] = 0x5C;
}
for (i=0; i<keyvalue.length; i++)=""
="" {
="" rgbinner[i]="" ^="KeyValue[i];
" rgbouter[i]="" }=""

="" public="" override="" byte[]="" key="" get="" {="" return="" (byte[])="" keyvalue.clone();="" }
="" set="" if="" (bhashing)="" throw="" new="" exception("cannot="" change="" during="" hash="" operation");
="" (value.length=""> 64)
{
KeyValue = hash1.ComputeHash(value);
// No need to call Initialize, ComputeHash does it automatically.
}
else
{
KeyValue = (byte[]) value.Clone();
}
// Compute rgbInner and rgbOuter.
int i = 0;
for (i=0; i<64; i++)
{
rgbInner[i] = 0x36;
rgbOuter[i] = 0x5C;
}
for (i=0; i
GeneralRe: Attempting to implement Keyed MD-5 Hash Pin
Heath Stewart21-Jun-04 5:42
protectorHeath Stewart21-Jun-04 5:42 
GeneralRe: Attempting to implement Keyed MD-5 Hash Pin
selil21-Jun-04 6:00
selil21-Jun-04 6:00 
GeneralRe: Attempting to implement Keyed MD-5 Hash Pin
Heath Stewart21-Jun-04 6:34
protectorHeath Stewart21-Jun-04 6:34 
GeneralRe: Attempting to implement Keyed MD-5 Hash Pin
Dave Kreskowiak21-Jun-04 5:52
mveDave Kreskowiak21-Jun-04 5:52 
GeneralRe: Attempting to implement Keyed MD-5 Hash Pin
selil21-Jun-04 6:10
selil21-Jun-04 6:10 
QuestionHow to use proertyGrid to show html componet's property? Pin
nakey_yang21-Jun-04 5:28
nakey_yang21-Jun-04 5:28 
AnswerRe: How to use proertyGrid to show html componet's property? Pin
Heath Stewart21-Jun-04 5:35
protectorHeath Stewart21-Jun-04 5:35 
Generalusing xmodem protocol for file transfer in dotnet(c#) Pin
son_sant21-Jun-04 5:20
son_sant21-Jun-04 5:20 
GeneralRe: using xmodem protocol for file transfer in dotnet(c#) Pin
Heath Stewart21-Jun-04 5:33
protectorHeath Stewart21-Jun-04 5:33 
QuestionExplorer DragImage? Pin
TylerBrinks21-Jun-04 4:47
TylerBrinks21-Jun-04 4:47 
AnswerRe: Explorer DragImage? Pin
Heath Stewart21-Jun-04 5:05
protectorHeath Stewart21-Jun-04 5:05 
GeneralRe: Explorer DragImage? Pin
TylerBrinks21-Jun-04 11:35
TylerBrinks21-Jun-04 11:35 
GeneralRe: Explorer DragImage? Pin
Heath Stewart21-Jun-04 11:42
protectorHeath Stewart21-Jun-04 11:42 
GeneralRe: Explorer DragImage? Pin
TylerBrinks21-Jun-04 11:52
TylerBrinks21-Jun-04 11:52 
GeneralRe: Explorer DragImage? Pin
Heath Stewart21-Jun-04 11:58
protectorHeath Stewart21-Jun-04 11:58 
GeneralRe: Explorer DragImage? Pin
TylerBrinks22-Jun-04 5:32
TylerBrinks22-Jun-04 5:32 
GeneralRe: Explorer DragImage? Pin
Heath Stewart22-Jun-04 5:43
protectorHeath Stewart22-Jun-04 5:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.