Page 1 of 1

Problem of DER bytes

Posted: Thu Nov 03, 2016 1:12 am
by Heather
I am implementing Fips 196 and I am caught in a problem recently. I encode the user certificate into encoded byte array and wrap the DER byte array into string object and when I tried to get these DER encoded bytes back again. I got the different bytes. Why this happened? Any help here?

Code: Select all

                         certData = response.getData();     
                         for(int i=0; i<certData.length; i++)
                         {
                              System.out.print(certData);

                         }

               String d = new String (certData);

               byte[] testarr = d.getBytes();

                    for(int i=0; i<testarr.length; i++)

                    {

                         System.out.print(d.getBytes()[i]);

                    }

Re: Problem of DER bytes

Posted: Thu Nov 03, 2016 11:02 pm
by chico0609
Your problem is just because bytes are signed. Any byte in the 0x80-0xFF range will be signed extended to int and printed as a negative integer in range -127...-1.

Re: Problem of DER bytes

Posted: Fri Nov 04, 2016 1:36 am
by Heather
chico0609 wrote:Your problem is just because bytes are signed. Any byte in the 0x80-0xFF range will be signed extended to int and printed as a negative integer in range -127...-1.


Thanks for your help. If the problem is due to sign extension, how can I modify my code?

Re: Problem of DER bytes

Posted: Fri Nov 04, 2016 3:58 am
by chico0609
Clear the high order bits with a AND

Code: Select all

int unsigned_byte = certData[i] & 0xff;