convert base64 decoded ascii string to hex

369 views
Skip to first unread message

sanjeev

unread,
Aug 17, 2007, 1:50:11 AM8/17/07
to jpos-...@googlegroups.com

Hello,

I am trying to decode a base64 encoded string which looks like this.
AAACBQRzZGmVmChglHNkAAAAAAA=
I used sourceforge.net iharder base64 package and the resulting ascii looks
like this
sdi•˜(`”sd
this string i am trying to convert to hex but always getting wrong result.
The result I am expecting
is like this 02 05 04 73 64 69 E2 80 A2 CB 9C 28 60 E2 80 9D 73 64 but i am
not getting this...
I am getting 02 05 04 73 64 69 95 98 28 60 94 73 64.
As you can see the first few bytes are correct but the rest is wrong. Here
is my test class.

public class iHarderTest {
public static void main(String args[]) {

String cavv = "AAACBQRzZGmVmChglHNkAAAAAAA=";

byte[] decBytes = Base64.decode(cavv);
String s2 = new String(decBytes);
StringBuffer newStr = new StringBuffer();
StringBuffer hexStr = new StringBuffer();
for (int x = 0; x < s2.length(); x++)
{
int i = (int) s2.charAt(x);

// split 1 hex byte to 2 ascii chars
newStr.append(" ");
newStr.append(Integer.toHexString(i >> 4).toUpperCase());
newStr.append(" ");
newStr.append(Integer.toHexString(i & 0xF).toUpperCase());
}
System.out.println("newStr: " + newStr);

System.out.println("DECODED==" + new String(decBytes) );
}

}
Can anyone please tell me why i am not getting the correct result or what i
am doing wrong ?

Thanks in advance.
cheers,
sanjeev.
--
View this message in context: http://www.nabble.com/convert-base64-decoded-ascii-string-to-hex-tf4283802.html#a12194366
Sent from the jPOS - Users mailing list archive at Nabble.com.

sanjeev

unread,
Aug 17, 2007, 6:50:58 AM8/17/07
to jpos-...@googlegroups.com

Mark Salter

unread,
Aug 17, 2007, 7:16:52 AM8/17/07
to jpos-...@googlegroups.com
sanjeev wrote:
>
> no takers ?
To sort out your coding problems on a decode that has only tenuous links
to jPOS, why are you surprised?

8)

x'0000020504736469959828609473640000000000'

Is the binary detail I think is encoded in your B64.

You likely need to treat your decoded data as a byte array, then ISOUtil
methods will covert the byte[] to hex or whatever format you need? At
least by using ISOUtil, you might get some additional help in the
application of a jPOS resource.

--
Mark

sanjeev

unread,
Aug 17, 2007, 9:47:16 AM8/17/07
to jpos-...@googlegroups.com

Mark,

Ok. Now i can say with certainty that i got the base64 decoding correct
because i used
iharder class which i read in the forum posts that it actually works.

My problem is that i used the jpos class among others to convert this to
hex, and very surprisingly
none of them give the correct result.

This base64 encoded string needs to be decoded and converted into hex
AAACBQRzZGmVmChglHNkAAAAAAA=

02 05 04 73 64 69 E2 80 A2 CB 9C 28 60 E2 80 9D 73 64

With a php script on this site http://www.hurgh.org/ascbinhex.php i can get
the above
hex string except for two digits which are wrong.

I am perplexed and a little confused.

Thanks for your help.

regards,
sanjeev.

--
View this message in context: http://www.nabble.com/convert-base64-decoded-ascii-string-to-hex-tf4283802.html#a12199806

Alejandro Revilla

unread,
Aug 17, 2007, 10:34:13 AM8/17/07
to jpos-...@googlegroups.com
You can use byte[] cryptogram = sun.misc.BASE64Decoder's decodeBuffer to
get a byte[] and then ISOUtil.hexString to get your hex string. I agree
with Mark that this has nothing to do with jPOS.

Mark Salter

unread,
Aug 17, 2007, 11:17:45 AM8/17/07
to jpos-...@googlegroups.com
sanjeev wrote:
>
> Mark,
>
> Ok. Now i can say with certainty that i got the base64 decoding correct
> because i used
> iharder class which i read in the forum posts that it actually works.
Maybe I was a bit too harsh even though your posted code contained no
jPOS related code at all.

>
> My problem is that i used the jpos class among others to convert this to
> hex, and very surprisingly
> none of them give the correct result.

Perhaps your expected result is wrong?

>
> This base64 encoded string needs to be decoded and converted into hex
> AAACBQRzZGmVmChglHNkAAAAAAA=
>
> 02 05 04 73 64 69 E2 80 A2 CB 9C 28 60 E2 80 9D 73 64
>
> With a php script on this site http://www.hurgh.org/ascbinhex.php i can get
> the above
> hex string except for two digits which are wrong.

You don't show what you get using this website, so it is very hard to
advise, perhaps a typo caused the difference?

>
> I am perplexed and a little confused.

I don't have the same classes you are using, so instead offer an
alternative with your error highlighted (I hope):-


String b64 = "AAACBQRzZGmVmChglHNkAAAAAAA=";
String binary = org.mortbay.util.B64Code.decode(b64);

System.out.println("wrong?="+ISOUtil.hexString(binary.getBytes()));
// Prints:-
// wrong?=00000205047364693F3F28603F73640000000000
System.out.println("right?="+ISOUtil.hexString(binary.getBytes("UTF-8")));
// Prints:-
// right?=0000020504736469C295C2982860C29473640000000000


Neither of which match your expected result!?

As you can see I think you are suffering from a String to byte array
conversion issue. So no need to change your Base64 decoding class, as
it is likely fine.

You still need to deal with the 'missing' (or padding zero bytes), which
really are there.

Does that help at all?

--
Mark

sanjeev

unread,
Aug 17, 2007, 11:57:14 AM8/17/07
to jpos-...@googlegroups.com

I agree that this question is not related to
jpos but i post here because i get good replies.

:-)

So anyway, i did what Alejandro told me to and I got this result.

0000020504736469959828609473640000000000

It is different from what i thought was the correct result.
If you see my original message the string i expected to find is there.

So my question again - is this result correct or not.

apologies for posting a non-jpos related question.

Thanks and cheers,
sanjeev.

--
View this message in context: http://www.nabble.com/convert-base64-decoded-ascii-string-to-hex-tf4283802.html#a12202119

sanjeev

unread,
Aug 18, 2007, 1:57:34 PM8/18/07
to jpos-...@googlegroups.com

Mark,

I am intrigued as well by the difference in the results because i have some
other code which
produces some other result as well.

No idea which is the correct result yet. I'll get back though.

Thanks a ton. :-)

regards,
sanjeev.

--
View this message in context: http://www.nabble.com/convert-base64-decoded-ascii-string-to-hex-tf4283802.html#a12215634

Reply all
Reply to author
Forward
0 new messages