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_E
mail=petera_gudzon.net:lang
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_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 AfoninWhy 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" <pva@.speakeasy.net> wrote in message
news:eJWO769iEHA.3928@.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_E
mail=petera_gudzon.net:lang
>
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_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
>
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)" <joseph.e.kaplan@.removethis.accenture.com> wrote
in message news:uoBsCL%23iEHA.4024@.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" <pva@.speakeasy.net> wrote in message
> news:eJWO769iEHA.3928@.TK2MSFTNGP11.phx.gbl...
this:
"pva:0. 05:101214:pa7735tH:inv_desc=205308:shp_E
mail=petera_gudzon.net:lang
>
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_Price=2.95:shp_HostPlan=
> string,
>
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" <pva@.speakeasy.net> wrote in message
news:eQeJri%23iEHA.3612@.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)" <joseph.e.kaplan@.removethis.accenture.com> wrote
> in message news:uoBsCL%23iEHA.4024@.TK2MSFTNGP09.phx.gbl...
to
data
> hash
> this:
> "pva:0. 05:101214:pa7735tH:inv_desc=205308:shp_E
mail=petera_gudzon.net:lang
>
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_Price=2.95:shp_HostPlan=
>
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)" <joseph.e.kaplan@.removethis.accenture.com> wrote
in message news:OJQ8p$%23iEHA.3988@.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" <pva@.speakeasy.net> wrote in message
> news:eQeJri%23iEHA.3612@.TK2MSFTNGP12.phx.gbl...
wrote
encode
string
> to
> data
the
user
"pva:0. 05:101214:pa7735tH:inv_desc=205308:shp_E
mail=petera_gudzon.net:lang
>
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_Price=2.95:shp_HostPlan=
Convert.ToString(md5. ComputeHash(hashedBytes))
>
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.HashPasswordForStoringInConfigFile()
Jos
"Peter Afonin" <pva@.speakeasy.net> wrote in message
news:eJWO769iEHA.3928@.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_E
mail=petera_gudzon.net:lang
> =ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_Price=2.95:shp_HostPla
n=
> 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
>
Thank you, I'll try.
Peter
"Jos Joye" <jose.joye@.KILLTHESPAMSbluewin.ch> wrote in message
news:uIDDl6AjEHA.140@.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.HashPasswordForStoringInConfigFile()
> Jos
> "Peter Afonin" <pva@.speakeasy.net> wrote in message
> news:eJWO769iEHA.3928@.TK2MSFTNGP11.phx.gbl...
this:
"pva:0. 05:101214:pa7735tH:inv_desc=205308:shp_E
mail=petera_gudzon.net:lang
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_Price=2.95:shp_HostPlan=
>
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" <pva@.speakeasy.net> wrote in message
news:utMGDDAjEHA.2544@.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)" <joseph.e.kaplan@.removethis.accenture.com> wrote
> in message news:OJQ8p$%23iEHA.3988@.tk2msftngp13.phx.gbl...
> was
the
> wrote
> encode
> string
> the
> user
like
> "pva:0. 05:101214:pa7735tH:inv_desc=205308:shp_E
mail=petera_gudzon.net:lang
>
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_Price=2.95:shp_HostPlan=
> Convert.ToString(md5.ComputeHash(hashedBytes))
this
like
>
Thank you, Joe!
Peter
"Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@.removethis.accenture.com> wrote
in message news:eB8IzPFjEHA.2696@.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" <pva@.speakeasy.net> wrote in message
> news:utMGDDAjEHA.2544@.TK2MSFTNGP10.phx.gbl...
pass
string
> to
number
> exact
wrote
looks
string
> the
throwing
compute
never
> like
"pva:0. 05:101214:pa7735tH:inv_desc=205308:shp_E
mail=petera_gudzon.net:lang
>
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_Price=2.95:shp_HostPlan=
> this
> like
>
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.MD5CryptoServiceProvider
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)" <joseph.e.kaplan@.removethis.accenture.com> wrote
in message news:eB8IzPFjEHA.2696@.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" <pva@.speakeasy.net> wrote in message
> news:utMGDDAjEHA.2544@.TK2MSFTNGP10.phx.gbl...
pass
string
> to
number
> exact
wrote
looks
string
> the
throwing
compute
never
> like
"pva:0. 05:101214:pa7735tH:inv_desc=205308:shp_E
mail=petera_gudzon.net:lang
>
=ru:shp_PaymentNo=20040825205308:shp_Use
rID=pva:shp_Price=2.95:shp_HostPlan=
> this
> like
>
No comments:
Post a Comment