473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

MD5 conversion problem

Hello,

I'm struggling with the string conversion to MD5 which I've never user
before.

I have a string that I need to encode which looks approximately like this:

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang
=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
BU:shp_Term=2"

I'm doing it this way:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As New ASCIIEncoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
Dim sNewCRC as String = Convert.ToString(md5.ComputeHash(hashedBytes))

It doesn't work. When I see the output on the page where I pass this string,
it looks like this:

'<input type=hidden name=crc value="System.Byte[]">'+

I don't know exactly how it should look like, but probably not like
"System.Byte[]"

I'm doing something wrong, but I don't know what.

I would really appreciate your help.

Thank you,

Peter Afonin
Nov 18 '05 #1
15 2392
Why don't you try Convert.ToBase64String instead? Typically, you encode
binary data as a string with either Base64 or hex string encoding.

Also, be careful about using ASCII encoding to convert the input string to
binary. If it includes any non-ASCII characters, you'll be throwing data
away. UTF8 is safer. Whatever you do, make sure you alway compute the hash
the same way if you are going to be using it for a comparison.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm struggling with the string conversion to MD5 which I've never user
before.

I have a string that I need to encode which looks approximately like this:

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang
=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan= BU:shp_Term=2"

I'm doing it this way:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As New ASCIIEncoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
Dim sNewCRC as String = Convert.ToString(md5.ComputeHash(hashedBytes))

It doesn't work. When I see the output on the page where I pass this string, it looks like this:

'<input type=hidden name=crc value="System.Byte[]">'+

I don't know exactly how it should look like, but probably not like
"System.Byte[]"

I'm doing something wrong, but I don't know what.

I would really appreciate your help.

Thank you,

Peter Afonin

Nov 18 '05 #2
Thank you, Joe.

I've tried to change it like this:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As new UTF8Encoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
Me.crc.Value = sCRC

Yes, the output string has changed:

'<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+

But I'm still getting a "bad crc" error.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:uo****************@TK2MSFTNGP09.phx.gbl...
Why don't you try Convert.ToBase64String instead? Typically, you encode
binary data as a string with either Base64 or hex string encoding.

Also, be careful about using ASCII encoding to convert the input string to
binary. If it includes any non-ASCII characters, you'll be throwing data
away. UTF8 is safer. Whatever you do, make sure you alway compute the hash the same way if you are going to be using it for a comparison.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm struggling with the string conversion to MD5 which I've never user
before.

I have a string that I need to encode which looks approximately like this:
"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
BU:shp_Term=2"

I'm doing it this way:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As New ASCIIEncoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
Dim sNewCRC as String = Convert.ToString(md5.ComputeHash(hashedBytes))

It doesn't work. When I see the output on the page where I pass this

string,
it looks like this:

'<input type=hidden name=crc value="System.Byte[]">'+

I don't know exactly how it should look like, but probably not like
"System.Byte[]"

I'm doing something wrong, but I don't know what.

I would really appreciate your help.

Thank you,

Peter Afonin


Nov 18 '05 #3
What is giving you a "bad CRC" error? Is it the code below? That looks
like it should just return a base64 encoded MD5 hash of whatever string was
provided.

It isn't clear to me what you are trying to do or what the input is in the
funtion.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eQ****************@TK2MSFTNGP12.phx.gbl...
Thank you, Joe.

I've tried to change it like this:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As new UTF8Encoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
Me.crc.Value = sCRC

Yes, the output string has changed:

'<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+

But I'm still getting a "bad crc" error.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:uo****************@TK2MSFTNGP09.phx.gbl...
Why don't you try Convert.ToBase64String instead? Typically, you encode
binary data as a string with either Base64 or hex string encoding.

Also, be careful about using ASCII encoding to convert the input string to
binary. If it includes any non-ASCII characters, you'll be throwing data away. UTF8 is safer. Whatever you do, make sure you alway compute the

hash
the same way if you are going to be using it for a comparison.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm struggling with the string conversion to MD5 which I've never user
before.

I have a string that I need to encode which looks approximately like

this:
"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
BU:shp_Term=2"

I'm doing it this way:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As New ASCIIEncoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
Dim sNewCRC as String = Convert.ToString(md5.ComputeHash(hashedBytes))

It doesn't work. When I see the output on the page where I pass this

string,
it looks like this:

'<input type=hidden name=crc value="System.Byte[]">'+

I don't know exactly how it should look like, but probably not like
"System.Byte[]"

I'm doing something wrong, but I don't know what.

I would really appreciate your help.

Thank you,

Peter Afonin



Nov 18 '05 #4
Joe, I'm connecting an online store to the payment system. I need to pass
this string to this payment gateway, and it will return me another string
back, confirming that the payment was successful.

I contacted the techsupport of this gateway. They said that I don't have to
convert my string to Base64. I need to convert every byte to 16-bit number
or something like this. They don't use ASP.Net, so couldn't give me an exact
code. They said that it should look approximately like this:

StringBuilder sb = new StringBuilder();

for (int i=0;i<hash.Length;i++)
sb.Append(hash[i].ToString("x").PadLeft(2,'0'));

return sb.ToString();

How it should look in VB.Net?

Thank you,

Peter
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:OJ****************@tk2msftngp13.phx.gbl...
What is giving you a "bad CRC" error? Is it the code below? That looks
like it should just return a base64 encoded MD5 hash of whatever string was provided.

It isn't clear to me what you are trying to do or what the input is in the
funtion.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eQ****************@TK2MSFTNGP12.phx.gbl...
Thank you, Joe.

I've tried to change it like this:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As new UTF8Encoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
Me.crc.Value = sCRC

Yes, the output string has changed:

'<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+

But I'm still getting a "bad crc" error.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:uo****************@TK2MSFTNGP09.phx.gbl...
Why don't you try Convert.ToBase64String instead? Typically, you encode binary data as a string with either Base64 or hex string encoding.

Also, be careful about using ASCII encoding to convert the input string
to binary. If it includes any non-ASCII characters, you'll be throwing data away. UTF8 is safer. Whatever you do, make sure you alway compute
the
hash
the same way if you are going to be using it for a comparison.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl...
> Hello,
>
> I'm struggling with the string conversion to MD5 which I've never

user > before.
>
> I have a string that I need to encode which looks approximately like

this:
>
>

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> BU:shp_Term=2"
>
> I'm doing it this way:
>
> Dim hashedBytes As Byte()
> Dim md5 As New MD5CryptoServiceProvider
> Dim encoder As New ASCIIEncoding
> hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> Dim sNewCRC as String = Convert.ToString(md5.ComputeHash(hashedBytes)) >
> It doesn't work. When I see the output on the page where I pass this
string,
> it looks like this:
>
> '<input type=hidden name=crc value="System.Byte[]">'+
>
> I don't know exactly how it should look like, but probably not like
> "System.Byte[]"
>
> I'm doing something wrong, but I don't know what.
>
> I would really appreciate your help.
>
> Thank you,
>
> Peter Afonin
>
>



Nov 18 '05 #5
By the way, If you want to compute an MD5 ash on a pswd like string and get
the result in a string (eg for storing ashing in a dB), you could use this
wrapper method:
FormsAuthentication.HashPasswordForStoringInConfig File()

José
"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm struggling with the string conversion to MD5 which I've never user
before.

I have a string that I need to encode which looks approximately like this:

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang
=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
BU:shp_Term=2"

I'm doing it this way:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As New ASCIIEncoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
Dim sNewCRC as String = Convert.ToString(md5.ComputeHash(hashedBytes))

It doesn't work. When I see the output on the page where I pass this
string,
it looks like this:

'<input type=hidden name=crc value="System.Byte[]">'+

I don't know exactly how it should look like, but probably not like
"System.Byte[]"

I'm doing something wrong, but I don't know what.

I would really appreciate your help.

Thank you,

Peter Afonin

Nov 18 '05 #6
Thank you, I'll try.

Peter

"José Joye" <jo*******@KILLTHESPAMSbluewin.ch> wrote in message
news:uI*************@TK2MSFTNGP12.phx.gbl...
By the way, If you want to compute an MD5 ash on a pswd like string and get the result in a string (eg for storing ashing in a dB), you could use this
wrapper method:
FormsAuthentication.HashPasswordForStoringInConfig File()

José
"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eJ**************@TK2MSFTNGP11.phx.gbl...
Hello,

I'm struggling with the string conversion to MD5 which I've never user
before.

I have a string that I need to encode which looks approximately like this:
"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang =ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan= BU:shp_Term=2"

I'm doing it this way:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As New ASCIIEncoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
Dim sNewCRC as String = Convert.ToString(md5.ComputeHash(hashedBytes))

It doesn't work. When I see the output on the page where I pass this
string,
it looks like this:

'<input type=hidden name=crc value="System.Byte[]">'+

I don't know exactly how it should look like, but probably not like
"System.Byte[]"

I'm doing something wrong, but I don't know what.

I would really appreciate your help.

Thank you,

Peter Afonin


Nov 18 '05 #7
Ah, this is the hex encoding thing I mentioned in my first post and didn't
provide an example for. You can either use the BitConverter class to
convert the byte[] to hex digits and then strip out the - characters it puts
in between or using something like my function called ConvertToOctetString
that you can do a Google groups search for that does this. It basically
just uses a StringBuilder and the X2 format code to loop over the bytes and
build the string.

Also, MAKE SURE that the vendor is calculating the MD5 of the data using the
same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your input
byte array may be different and thus your hash will be different.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
Joe, I'm connecting an online store to the payment system. I need to pass
this string to this payment gateway, and it will return me another string
back, confirming that the payment was successful.

I contacted the techsupport of this gateway. They said that I don't have to convert my string to Base64. I need to convert every byte to 16-bit number
or something like this. They don't use ASP.Net, so couldn't give me an exact code. They said that it should look approximately like this:

StringBuilder sb = new StringBuilder();

for (int i=0;i<hash.Length;i++)
sb.Append(hash[i].ToString("x").PadLeft(2,'0'));

return sb.ToString();

How it should look in VB.Net?

Thank you,

Peter
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:OJ****************@tk2msftngp13.phx.gbl...
What is giving you a "bad CRC" error? Is it the code below? That looks
like it should just return a base64 encoded MD5 hash of whatever string

was
provided.

It isn't clear to me what you are trying to do or what the input is in the
funtion.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eQ****************@TK2MSFTNGP12.phx.gbl...
Thank you, Joe.

I've tried to change it like this:

Dim hashedBytes As Byte()
Dim md5 As New MD5CryptoServiceProvider
Dim encoder As new UTF8Encoding
hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
Me.crc.Value = sCRC

Yes, the output string has changed:

'<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+

But I'm still getting a "bad crc" error.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com>

wrote in message news:uo****************@TK2MSFTNGP09.phx.gbl...
> Why don't you try Convert.ToBase64String instead? Typically, you encode > binary data as a string with either Base64 or hex string encoding.
>
> Also, be careful about using ASCII encoding to convert the input string
to
> binary. If it includes any non-ASCII characters, you'll be throwing

data
> away. UTF8 is safer. Whatever you do, make sure you alway compute

the hash
> the same way if you are going to be using it for a comparison.
>
> HTH,
>
> Joe K.
>
> "Peter Afonin" <pv*@speakeasy.net> wrote in message
> news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > Hello,
> >
> > I'm struggling with the string conversion to MD5 which I've never user > > before.
> >
> > I have a string that I need to encode which looks approximately like this:
> >
> >
"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang > >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> > BU:shp_Term=2"
> >
> > I'm doing it this way:
> >
> > Dim hashedBytes As Byte()
> > Dim md5 As New MD5CryptoServiceProvider
> > Dim encoder As New ASCIIEncoding
> > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > Dim sNewCRC as String = Convert.ToString(md5.ComputeHash(hashedBytes)) > >
> > It doesn't work. When I see the output on the page where I pass this > string,
> > it looks like this:
> >
> > '<input type=hidden name=crc value="System.Byte[]">'+
> >
> > I don't know exactly how it should look like, but probably not like > > "System.Byte[]"
> >
> > I'm doing something wrong, but I don't know what.
> >
> > I would really appreciate your help.
> >
> > Thank you,
> >
> > Peter Afonin
> >
> >
>
>



Nov 18 '05 #8
Thank you, Joe!

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:eB**************@TK2MSFTNGP11.phx.gbl...
Ah, this is the hex encoding thing I mentioned in my first post and didn't
provide an example for. You can either use the BitConverter class to
convert the byte[] to hex digits and then strip out the - characters it puts in between or using something like my function called ConvertToOctetString
that you can do a Google groups search for that does this. It basically
just uses a StringBuilder and the X2 format code to loop over the bytes and build the string.

Also, MAKE SURE that the vendor is calculating the MD5 of the data using the same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your input
byte array may be different and thus your hash will be different.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
Joe, I'm connecting an online store to the payment system. I need to pass
this string to this payment gateway, and it will return me another string back, confirming that the payment was successful.

I contacted the techsupport of this gateway. They said that I don't have

to
convert my string to Base64. I need to convert every byte to 16-bit number or something like this. They don't use ASP.Net, so couldn't give me an

exact
code. They said that it should look approximately like this:

StringBuilder sb = new StringBuilder();

for (int i=0;i<hash.Length;i++)
sb.Append(hash[i].ToString("x").PadLeft(2,'0'));

return sb.ToString();

How it should look in VB.Net?

Thank you,

Peter
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote in message news:OJ****************@tk2msftngp13.phx.gbl...
What is giving you a "bad CRC" error? Is it the code below? That looks like it should just return a base64 encoded MD5 hash of whatever string
was
provided.

It isn't clear to me what you are trying to do or what the input is in the funtion.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eQ****************@TK2MSFTNGP12.phx.gbl...
> Thank you, Joe.
>
> I've tried to change it like this:
>
> Dim hashedBytes As Byte()
> Dim md5 As New MD5CryptoServiceProvider
> Dim encoder As new UTF8Encoding
> hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
> Me.crc.Value = sCRC
>
> Yes, the output string has changed:
>
> '<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+
>
> But I'm still getting a "bad crc" error.
>
> Peter
>
> "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com>

wrote
> in message news:uo****************@TK2MSFTNGP09.phx.gbl...
> > Why don't you try Convert.ToBase64String instead? Typically, you

encode
> > binary data as a string with either Base64 or hex string encoding.
> >
> > Also, be careful about using ASCII encoding to convert the input

string
to
> > binary. If it includes any non-ASCII characters, you'll be
throwing data
> > away. UTF8 is safer. Whatever you do, make sure you alway compute the
> hash
> > the same way if you are going to be using it for a comparison.
> >
> > HTH,
> >
> > Joe K.
> >
> > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > > Hello,
> > >
> > > I'm struggling with the string conversion to MD5 which I've
never user
> > > before.
> > >
> > > I have a string that I need to encode which looks approximately

like > this:
> > >
> > >
>

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang > > >
> >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> > > BU:shp_Term=2"
> > >
> > > I'm doing it this way:
> > >
> > > Dim hashedBytes As Byte()
> > > Dim md5 As New MD5CryptoServiceProvider
> > > Dim encoder As New ASCIIEncoding
> > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > Dim sNewCRC as String =

Convert.ToString(md5.ComputeHash(hashedBytes))
> > >
> > > It doesn't work. When I see the output on the page where I pass this > > string,
> > > it looks like this:
> > >
> > > '<input type=hidden name=crc value="System.Byte[]">'+
> > >
> > > I don't know exactly how it should look like, but probably not like > > > "System.Byte[]"
> > >
> > > I'm doing something wrong, but I don't know what.
> > >
> > > I would really appreciate your help.
> > >
> > > Thank you,
> > >
> > > Peter Afonin
> > >
> > >
> >
> >
>
>



Nov 18 '05 #9
Hello Joe,

I found a code that should do exactly the same as in the example in my
previous message, but still doing something wrong, because the payment
gateway gives me a message that the string is bad. There is a chance that
the code itself is OK, but the data I put in is bad. But do you see anything
wrong with this code? I would appreciate your comments very much. Peter.

Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()

Dim unicodeText() As Byte
unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC)

enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
0, True)

Dim oMD5 As New System.Security.Cryptography.MD5CryptoServiceProvi der
Dim result() As Byte = oMD5.ComputeHash(unicodeText)

Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To CType(result.Length - 1, Integer)
sb.Append(result(i).ToString("X").PadLeft(2, "0"))
Next

sCRC = sb.ToString
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:eB**************@TK2MSFTNGP11.phx.gbl...
Ah, this is the hex encoding thing I mentioned in my first post and didn't
provide an example for. You can either use the BitConverter class to
convert the byte[] to hex digits and then strip out the - characters it puts in between or using something like my function called ConvertToOctetString
that you can do a Google groups search for that does this. It basically
just uses a StringBuilder and the X2 format code to loop over the bytes and build the string.

Also, MAKE SURE that the vendor is calculating the MD5 of the data using the same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your input
byte array may be different and thus your hash will be different.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
Joe, I'm connecting an online store to the payment system. I need to pass
this string to this payment gateway, and it will return me another string back, confirming that the payment was successful.

I contacted the techsupport of this gateway. They said that I don't have

to
convert my string to Base64. I need to convert every byte to 16-bit number or something like this. They don't use ASP.Net, so couldn't give me an

exact
code. They said that it should look approximately like this:

StringBuilder sb = new StringBuilder();

for (int i=0;i<hash.Length;i++)
sb.Append(hash[i].ToString("x").PadLeft(2,'0'));

return sb.ToString();

How it should look in VB.Net?

Thank you,

Peter
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote in message news:OJ****************@tk2msftngp13.phx.gbl...
What is giving you a "bad CRC" error? Is it the code below? That looks like it should just return a base64 encoded MD5 hash of whatever string
was
provided.

It isn't clear to me what you are trying to do or what the input is in the funtion.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:eQ****************@TK2MSFTNGP12.phx.gbl...
> Thank you, Joe.
>
> I've tried to change it like this:
>
> Dim hashedBytes As Byte()
> Dim md5 As New MD5CryptoServiceProvider
> Dim encoder As new UTF8Encoding
> hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
> Me.crc.Value = sCRC
>
> Yes, the output string has changed:
>
> '<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+
>
> But I'm still getting a "bad crc" error.
>
> Peter
>
> "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com>

wrote
> in message news:uo****************@TK2MSFTNGP09.phx.gbl...
> > Why don't you try Convert.ToBase64String instead? Typically, you

encode
> > binary data as a string with either Base64 or hex string encoding.
> >
> > Also, be careful about using ASCII encoding to convert the input

string
to
> > binary. If it includes any non-ASCII characters, you'll be
throwing data
> > away. UTF8 is safer. Whatever you do, make sure you alway compute the
> hash
> > the same way if you are going to be using it for a comparison.
> >
> > HTH,
> >
> > Joe K.
> >
> > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > > Hello,
> > >
> > > I'm struggling with the string conversion to MD5 which I've
never user
> > > before.
> > >
> > > I have a string that I need to encode which looks approximately

like > this:
> > >
> > >
>

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang > > >
> >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> > > BU:shp_Term=2"
> > >
> > > I'm doing it this way:
> > >
> > > Dim hashedBytes As Byte()
> > > Dim md5 As New MD5CryptoServiceProvider
> > > Dim encoder As New ASCIIEncoding
> > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > Dim sNewCRC as String =

Convert.ToString(md5.ComputeHash(hashedBytes))
> > >
> > > It doesn't work. When I see the output on the page where I pass this > > string,
> > > it looks like this:
> > >
> > > '<input type=hidden name=crc value="System.Byte[]">'+
> > >
> > > I don't know exactly how it should look like, but probably not like > > > "System.Byte[]"
> > >
> > > I'm doing something wrong, but I don't know what.
> > >
> > > I would really appreciate your help.
> > >
> > > Thank you,
> > >
> > > Peter Afonin
> > >
> > >
> >
> >
>
>



Nov 18 '05 #10
There are two main things to keep in mind here:

The value of sCRC is what is being used to create the hash.
The hash is being computed based on a Unicode encoding of the same hash.

Thus, for someone else to recreate the MD5 hash of the data from the same
source data, they need to use the exact same sCRC as input and must use
Unicode encoding. UTF8 or any other encoding will produce a different byte
array and thus a different hash.

If the input string and the encoding is the same, the MD5 should be the
same. The only thing that might vary is if they are assuming the bytes are
in the opposite order and you need to reverse the string.

The code you keep showing below is hard to follow because it is using a
variable called sCRC as the input and then also setting that to the output.
We can't tell where the data came from or where it is going.

Can you post a function that calculates the MD5 of an input string using the
proper encoding and returns it as a hex string?

Joe K.

"Peter Afonin" <pe***@gudzon.net> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Hello Joe,

I found a code that should do exactly the same as in the example in my
previous message, but still doing something wrong, because the payment
gateway gives me a message that the string is bad. There is a chance that
the code itself is OK, but the data I put in is bad. But do you see anything wrong with this code? I would appreciate your comments very much. Peter.

Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()

Dim unicodeText() As Byte
unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC)

enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
0, True)

Dim oMD5 As New System.Security.Cryptography.MD5CryptoServiceProvi der
Dim result() As Byte = oMD5.ComputeHash(unicodeText)

Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To CType(result.Length - 1, Integer)
sb.Append(result(i).ToString("X").PadLeft(2, "0"))
Next

sCRC = sb.ToString
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:eB**************@TK2MSFTNGP11.phx.gbl...
Ah, this is the hex encoding thing I mentioned in my first post and didn't
provide an example for. You can either use the BitConverter class to
convert the byte[] to hex digits and then strip out the - characters it

puts
in between or using something like my function called ConvertToOctetString that you can do a Google groups search for that does this. It basically
just uses a StringBuilder and the X2 format code to loop over the bytes

and
build the string.

Also, MAKE SURE that the vendor is calculating the MD5 of the data using

the
same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your input
byte array may be different and thus your hash will be different.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
Joe, I'm connecting an online store to the payment system. I need to pass this string to this payment gateway, and it will return me another string back, confirming that the payment was successful.

I contacted the techsupport of this gateway. They said that I don't have to
convert my string to Base64. I need to convert every byte to 16-bit number or something like this. They don't use ASP.Net, so couldn't give me an

exact
code. They said that it should look approximately like this:

StringBuilder sb = new StringBuilder();

for (int i=0;i<hash.Length;i++)
sb.Append(hash[i].ToString("x").PadLeft(2,'0'));

return sb.ToString();

How it should look in VB.Net?

Thank you,

Peter
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote in message news:OJ****************@tk2msftngp13.phx.gbl...
> What is giving you a "bad CRC" error? Is it the code below? That looks > like it should just return a base64 encoded MD5 hash of whatever string was
> provided.
>
> It isn't clear to me what you are trying to do or what the input is
in
the
> funtion.
>
> Joe K.
>
> "Peter Afonin" <pv*@speakeasy.net> wrote in message
> news:eQ****************@TK2MSFTNGP12.phx.gbl...
> > Thank you, Joe.
> >
> > I've tried to change it like this:
> >
> > Dim hashedBytes As Byte()
> > Dim md5 As New MD5CryptoServiceProvider
> > Dim encoder As new UTF8Encoding
> > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
> > Me.crc.Value = sCRC
> >
> > Yes, the output string has changed:
> >
> > '<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+
> >
> > But I'm still getting a "bad crc" error.
> >
> > Peter
> >
> > "Joe Kaplan (MVP - ADSI)"
<jo*************@removethis.accenture.com> wrote
> > in message news:uo****************@TK2MSFTNGP09.phx.gbl...
> > > Why don't you try Convert.ToBase64String instead? Typically, you encode
> > > binary data as a string with either Base64 or hex string encoding. > > >
> > > Also, be careful about using ASCII encoding to convert the input
string
> to
> > > binary. If it includes any non-ASCII characters, you'll be

throwing > data
> > > away. UTF8 is safer. Whatever you do, make sure you alway compute the
> > hash
> > > the same way if you are going to be using it for a comparison.
> > >
> > > HTH,
> > >
> > > Joe K.
> > >
> > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > > > Hello,
> > > >
> > > > I'm struggling with the string conversion to MD5 which I've never user
> > > > before.
> > > >
> > > > I have a string that I need to encode which looks approximately like
> > this:
> > > >
> > > >
> >
"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang > > > >
> > >
> >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan= > > > > BU:shp_Term=2"
> > > >
> > > > I'm doing it this way:
> > > >
> > > > Dim hashedBytes As Byte()
> > > > Dim md5 As New MD5CryptoServiceProvider
> > > > Dim encoder As New ASCIIEncoding
> > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > Dim sNewCRC as String =
Convert.ToString(md5.ComputeHash(hashedBytes))
> > > >
> > > > It doesn't work. When I see the output on the page where I
pass this
> > > string,
> > > > it looks like this:
> > > >
> > > > '<input type=hidden name=crc value="System.Byte[]">'+
> > > >
> > > > I don't know exactly how it should look like, but probably not

like
> > > > "System.Byte[]"
> > > >
> > > > I'm doing something wrong, but I don't know what.
> > > >
> > > > I would really appreciate your help.
> > > >
> > > > Thank you,
> > > >
> > > > Peter Afonin
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #11
Thank you, Joe.

This makes sense. However, I don't know what function is used by the payment
gateway provider. I will contact them with all this information.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:e6**************@TK2MSFTNGP15.phx.gbl...
There are two main things to keep in mind here:

The value of sCRC is what is being used to create the hash.
The hash is being computed based on a Unicode encoding of the same hash.

Thus, for someone else to recreate the MD5 hash of the data from the same
source data, they need to use the exact same sCRC as input and must use
Unicode encoding. UTF8 or any other encoding will produce a different byte array and thus a different hash.

If the input string and the encoding is the same, the MD5 should be the
same. The only thing that might vary is if they are assuming the bytes are in the opposite order and you need to reverse the string.

The code you keep showing below is hard to follow because it is using a
variable called sCRC as the input and then also setting that to the output. We can't tell where the data came from or where it is going.

Can you post a function that calculates the MD5 of an input string using the proper encoding and returns it as a hex string?

Joe K.

"Peter Afonin" <pe***@gudzon.net> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Hello Joe,

I found a code that should do exactly the same as in the example in my
previous message, but still doing something wrong, because the payment
gateway gives me a message that the string is bad. There is a chance that
the code itself is OK, but the data I put in is bad. But do you see anything
wrong with this code? I would appreciate your comments very much. Peter.

Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()

Dim unicodeText() As Byte
unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC)

enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
0, True)

Dim oMD5 As New System.Security.Cryptography.MD5CryptoServiceProvi der Dim result() As Byte = oMD5.ComputeHash(unicodeText)

Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To CType(result.Length - 1, Integer)
sb.Append(result(i).ToString("X").PadLeft(2, "0"))
Next

sCRC = sb.ToString
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote in message news:eB**************@TK2MSFTNGP11.phx.gbl...
Ah, this is the hex encoding thing I mentioned in my first post and didn't provide an example for. You can either use the BitConverter class to
convert the byte[] to hex digits and then strip out the - characters it puts
in between or using something like my function called ConvertToOctetString that you can do a Google groups search for that does this. It
basically just uses a StringBuilder and the X2 format code to loop over the bytes
and
build the string.

Also, MAKE SURE that the vendor is calculating the MD5 of the data
using the
same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your

input byte array may be different and thus your hash will be different.

HTH,

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:ut**************@TK2MSFTNGP10.phx.gbl...
> Joe, I'm connecting an online store to the payment system. I need to

pass
> this string to this payment gateway, and it will return me another

string
> back, confirming that the payment was successful.
>
> I contacted the techsupport of this gateway. They said that I don't

have to
> convert my string to Base64. I need to convert every byte to 16-bit

number
> or something like this. They don't use ASP.Net, so couldn't give me an exact
> code. They said that it should look approximately like this:
>
> StringBuilder sb = new StringBuilder();
>
> for (int i=0;i<hash.Length;i++)
> sb.Append(hash[i].ToString("x").PadLeft(2,'0'));
>
> return sb.ToString();
>
> How it should look in VB.Net?
>
> Thank you,
>
> Peter
>
>
> "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com>

wrote
> in message news:OJ****************@tk2msftngp13.phx.gbl...
> > What is giving you a "bad CRC" error? Is it the code below? That

looks
> > like it should just return a base64 encoded MD5 hash of whatever

string
> was
> > provided.
> >
> > It isn't clear to me what you are trying to do or what the input is in
the
> > funtion.
> >
> > Joe K.
> >
> > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > news:eQ****************@TK2MSFTNGP12.phx.gbl...
> > > Thank you, Joe.
> > >
> > > I've tried to change it like this:
> > >
> > > Dim hashedBytes As Byte()
> > > Dim md5 As New MD5CryptoServiceProvider
> > > Dim encoder As new UTF8Encoding
> > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
> > > Me.crc.Value = sCRC
> > >
> > > Yes, the output string has changed:
> > >
> > > '<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+
> > >
> > > But I'm still getting a "bad crc" error.
> > >
> > > Peter
> > >
> > > "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> > wrote
> > > in message news:uo****************@TK2MSFTNGP09.phx.gbl...
> > > > Why don't you try Convert.ToBase64String instead? Typically, you > encode
> > > > binary data as a string with either Base64 or hex string encoding. > > > >
> > > > Also, be careful about using ASCII encoding to convert the
input > string
> > to
> > > > binary. If it includes any non-ASCII characters, you'll be

throwing
> > data
> > > > away. UTF8 is safer. Whatever you do, make sure you alway

compute
> the
> > > hash
> > > > the same way if you are going to be using it for a comparison.
> > > >
> > > > HTH,
> > > >
> > > > Joe K.
> > > >
> > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > > > > Hello,
> > > > >
> > > > > I'm struggling with the string conversion to MD5 which I've

never
> user
> > > > > before.
> > > > >
> > > > > I have a string that I need to encode which looks

approximately like
> > > this:
> > > > >
> > > > >
> > >
>

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang
> > > > >
> > > >
> > >
> >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> > > > > BU:shp_Term=2"
> > > > >
> > > > > I'm doing it this way:
> > > > >
> > > > > Dim hashedBytes As Byte()
> > > > > Dim md5 As New MD5CryptoServiceProvider
> > > > > Dim encoder As New ASCIIEncoding
> > > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > > Dim sNewCRC as String =
> Convert.ToString(md5.ComputeHash(hashedBytes))
> > > > >
> > > > > It doesn't work. When I see the output on the page where I pass this
> > > > string,
> > > > > it looks like this:
> > > > >
> > > > > '<input type=hidden name=crc value="System.Byte[]">'+
> > > > >
> > > > > I don't know exactly how it should look like, but probably not like
> > > > > "System.Byte[]"
> > > > >
> > > > > I'm doing something wrong, but I don't know what.
> > > > >
> > > > > I would really appreciate your help.
> > > > >
> > > > > Thank you,
> > > > >
> > > > > Peter Afonin
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #12
My idea was that you would have a function that calculates the MD5 so that
we could see more clearly exactly how you are calculating it. After that,
you can verify with the vendor that they are using the same algorithm. The
function would look like:

Public Function GetMD5(ByVal inputData As String) as String
....
End Function

Then, we could see what results you are getting by passing in your input
data.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:uW****************@tk2msftngp13.phx.gbl...
Thank you, Joe.

This makes sense. However, I don't know what function is used by the payment gateway provider. I will contact them with all this information.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:e6**************@TK2MSFTNGP15.phx.gbl...
There are two main things to keep in mind here:

The value of sCRC is what is being used to create the hash.
The hash is being computed based on a Unicode encoding of the same hash.

Thus, for someone else to recreate the MD5 hash of the data from the same
source data, they need to use the exact same sCRC as input and must use
Unicode encoding. UTF8 or any other encoding will produce a different byte
array and thus a different hash.

If the input string and the encoding is the same, the MD5 should be the
same. The only thing that might vary is if they are assuming the bytes

are
in the opposite order and you need to reverse the string.

The code you keep showing below is hard to follow because it is using a
variable called sCRC as the input and then also setting that to the

output.
We can't tell where the data came from or where it is going.

Can you post a function that calculates the MD5 of an input string using

the
proper encoding and returns it as a hex string?

Joe K.

"Peter Afonin" <pe***@gudzon.net> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
Hello Joe,

I found a code that should do exactly the same as in the example in my
previous message, but still doing something wrong, because the payment
gateway gives me a message that the string is bad. There is a chance that the code itself is OK, but the data I put in is bad. But do you see

anything
wrong with this code? I would appreciate your comments very much. Peter.
Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()

Dim unicodeText() As Byte
unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC)

enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
0, True)

Dim oMD5 As New System.Security.Cryptography.MD5CryptoServiceProvi der Dim result() As Byte = oMD5.ComputeHash(unicodeText)

Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To CType(result.Length - 1, Integer)
sb.Append(result(i).ToString("X").PadLeft(2, "0"))
Next

sCRC = sb.ToString
"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote in message news:eB**************@TK2MSFTNGP11.phx.gbl...
> Ah, this is the hex encoding thing I mentioned in my first post and

didn't
> provide an example for. You can either use the BitConverter class to > convert the byte[] to hex digits and then strip out the - characters it puts
> in between or using something like my function called

ConvertToOctetString
> that you can do a Google groups search for that does this. It basically > just uses a StringBuilder and the X2 format code to loop over the bytes and
> build the string.
>
> Also, MAKE SURE that the vendor is calculating the MD5 of the data using the
> same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your input > byte array may be different and thus your hash will be different.
>
> HTH,
>
> Joe K.
>
> "Peter Afonin" <pv*@speakeasy.net> wrote in message
> news:ut**************@TK2MSFTNGP10.phx.gbl...
> > Joe, I'm connecting an online store to the payment system. I need to pass
> > this string to this payment gateway, and it will return me another
string
> > back, confirming that the payment was successful.
> >
> > I contacted the techsupport of this gateway. They said that I don't have
> to
> > convert my string to Base64. I need to convert every byte to
16-bit number
> > or something like this. They don't use ASP.Net, so couldn't give me an > exact
> > code. They said that it should look approximately like this:
> >
> > StringBuilder sb = new StringBuilder();
> >
> > for (int i=0;i<hash.Length;i++)
> >
sb.Append(hash[i].ToString("x").PadLeft(2,'0')); > >
> > return sb.ToString();
> >
> > How it should look in VB.Net?
> >
> > Thank you,
> >
> > Peter
> >
> >
> > "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
> > in message news:OJ****************@tk2msftngp13.phx.gbl...
> > > What is giving you a "bad CRC" error? Is it the code below? That looks
> > > like it should just return a base64 encoded MD5 hash of whatever
string
> > was
> > > provided.
> > >
> > > It isn't clear to me what you are trying to do or what the input is
in
> the
> > > funtion.
> > >
> > > Joe K.
> > >
> > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > news:eQ****************@TK2MSFTNGP12.phx.gbl...
> > > > Thank you, Joe.
> > > >
> > > > I've tried to change it like this:
> > > >
> > > > Dim hashedBytes As Byte()
> > > > Dim md5 As New MD5CryptoServiceProvider
> > > > Dim encoder As new UTF8Encoding
> > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
> > > > Me.crc.Value = sCRC
> > > >
> > > > Yes, the output string has changed:
> > > >
> > > > '<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+ > > > >
> > > > But I'm still getting a "bad crc" error.
> > > >
> > > > Peter
> > > >
> > > > "Joe Kaplan (MVP - ADSI)"

<jo*************@removethis.accenture.com>
> > wrote
> > > > in message news:uo****************@TK2MSFTNGP09.phx.gbl...
> > > > > Why don't you try Convert.ToBase64String instead? Typically, you
> > encode
> > > > > binary data as a string with either Base64 or hex string

encoding.
> > > > >
> > > > > Also, be careful about using ASCII encoding to convert the

input > > string
> > > to
> > > > > binary. If it includes any non-ASCII characters, you'll be
throwing
> > > data
> > > > > away. UTF8 is safer. Whatever you do, make sure you alway
compute
> > the
> > > > hash
> > > > > the same way if you are going to be using it for a
comparison. > > > > >
> > > > > HTH,
> > > > >
> > > > > Joe K.
> > > > >
> > > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > > news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > > > > > Hello,
> > > > > >
> > > > > > I'm struggling with the string conversion to MD5 which I've never
> > user
> > > > > > before.
> > > > > >
> > > > > > I have a string that I need to encode which looks

approximately
> like
> > > > this:
> > > > > >
> > > > > >
> > > >
> >

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang > > > > > >
> > > > >
> > > >
> > >
> >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> > > > > > BU:shp_Term=2"
> > > > > >
> > > > > > I'm doing it this way:
> > > > > >
> > > > > > Dim hashedBytes As Byte()
> > > > > > Dim md5 As New MD5CryptoServiceProvider
> > > > > > Dim encoder As New ASCIIEncoding
> > > > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > > > Dim sNewCRC as String =
> > Convert.ToString(md5.ComputeHash(hashedBytes))
> > > > > >
> > > > > > It doesn't work. When I see the output on the page where I

pass
> this
> > > > > string,
> > > > > > it looks like this:
> > > > > >
> > > > > > '<input type=hidden name=crc value="System.Byte[]">'+
> > > > > >
> > > > > > I don't know exactly how it should look like, but probably not > like
> > > > > > "System.Byte[]"
> > > > > >
> > > > > > I'm doing something wrong, but I don't know what.
> > > > > >
> > > > > > I would really appreciate your help.
> > > > > >
> > > > > > Thank you,
> > > > > >
> > > > > > Peter Afonin
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #13
Thank you, Joe.

I'll put here the whole function, I don't know if it would make sense to
you. I'm actually doing it on the Pag_Load event:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim sUser As String
Dim dblSum As Double
Dim sPlan As String
Dim sTerm As String
Dim dblPrice As Double
Dim sCRC As String
If Not IsPostBack Then
sUser = Request.QueryString("user")
dblSum = Request.QueryString("sum")
dblPrice = Request.QueryString("price")
sPlan = Request.QueryString("plan")
sTerm = Request.QueryString("term")
Dim sEmail = Request.QueryString("email")
Dim unicodeString As String
If Request.QueryString("up") = 1 Then
Me.shp_Up.Value = "1"
unicodeString = "Upgrade to" & sPlan
Else
unicodeString = "Extension to " & sPlan & " for " & sTerm & " months"
Me.shp_Up.Value = "0"
End If
If Not IsDBNull(sUser) Then
If Me.shp_Up.Value = "0" Then
inv_desc.Value = "Hosting " & sPlan & " extension for " & sTerm & " months"
Else
inv_desc.Value = "Hosting upgrade to plan " & sPlan
End If
Me.shp_UserID.Value = sUser.ToString
Me.Description.Value = unicodeString
Me.shp_Price.Value = dblPrice.ToString
End If
If Not IsDBNull(dblSum) Then
out_summ.Value = dblSum.ToString("c")
End If
Me.shp_HostPlan.Value = sPlan
inv_id.Value = CLng(Format(Now, "HHmmss"))
Me.shp_PaymentNo.Value = CLng(Format(Now, "yyyyMMddHHmmss"))
If Not IsDBNull(sTerm) Then
Me.shp_Term.Value = sTerm
End If
If Not IsDBNull("email") Then
Me.shp_Email.Value = sEmail
End If
sCRC = "pva:" & dblSum.ToString & ":" & CLng(Format(Now, "HHmmss")) _
& ":pa0567Ztro:inv_desc=" & inv_desc.Value & ":shp_Email=" _
& sEmail & ":lang=ru" & ":shp_PaymentNo=" _
& CLng(Format(Now, "yyyyMMddHHmmss")).ToString & ":shp_UserID=" & sUser _
& ":shp_Price=" & dblPrice.ToString _
& ":shp_HostPlan=" & sPlan & ":shp_Term=" _
& sTerm
Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()
Dim unicodeText() As Byte
unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC)
enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
0, True)
Dim oMD5 As New System.Security.Cryptography.MD5CryptoServiceProvi der
Dim result() As Byte = oMD5.ComputeHash(unicodeText)
Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To CType(result.Length - 1, Integer)
sb.Append(result(i).ToString("X").PadLeft(2, "0"))
Next
sCRC = sb.ToString

sCRC = Convert.ToBase64String(MD5.ComputeHash(hashedBytes ))
Me.crc.Value = sCRC
End If
Catch ex As Exception
lblError.Text = ex.Message
Finally
End Try
End Sub

The hidden textbox crc gets the value of sCRC, that is passed to the Payment
Gateway when the form is submitted.

Thanks,

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:#j**************@TK2MSFTNGP10.phx.gbl...
My idea was that you would have a function that calculates the MD5 so that
we could see more clearly exactly how you are calculating it. After that,
you can verify with the vendor that they are using the same algorithm. The function would look like:

Public Function GetMD5(ByVal inputData As String) as String
...
End Function

Then, we could see what results you are getting by passing in your input
data.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:uW****************@tk2msftngp13.phx.gbl...
Thank you, Joe.

This makes sense. However, I don't know what function is used by the payment
gateway provider. I will contact them with all this information.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:e6**************@TK2MSFTNGP15.phx.gbl...
There are two main things to keep in mind here:

The value of sCRC is what is being used to create the hash.
The hash is being computed based on a Unicode encoding of the same hash.
Thus, for someone else to recreate the MD5 hash of the data from the same source data, they need to use the exact same sCRC as input and must use Unicode encoding. UTF8 or any other encoding will produce a different

byte
array and thus a different hash.

If the input string and the encoding is the same, the MD5 should be the same. The only thing that might vary is if they are assuming the bytes are
in the opposite order and you need to reverse the string.

The code you keep showing below is hard to follow because it is using
a variable called sCRC as the input and then also setting that to the

output.
We can't tell where the data came from or where it is going.

Can you post a function that calculates the MD5 of an input string using
the
proper encoding and returns it as a hex string?

Joe K.

"Peter Afonin" <pe***@gudzon.net> wrote in message
news:uJ**************@TK2MSFTNGP11.phx.gbl...
> Hello Joe,
>
> I found a code that should do exactly the same as in the example in
my > previous message, but still doing something wrong, because the payment > gateway gives me a message that the string is bad. There is a chance

that
> the code itself is OK, but the data I put in is bad. But do you see
anything
> wrong with this code? I would appreciate your comments very much.

Peter. >
> Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()
>
> Dim unicodeText() As Byte
> unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC)
>
> enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
> 0, True)
>
> Dim oMD5 As New

System.Security.Cryptography.MD5CryptoServiceProvi der
> Dim result() As Byte = oMD5.ComputeHash(unicodeText)
>
> Dim sb As New StringBuilder
> Dim i As Integer = 0
> For i = 0 To CType(result.Length - 1, Integer)
> sb.Append(result(i).ToString("X").PadLeft(2, "0"))
> Next
>
> sCRC = sb.ToString
>
>
> "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com>

wrote
> in message news:eB**************@TK2MSFTNGP11.phx.gbl...
> > Ah, this is the hex encoding thing I mentioned in my first post and didn't
> > provide an example for. You can either use the BitConverter class to > > convert the byte[] to hex digits and then strip out the - characters it
> puts
> > in between or using something like my function called
ConvertToOctetString
> > that you can do a Google groups search for that does this. It

basically
> > just uses a StringBuilder and the X2 format code to loop over the

bytes
> and
> > build the string.
> >
> > Also, MAKE SURE that the vendor is calculating the MD5 of the data

using
> the
> > same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your

input
> > byte array may be different and thus your hash will be different.
> >
> > HTH,
> >
> > Joe K.
> >
> > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > news:ut**************@TK2MSFTNGP10.phx.gbl...
> > > Joe, I'm connecting an online store to the payment system. I
need to > pass
> > > this string to this payment gateway, and it will return me
another > string
> > > back, confirming that the payment was successful.
> > >
> > > I contacted the techsupport of this gateway. They said that I don't have
> > to
> > > convert my string to Base64. I need to convert every byte to 16-bit > number
> > > or something like this. They don't use ASP.Net, so couldn't give me
an
> > exact
> > > code. They said that it should look approximately like this:
> > >
> > > StringBuilder sb = new StringBuilder();
> > >
> > > for (int i=0;i<hash.Length;i++)
> > >

sb.Append(hash[i].ToString("x").PadLeft(2,'0')); > > >
> > > return sb.ToString();
> > >
> > > How it should look in VB.Net?
> > >
> > > Thank you,
> > >
> > > Peter
> > >
> > >
> > > "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> > wrote
> > > in message news:OJ****************@tk2msftngp13.phx.gbl...
> > > > What is giving you a "bad CRC" error? Is it the code below? That > looks
> > > > like it should just return a base64 encoded MD5 hash of whatever > string
> > > was
> > > > provided.
> > > >
> > > > It isn't clear to me what you are trying to do or what the input is
in
> > the
> > > > funtion.
> > > >
> > > > Joe K.
> > > >
> > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > news:eQ****************@TK2MSFTNGP12.phx.gbl...
> > > > > Thank you, Joe.
> > > > >
> > > > > I've tried to change it like this:
> > > > >
> > > > > Dim hashedBytes As Byte()
> > > > > Dim md5 As New MD5CryptoServiceProvider
> > > > > Dim encoder As new UTF8Encoding
> > > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > > sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes ))
> > > > > Me.crc.Value = sCRC
> > > > >
> > > > > Yes, the output string has changed:
> > > > >
> > > > > '<input type=hidden name=crc value="35r0XmeFIOXs5evTQM0q+w==">'+ > > > > >
> > > > > But I'm still getting a "bad crc" error.
> > > > >
> > > > > Peter
> > > > >
> > > > > "Joe Kaplan (MVP - ADSI)"
<jo*************@removethis.accenture.com>
> > > wrote
> > > > > in message news:uo****************@TK2MSFTNGP09.phx.gbl...
> > > > > > Why don't you try Convert.ToBase64String instead? Typically, you
> > > encode
> > > > > > binary data as a string with either Base64 or hex string
encoding.
> > > > > >
> > > > > > Also, be careful about using ASCII encoding to convert the

input
> > > string
> > > > to
> > > > > > binary. If it includes any non-ASCII characters, you'll
be > throwing
> > > > data
> > > > > > away. UTF8 is safer. Whatever you do, make sure you alway > compute
> > > the
> > > > > hash
> > > > > > the same way if you are going to be using it for a comparison. > > > > > >
> > > > > > HTH,
> > > > > >
> > > > > > Joe K.
> > > > > >
> > > > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > > > news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > > > > > > Hello,
> > > > > > >
> > > > > > > I'm struggling with the string conversion to MD5 which I've > never
> > > user
> > > > > > > before.
> > > > > > >
> > > > > > > I have a string that I need to encode which looks
approximately
> > like
> > > > > this:
> > > > > > >
> > > > > > >
> > > > >
> > >
>

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> > > > > > > BU:shp_Term=2"
> > > > > > >
> > > > > > > I'm doing it this way:
> > > > > > >
> > > > > > > Dim hashedBytes As Byte()
> > > > > > > Dim md5 As New MD5CryptoServiceProvider
> > > > > > > Dim encoder As New ASCIIEncoding
> > > > > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > > > > Dim sNewCRC as String =
> > > Convert.ToString(md5.ComputeHash(hashedBytes))
> > > > > > >
> > > > > > > It doesn't work. When I see the output on the page where I pass
> > this
> > > > > > string,
> > > > > > > it looks like this:
> > > > > > >
> > > > > > > '<input type=hidden name=crc value="System.Byte[]">'+
> > > > > > >
> > > > > > > I don't know exactly how it should look like, but

probably not
> > like
> > > > > > > "System.Byte[]"
> > > > > > >
> > > > > > > I'm doing something wrong, but I don't know what.
> > > > > > >
> > > > > > > I would really appreciate your help.
> > > > > > >
> > > > > > > Thank you,
> > > > > > >
> > > > > > > Peter Afonin
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #14
Ok, I was thinking of a function kind of like this:

Private Function ComputeMD5Hash(ByVal input As String, ByVal
targetEncoding As Encoding) As String

Dim textData() As Byte
Dim hexString As String
textData = targetEncoding.GetBytes(input)

Dim hashProvider As New
System.Security.Cryptography.MD5CryptoServiceProvi der
Dim md5Hash() As Byte = hashProvider.ComputeHash(textData)

Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To md5Hash.Length - 1
sb.Append(md5Hash(i).ToString("X2"))
Next
hexString = sb.ToString

Return hexString

End Function

You would call it in your code with:

hashText = ComputeMD5Hash(sCRC, Encoding.Unicode)

That would then leave three possibilities as to why you aren't getting the
same results as the vendor:
- The string you are passing in to sCRC is different from what they are
passing in
- You are using a different encoding to get the byte array than they are
- The output format you are getting is different (due to the bytes being
reversed or case sensitive or something)

My suspicion is that it is the encoding piece. You could also try
Encoding.ASCII and Encoding.UTF8 to see if those give you the result you are
looking for. Unicode will produce a totally different byte array than ASCII
and UTF8, so the hash will be different as a result.

Hopefully that will help you resolve it.

Joe K.

"Peter Afonin" <pe***@gudzon.net> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Thank you, Joe.

I'll put here the whole function, I don't know if it would make sense to
you. I'm actually doing it on the Pag_Load event:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim sUser As String
Dim dblSum As Double
Dim sPlan As String
Dim sTerm As String
Dim dblPrice As Double
Dim sCRC As String
If Not IsPostBack Then
sUser = Request.QueryString("user")
dblSum = Request.QueryString("sum")
dblPrice = Request.QueryString("price")
sPlan = Request.QueryString("plan")
sTerm = Request.QueryString("term")
Dim sEmail = Request.QueryString("email")
Dim unicodeString As String
If Request.QueryString("up") = 1 Then
Me.shp_Up.Value = "1"
unicodeString = "Upgrade to" & sPlan
Else
unicodeString = "Extension to " & sPlan & " for " & sTerm & " months"
Me.shp_Up.Value = "0"
End If
If Not IsDBNull(sUser) Then
If Me.shp_Up.Value = "0" Then
inv_desc.Value = "Hosting " & sPlan & " extension for " & sTerm & " months" Else
inv_desc.Value = "Hosting upgrade to plan " & sPlan
End If
Me.shp_UserID.Value = sUser.ToString
Me.Description.Value = unicodeString
Me.shp_Price.Value = dblPrice.ToString
End If
If Not IsDBNull(dblSum) Then
out_summ.Value = dblSum.ToString("c")
End If
Me.shp_HostPlan.Value = sPlan
inv_id.Value = CLng(Format(Now, "HHmmss"))
Me.shp_PaymentNo.Value = CLng(Format(Now, "yyyyMMddHHmmss"))
If Not IsDBNull(sTerm) Then
Me.shp_Term.Value = sTerm
End If
If Not IsDBNull("email") Then
Me.shp_Email.Value = sEmail
End If
sCRC = "pva:" & dblSum.ToString & ":" & CLng(Format(Now, "HHmmss")) _
& ":pa0567Ztro:inv_desc=" & inv_desc.Value & ":shp_Email=" _
& sEmail & ":lang=ru" & ":shp_PaymentNo=" _
& CLng(Format(Now, "yyyyMMddHHmmss")).ToString & ":shp_UserID=" & sUser _
& ":shp_Price=" & dblPrice.ToString _
& ":shp_HostPlan=" & sPlan & ":shp_Term=" _
& sTerm
Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()
Dim unicodeText() As Byte
unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC)
enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
0, True)
Dim oMD5 As New System.Security.Cryptography.MD5CryptoServiceProvi der
Dim result() As Byte = oMD5.ComputeHash(unicodeText)
Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To CType(result.Length - 1, Integer)
sb.Append(result(i).ToString("X").PadLeft(2, "0"))
Next
sCRC = sb.ToString

sCRC = Convert.ToBase64String(MD5.ComputeHash(hashedBytes ))
Me.crc.Value = sCRC
End If
Catch ex As Exception
lblError.Text = ex.Message
Finally
End Try
End Sub

The hidden textbox crc gets the value of sCRC, that is passed to the Payment Gateway when the form is submitted.

Thanks,

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:#j**************@TK2MSFTNGP10.phx.gbl...
My idea was that you would have a function that calculates the MD5 so that
we could see more clearly exactly how you are calculating it. After that, you can verify with the vendor that they are using the same algorithm. The
function would look like:

Public Function GetMD5(ByVal inputData As String) as String
...
End Function

Then, we could see what results you are getting by passing in your input
data.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:uW****************@tk2msftngp13.phx.gbl...
Thank you, Joe.

This makes sense. However, I don't know what function is used by the

payment
gateway provider. I will contact them with all this information.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote in message news:e6**************@TK2MSFTNGP15.phx.gbl...
> There are two main things to keep in mind here:
>
> The value of sCRC is what is being used to create the hash.
> The hash is being computed based on a Unicode encoding of the same hash. >
> Thus, for someone else to recreate the MD5 hash of the data from the

same
> source data, they need to use the exact same sCRC as input and must use > Unicode encoding. UTF8 or any other encoding will produce a different byte
> array and thus a different hash.
>
> If the input string and the encoding is the same, the MD5 should be the > same. The only thing that might vary is if they are assuming the bytes are
> in the opposite order and you need to reverse the string.
>
> The code you keep showing below is hard to follow because it is using a
> variable called sCRC as the input and then also setting that to the
output.
> We can't tell where the data came from or where it is going.
>
> Can you post a function that calculates the MD5 of an input string using the
> proper encoding and returns it as a hex string?
>
> Joe K.
>
> "Peter Afonin" <pe***@gudzon.net> wrote in message
> news:uJ**************@TK2MSFTNGP11.phx.gbl...
> > Hello Joe,
> >
> > I found a code that should do exactly the same as in the example
in
my > > previous message, but still doing something wrong, because the payment > > gateway gives me a message that the string is bad. There is a
chance that
> > the code itself is OK, but the data I put in is bad. But do you see > anything
> > wrong with this code? I would appreciate your comments very much. Peter.
> >
> > Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()
> >
> > Dim unicodeText() As Byte
> > unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC) > >
> > enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _ > > 0, True)
> >
> > Dim oMD5 As New
System.Security.Cryptography.MD5CryptoServiceProvi der
> > Dim result() As Byte = oMD5.ComputeHash(unicodeText)
> >
> > Dim sb As New StringBuilder
> > Dim i As Integer = 0
> > For i = 0 To CType(result.Length - 1, Integer)
> > sb.Append(result(i).ToString("X").PadLeft(2, "0"))
> > Next
> >
> > sCRC = sb.ToString
> >
> >
> > "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
> > in message news:eB**************@TK2MSFTNGP11.phx.gbl...
> > > Ah, this is the hex encoding thing I mentioned in my first post

and > didn't
> > > provide an example for. You can either use the BitConverter class to
> > > convert the byte[] to hex digits and then strip out the - characters it
> > puts
> > > in between or using something like my function called
> ConvertToOctetString
> > > that you can do a Google groups search for that does this. It
basically
> > > just uses a StringBuilder and the X2 format code to loop over
the bytes
> > and
> > > build the string.
> > >
> > > Also, MAKE SURE that the vendor is calculating the MD5 of the data using
> > the
> > > same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your input
> > > byte array may be different and thus your hash will be different. > > >
> > > HTH,
> > >
> > > Joe K.
> > >
> > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > news:ut**************@TK2MSFTNGP10.phx.gbl...
> > > > Joe, I'm connecting an online store to the payment system. I need
to
> > pass
> > > > this string to this payment gateway, and it will return me

another > > string
> > > > back, confirming that the payment was successful.
> > > >
> > > > I contacted the techsupport of this gateway. They said that I

don't
> have
> > > to
> > > > convert my string to Base64. I need to convert every byte to

16-bit
> > number
> > > > or something like this. They don't use ASP.Net, so couldn't give me
an
> > > exact
> > > > code. They said that it should look approximately like this:
> > > >
> > > > StringBuilder sb = new StringBuilder();
> > > >
> > > > for (int i=0;i<hash.Length;i++)
> > > >

sb.Append(hash[i].ToString("x").PadLeft(2,'0'));
> > > >
> > > > return sb.ToString();
> > > >
> > > > How it should look in VB.Net?
> > > >
> > > > Thank you,
> > > >
> > > > Peter
> > > >
> > > >
> > > > "Joe Kaplan (MVP - ADSI)"

<jo*************@removethis.accenture.com>
> > wrote
> > > > in message news:OJ****************@tk2msftngp13.phx.gbl...
> > > > > What is giving you a "bad CRC" error? Is it the code below?

That
> > looks
> > > > > like it should just return a base64 encoded MD5 hash of whatever > > string
> > > > was
> > > > > provided.
> > > > >
> > > > > It isn't clear to me what you are trying to do or what the input is
> in
> > > the
> > > > > funtion.
> > > > >
> > > > > Joe K.
> > > > >
> > > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > > news:eQ****************@TK2MSFTNGP12.phx.gbl...
> > > > > > Thank you, Joe.
> > > > > >
> > > > > > I've tried to change it like this:
> > > > > >
> > > > > > Dim hashedBytes As Byte()
> > > > > > Dim md5 As New MD5CryptoServiceProvider
> > > > > > Dim encoder As new UTF8Encoding
> > > > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > > > sCRC =
Convert.ToBase64String(md5.ComputeHash(hashedBytes )) > > > > > > Me.crc.Value = sCRC
> > > > > >
> > > > > > Yes, the output string has changed:
> > > > > >
> > > > > > '<input type=hidden name=crc

value="35r0XmeFIOXs5evTQM0q+w==">'+
> > > > > >
> > > > > > But I'm still getting a "bad crc" error.
> > > > > >
> > > > > > Peter
> > > > > >
> > > > > > "Joe Kaplan (MVP - ADSI)"
> <jo*************@removethis.accenture.com>
> > > > wrote
> > > > > > in message news:uo****************@TK2MSFTNGP09.phx.gbl...
> > > > > > > Why don't you try Convert.ToBase64String instead?

Typically,
> you
> > > > encode
> > > > > > > binary data as a string with either Base64 or hex string
> encoding.
> > > > > > >
> > > > > > > Also, be careful about using ASCII encoding to convert the input
> > > > string
> > > > > to
> > > > > > > binary. If it includes any non-ASCII characters, you'll

be > > throwing
> > > > > data
> > > > > > > away. UTF8 is safer. Whatever you do, make sure you alway > > compute
> > > > the
> > > > > > hash
> > > > > > > the same way if you are going to be using it for a

comparison.
> > > > > > >
> > > > > > > HTH,
> > > > > > >
> > > > > > > Joe K.
> > > > > > >
> > > > > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > > > > news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > > > > > > > Hello,
> > > > > > > >
> > > > > > > > I'm struggling with the string conversion to MD5 which

I've
> > never
> > > > user
> > > > > > > > before.
> > > > > > > >
> > > > > > > > I have a string that I need to encode which looks
> approximately
> > > like
> > > > > > this:
> > > > > > > >
> > > > > > > >
> > > > > >
> > > >
> >
"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang > > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> > > > > > > > BU:shp_Term=2"
> > > > > > > >
> > > > > > > > I'm doing it this way:
> > > > > > > >
> > > > > > > > Dim hashedBytes As Byte()
> > > > > > > > Dim md5 As New MD5CryptoServiceProvider
> > > > > > > > Dim encoder As New ASCIIEncoding
> > > > > > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > > > > > Dim sNewCRC as String =
> > > > Convert.ToString(md5.ComputeHash(hashedBytes))
> > > > > > > >
> > > > > > > > It doesn't work. When I see the output on the page
where I > pass
> > > this
> > > > > > > string,
> > > > > > > > it looks like this:
> > > > > > > >
> > > > > > > > '<input type=hidden name=crc value="System.Byte[]">'+
> > > > > > > >
> > > > > > > > I don't know exactly how it should look like, but probably not
> > > like
> > > > > > > > "System.Byte[]"
> > > > > > > >
> > > > > > > > I'm doing something wrong, but I don't know what.
> > > > > > > >
> > > > > > > > I would really appreciate your help.
> > > > > > > >
> > > > > > > > Thank you,
> > > > > > > >
> > > > > > > > Peter Afonin
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #15
Thank you very much, Joe. I'll keep trying.

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote
in message news:OG**************@TK2MSFTNGP12.phx.gbl...
Ok, I was thinking of a function kind of like this:

Private Function ComputeMD5Hash(ByVal input As String, ByVal
targetEncoding As Encoding) As String

Dim textData() As Byte
Dim hexString As String
textData = targetEncoding.GetBytes(input)

Dim hashProvider As New
System.Security.Cryptography.MD5CryptoServiceProvi der
Dim md5Hash() As Byte = hashProvider.ComputeHash(textData)

Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To md5Hash.Length - 1
sb.Append(md5Hash(i).ToString("X2"))
Next
hexString = sb.ToString

Return hexString

End Function

You would call it in your code with:

hashText = ComputeMD5Hash(sCRC, Encoding.Unicode)

That would then leave three possibilities as to why you aren't getting the
same results as the vendor:
- The string you are passing in to sCRC is different from what they are
passing in
- You are using a different encoding to get the byte array than they are
- The output format you are getting is different (due to the bytes being
reversed or case sensitive or something)

My suspicion is that it is the encoding piece. You could also try
Encoding.ASCII and Encoding.UTF8 to see if those give you the result you are looking for. Unicode will produce a totally different byte array than ASCII and UTF8, so the hash will be different as a result.

Hopefully that will help you resolve it.

Joe K.

"Peter Afonin" <pe***@gudzon.net> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...
Thank you, Joe.

I'll put here the whole function, I don't know if it would make sense to
you. I'm actually doing it on the Pag_Load event:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Dim sUser As String
Dim dblSum As Double
Dim sPlan As String
Dim sTerm As String
Dim dblPrice As Double
Dim sCRC As String
If Not IsPostBack Then
sUser = Request.QueryString("user")
dblSum = Request.QueryString("sum")
dblPrice = Request.QueryString("price")
sPlan = Request.QueryString("plan")
sTerm = Request.QueryString("term")
Dim sEmail = Request.QueryString("email")
Dim unicodeString As String
If Request.QueryString("up") = 1 Then
Me.shp_Up.Value = "1"
unicodeString = "Upgrade to" & sPlan
Else
unicodeString = "Extension to " & sPlan & " for " & sTerm & " months"
Me.shp_Up.Value = "0"
End If
If Not IsDBNull(sUser) Then
If Me.shp_Up.Value = "0" Then
inv_desc.Value = "Hosting " & sPlan & " extension for " & sTerm & " months"
Else
inv_desc.Value = "Hosting upgrade to plan " & sPlan
End If
Me.shp_UserID.Value = sUser.ToString
Me.Description.Value = unicodeString
Me.shp_Price.Value = dblPrice.ToString
End If
If Not IsDBNull(dblSum) Then
out_summ.Value = dblSum.ToString("c")
End If
Me.shp_HostPlan.Value = sPlan
inv_id.Value = CLng(Format(Now, "HHmmss"))
Me.shp_PaymentNo.Value = CLng(Format(Now, "yyyyMMddHHmmss"))
If Not IsDBNull(sTerm) Then
Me.shp_Term.Value = sTerm
End If
If Not IsDBNull("email") Then
Me.shp_Email.Value = sEmail
End If
sCRC = "pva:" & dblSum.ToString & ":" & CLng(Format(Now, "HHmmss")) _
& ":pa0567Ztro:inv_desc=" & inv_desc.Value & ":shp_Email=" _
& sEmail & ":lang=ru" & ":shp_PaymentNo=" _
& CLng(Format(Now, "yyyyMMddHHmmss")).ToString & ":shp_UserID=" & sUser _
& ":shp_Price=" & dblPrice.ToString _
& ":shp_HostPlan=" & sPlan & ":shp_Term=" _
& sTerm
Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()
Dim unicodeText() As Byte
unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC)
enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
0, True)
Dim oMD5 As New System.Security.Cryptography.MD5CryptoServiceProvi der
Dim result() As Byte = oMD5.ComputeHash(unicodeText)
Dim sb As New StringBuilder
Dim i As Integer = 0
For i = 0 To CType(result.Length - 1, Integer)
sb.Append(result(i).ToString("X").PadLeft(2, "0"))
Next
sCRC = sb.ToString

sCRC = Convert.ToBase64String(MD5.ComputeHash(hashedBytes ))
Me.crc.Value = sCRC
End If
Catch ex As Exception
lblError.Text = ex.Message
Finally
End Try
End Sub

The hidden textbox crc gets the value of sCRC, that is passed to the

Payment
Gateway when the form is submitted.

Thanks,

Peter

"Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> wrote in message news:#j**************@TK2MSFTNGP10.phx.gbl...
My idea was that you would have a function that calculates the MD5 so that we could see more clearly exactly how you are calculating it. After that, you can verify with the vendor that they are using the same algorithm.

The
function would look like:

Public Function GetMD5(ByVal inputData As String) as String
...
End Function

Then, we could see what results you are getting by passing in your input data.

Joe K.

"Peter Afonin" <pv*@speakeasy.net> wrote in message
news:uW****************@tk2msftngp13.phx.gbl...
> Thank you, Joe.
>
> This makes sense. However, I don't know what function is used by the
payment
> gateway provider. I will contact them with all this information.
>
> Peter
>
> "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com>

wrote
> in message news:e6**************@TK2MSFTNGP15.phx.gbl...
> > There are two main things to keep in mind here:
> >
> > The value of sCRC is what is being used to create the hash.
> > The hash is being computed based on a Unicode encoding of the same

hash.
> >
> > Thus, for someone else to recreate the MD5 hash of the data from the same
> > source data, they need to use the exact same sCRC as input and must use
> > Unicode encoding. UTF8 or any other encoding will produce a different > byte
> > array and thus a different hash.
> >
> > If the input string and the encoding is the same, the MD5 should
be
the
> > same. The only thing that might vary is if they are assuming the

bytes
> are
> > in the opposite order and you need to reverse the string.
> >
> > The code you keep showing below is hard to follow because it is using
a
> > variable called sCRC as the input and then also setting that to

the > output.
> > We can't tell where the data came from or where it is going.
> >
> > Can you post a function that calculates the MD5 of an input string

using
> the
> > proper encoding and returns it as a hex string?
> >
> > Joe K.
> >
> > "Peter Afonin" <pe***@gudzon.net> wrote in message
> > news:uJ**************@TK2MSFTNGP11.phx.gbl...
> > > Hello Joe,
> > >
> > > I found a code that should do exactly the same as in the example

in
my
> > > previous message, but still doing something wrong, because the

payment
> > > gateway gives me a message that the string is bad. There is a

chance > that
> > > the code itself is OK, but the data I put in is bad. But do you see > > anything
> > > wrong with this code? I would appreciate your comments very much. Peter.
> > >
> > > Dim enc As Encoder = System.Text.Encoding.Unicode.GetEncoder()
> > >
> > > Dim unicodeText() As Byte
> > > unicodeText = System.Text.UnicodeEncoding.Unicode.GetBytes(sCRC) > > >
> > > enc.GetBytes(sCRC.ToCharArray(), 0, sCRC.Length, unicodeText, _
> > > 0, True)
> > >
> > > Dim oMD5 As New
> System.Security.Cryptography.MD5CryptoServiceProvi der
> > > Dim result() As Byte = oMD5.ComputeHash(unicodeText)
> > >
> > > Dim sb As New StringBuilder
> > > Dim i As Integer = 0
> > > For i = 0 To CType(result.Length - 1, Integer)
> > > sb.Append(result(i).ToString("X").PadLeft(2, "0"))
> > > Next
> > >
> > > sCRC = sb.ToString
> > >
> > >
> > > "Joe Kaplan (MVP - ADSI)" <jo*************@removethis.accenture.com> > wrote
> > > in message news:eB**************@TK2MSFTNGP11.phx.gbl...
> > > > Ah, this is the hex encoding thing I mentioned in my first
post and
> > didn't
> > > > provide an example for. You can either use the BitConverter class to
> > > > convert the byte[] to hex digits and then strip out the -

characters
> it
> > > puts
> > > > in between or using something like my function called
> > ConvertToOctetString
> > > > that you can do a Google groups search for that does this. It
> basically
> > > > just uses a StringBuilder and the X2 format code to loop over the > bytes
> > > and
> > > > build the string.
> > > >
> > > > Also, MAKE SURE that the vendor is calculating the MD5 of the data > using
> > > the
> > > > same encoding that you are (UTF8, ACSII, UTF16, etc.) or else your > input
> > > > byte array may be different and thus your hash will be different. > > > >
> > > > HTH,
> > > >
> > > > Joe K.
> > > >
> > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > news:ut**************@TK2MSFTNGP10.phx.gbl...
> > > > > Joe, I'm connecting an online store to the payment system. I

need
to
> > > pass
> > > > > this string to this payment gateway, and it will return me

another
> > > string
> > > > > back, confirming that the payment was successful.
> > > > >
> > > > > I contacted the techsupport of this gateway. They said that
I don't
> > have
> > > > to
> > > > > convert my string to Base64. I need to convert every byte to
16-bit
> > > number
> > > > > or something like this. They don't use ASP.Net, so couldn't

give me
> an
> > > > exact
> > > > > code. They said that it should look approximately like this:
> > > > >
> > > > > StringBuilder sb = new StringBuilder();
> > > > >
> > > > > for (int i=0;i<hash.Length;i++)
> > > > >
sb.Append(hash[i].ToString("x").PadLeft(2,'0'));
> > > > >
> > > > > return sb.ToString();
> > > > >
> > > > > How it should look in VB.Net?
> > > > >
> > > > > Thank you,
> > > > >
> > > > > Peter
> > > > >
> > > > >
> > > > > "Joe Kaplan (MVP - ADSI)"
<jo*************@removethis.accenture.com>
> > > wrote
> > > > > in message news:OJ****************@tk2msftngp13.phx.gbl...
> > > > > > What is giving you a "bad CRC" error? Is it the code below? That
> > > looks
> > > > > > like it should just return a base64 encoded MD5 hash of

whatever
> > > string
> > > > > was
> > > > > > provided.
> > > > > >
> > > > > > It isn't clear to me what you are trying to do or what the

input
> is
> > in
> > > > the
> > > > > > funtion.
> > > > > >
> > > > > > Joe K.
> > > > > >
> > > > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > > > news:eQ****************@TK2MSFTNGP12.phx.gbl...
> > > > > > > Thank you, Joe.
> > > > > > >
> > > > > > > I've tried to change it like this:
> > > > > > >
> > > > > > > Dim hashedBytes As Byte()
> > > > > > > Dim md5 As New MD5CryptoServiceProvider
> > > > > > > Dim encoder As new UTF8Encoding
> > > > > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC))
> > > > > > > sCRC = Convert.ToBase64String(md5.ComputeHash(hashedBytes )) > > > > > > > Me.crc.Value = sCRC
> > > > > > >
> > > > > > > Yes, the output string has changed:
> > > > > > >
> > > > > > > '<input type=hidden name=crc
value="35r0XmeFIOXs5evTQM0q+w==">'+
> > > > > > >
> > > > > > > But I'm still getting a "bad crc" error.
> > > > > > >
> > > > > > > Peter
> > > > > > >
> > > > > > > "Joe Kaplan (MVP - ADSI)"
> > <jo*************@removethis.accenture.com>
> > > > > wrote
> > > > > > > in message news:uo****************@TK2MSFTNGP09.phx.gbl... > > > > > > > > Why don't you try Convert.ToBase64String instead?
Typically,
> > you
> > > > > encode
> > > > > > > > binary data as a string with either Base64 or hex string > > encoding.
> > > > > > > >
> > > > > > > > Also, be careful about using ASCII encoding to convert the > input
> > > > > string
> > > > > > to
> > > > > > > > binary. If it includes any non-ASCII characters, you'll be
> > > throwing
> > > > > > data
> > > > > > > > away. UTF8 is safer. Whatever you do, make sure you

alway
> > > compute
> > > > > the
> > > > > > > hash
> > > > > > > > the same way if you are going to be using it for a
comparison.
> > > > > > > >
> > > > > > > > HTH,
> > > > > > > >
> > > > > > > > Joe K.
> > > > > > > >
> > > > > > > > "Peter Afonin" <pv*@speakeasy.net> wrote in message
> > > > > > > > news:eJ**************@TK2MSFTNGP11.phx.gbl...
> > > > > > > > > Hello,
> > > > > > > > >
> > > > > > > > > I'm struggling with the string conversion to MD5
which I've
> > > never
> > > > > user
> > > > > > > > > before.
> > > > > > > > >
> > > > > > > > > I have a string that I need to encode which looks
> > approximately
> > > > like
> > > > > > > this:
> > > > > > > > >
> > > > > > > > >
> > > > > > >
> > > > >
> > >
>

"pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Emai l=petera_gudzon.net:lang
> > > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>

=ru:shp_PaymentNo=20040825205308:shp_UserID=pva:sh p_Price=2.95:shp_HostPlan=
> > > > > > > > > BU:shp_Term=2"
> > > > > > > > >
> > > > > > > > > I'm doing it this way:
> > > > > > > > >
> > > > > > > > > Dim hashedBytes As Byte()
> > > > > > > > > Dim md5 As New MD5CryptoServiceProvider
> > > > > > > > > Dim encoder As New ASCIIEncoding
> > > > > > > > > hashedBytes = md5.ComputeHash(encoder.GetBytes(sCRC)) > > > > > > > > > Dim sNewCRC as String =
> > > > > Convert.ToString(md5.ComputeHash(hashedBytes))
> > > > > > > > >
> > > > > > > > > It doesn't work. When I see the output on the page

where
I
> > pass
> > > > this
> > > > > > > > string,
> > > > > > > > > it looks like this:
> > > > > > > > >
> > > > > > > > > '<input type=hidden name=crc value="System.Byte[]">'+ > > > > > > > > >
> > > > > > > > > I don't know exactly how it should look like, but

probably
> not
> > > > like
> > > > > > > > > "System.Byte[]"
> > > > > > > > >
> > > > > > > > > I'm doing something wrong, but I don't know what.
> > > > > > > > >
> > > > > > > > > I would really appreciate your help.
> > > > > > > > >
> > > > > > > > > Thank you,
> > > > > > > > >
> > > > > > > > > Peter Afonin
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 18 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vladimir Khvostov | last post by:
Hi, We have some DB2 table on the host that has varchar(3200) columns that are used to store binary data (I know that "varchar(3200) for bit data" should have been used, by modifying host table is...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
3
by: Steve Richter | last post by:
here is a warning I am getting in a C++ .NET compile: c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while...
0
by: VB Programmer | last post by:
Simple ASP.NET 1 site. Opened solution in beta 2 of 2.0. Ran thru conversion wizard and it states: "Conversion Complete. There were some errors during conversion." I view the conversion log...
4
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
6
by: Dhirendra Singh | last post by:
Hi, The following C++ program is not compiling on my system. #include <iostream> using namespace std; class complex { double re, im; public: complex( ) :re(0), im(0) {}
4
by: Coleen | last post by:
Hi All :-) I'm new to this site. I've been trying to convert several .Net 2003 web applications and getting tons of conversion errors. I found this site to help walk me through the...
8
by: Nikola | last post by:
Hello, I'm writing a String class for C++ and I'm getting the following error message when using operator: test.cpp: In function ‘int main()’: test.cpp:7: error: ISO C++ says that these are...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.