Android Question Error with byte values < 0

Johan Schoeman

Expert
Licensed User
This question follows on my posting here:
https://www.b4x.com/android/forum/threads/rsa-encryption-decryption.89436/

To give some background:
1. The below is a pic from our South African Drivers Licences:
pdf417.png


2. The PDF417 code is encrypted but I have managed to decrypt it.
3. After RSA decryption I get a string from block 1 of the PDF417 decrypted code that looks like this:
B4X:
AQIDBAUCjhoDADcBGoJRQeBFQuHhU0NIT0VNQU7gSkjhWkHgWkHgMOAw4eEyMDIyMDAwNlQ2UzbgNjIwNTA0NTEzNjA4MQIZmBIHGZgSB6oAoBGWIFBCAVEBAgIBAlAaV0kEAPoAyEMcKEAAMCAAMUUOMQ+mGSMUy07AWsq8664=
4. Running the string in (3) above through the attached B4A project (the sting is hard coded in the B4A project) gives me the following result (see code in attached B4A project where I log it):
���7�QA�EB��SCHOEMAN�JH�ZA�ZA�0�0��20220006T6S6�6205045136081������� PBQPWI������C(@��0 ��1E1�#�N�Zʼ��

5. The result (in the B4A log) is the following (sorry for not putting it into a "code" block but I need to highlight some rights and wrongs and what could possibly be incorrect):

���7�QA�EB��SCHOEMAN�JH�ZA�ZA�0�0��20220006T6S6�6205045136081������� PBQPWI������C(@��0 ��1E1�#�N�Zʼ��
LENGTE = 127
********************************
char = karak = 01 MyBase64StringAsBytes(0) = 1
char = karak = 02 MyBase64StringAsBytes(1) = 2
char = karak = 03 MyBase64StringAsBytes(2) = 3
char = karak = 04 MyBase64StringAsBytes(3) = 4
char = karak = 05 MyBase64StringAsBytes(4) = 5
char = karak = 02 MyBase64StringAsBytes(5) = 2
char = � karak = fffd MyBase64StringAsBytes(6) = -114
char = karak = 1a MyBase64StringAsBytes(7) = 26
char = karak = 03 MyBase64StringAsBytes(8) = 3
char = �� karak = 00 MyBase64StringAsBytes(9) = 0 'Possibly incorrect decoding!!!!!!
char = 7 karak = 37 MyBase64StringAsBytes(10) = 55 'Version No
char = karak = 01 MyBase64StringAsBytes(11) = 1
char = karak = 1a MyBase64StringAsBytes(12) = 26
char = � karak = fffd MyBase64StringAsBytes(13) = -126 'Incorrect decoding!!!!!!!
char = Q karak = 51 MyBase64StringAsBytes(14) = 81
char = A karak = 41 MyBase64StringAsBytes(15) = 65 'First licence type - motorbike > 50cc
char = � karak = fffd MyBase64StringAsBytes(16) = -32 'Incorrect decoding!!!!!!!
char = E karak = 45 MyBase64StringAsBytes(17) = 69 'EB = car with caravan
char = B karak = 42 MyBase64StringAsBytes(18) = 66
char = � karak = fffd MyBase64StringAsBytes(19) = -31 'Incorrect decoding!!!!!!!
char = � karak = fffd MyBase64StringAsBytes(20) = -31 'Incorrect decoding!!!!!!!

char = S karak = 53 MyBase64StringAsBytes(21) = 83 'my surname in the next 8 chars
char = C karak = 43 MyBase64StringAsBytes(22) = 67
char = H karak = 48 MyBase64StringAsBytes(23) = 72
char = O karak = 4f MyBase64StringAsBytes(24) = 79
char = E karak = 45 MyBase64StringAsBytes(25) = 69
char = M karak = 4d MyBase64StringAsBytes(26) = 77
char = A karak = 41 MyBase64StringAsBytes(27) = 65
char = N karak = 4e MyBase64StringAsBytes(28) = 78
char = � karak = fffd MyBase64StringAsBytes(29) = -32 'Incorrect decoding!!!!!!!
char = J karak = 4a MyBase64StringAsBytes(30) = 74 'my initials in the next 2 chars
char = H karak = 48 MyBase64StringAsBytes(31) = 72
char = � karak = fffd MyBase64StringAsBytes(32) = -31 'Incorrect decoding!!!!!!!
char = Z karak = 5a MyBase64StringAsBytes(33) = 90 'Country where ID issued = ZA
char = A karak = 41 MyBase64StringAsBytes(34) = 65
char = � karak = fffd MyBase64StringAsBytes(35) = -32 'Incorrect decoding!!!!!!!
char = Z karak = 5a MyBase64StringAsBytes(36) = 90 'County where drivers licence issued = ZA
char = A karak = 41 MyBase64StringAsBytes(37) = 65
char = � karak = fffd MyBase64StringAsBytes(38) = -32 'Incorrect decoding!!!!!!!
char = 0 karak = 30 MyBase64StringAsBytes(39) = 48
char = � karak = fffd MyBase64StringAsBytes(40) = -32 'Incorrect decoding!!!!!!!
char = 0 karak = 30 MyBase64StringAsBytes(41) = 48 'Restrictions
char = � karak = fffd MyBase64StringAsBytes(42) = -31 'Incorrect decoding!!!!!!!
char = � karak = fffd MyBase64StringAsBytes(43) = -31 'Incorrect decoding!!!!!!!

char = 2 karak = 32 MyBase64StringAsBytes(44) = 50 'The following 11 green char forms my license No
char = 0 karak = 30 MyBase64StringAsBytes(45) = 48
char = 2 karak = 32 MyBase64StringAsBytes(46) = 50
char = 2 karak = 32 MyBase64StringAsBytes(47) = 50
char = 0 karak = 30 MyBase64StringAsBytes(48) = 48
char = 0 karak = 30 MyBase64StringAsBytes(49) = 48
char = 0 karak = 30 MyBase64StringAsBytes(50) = 48
char = 6 karak = 36 MyBase64StringAsBytes(51) = 54
char = T karak = 54 MyBase64StringAsBytes(52) = 84
char = 6 karak = 36 MyBase64StringAsBytes(53) = 54
char = S karak = 53 MyBase64StringAsBytes(54) = 83
char = 6 karak = 36 MyBase64StringAsBytes(55) = 54
char = � karak = fffd MyBase64StringAsBytes(56) = -32 'Incorrect decoding!!!!!!!
char = 6 karak = 36 MyBase64StringAsBytes(57) = 54 'the next 13 green chars is my ID No
char = 2 karak = 32 MyBase64StringAsBytes(58) = 50
char = 0 karak = 30 MyBase64StringAsBytes(59) = 48
char = 5 karak = 35 MyBase64StringAsBytes(60) = 53
char = 0 karak = 30 MyBase64StringAsBytes(61) = 48
char = 4 karak = 34 MyBase64StringAsBytes(62) = 52
char = 5 karak = 35 MyBase64StringAsBytes(63) = 53
char = 1 karak = 31 MyBase64StringAsBytes(64) = 49
char = 3 karak = 33 MyBase64StringAsBytes(65) = 51
char = 6 karak = 36 MyBase64StringAsBytes(66) = 54
char = 0 karak = 30 MyBase64StringAsBytes(67) = 48
char = 8 karak = 38 MyBase64StringAsBytes(68) = 56
char = 1 karak = 31 MyBase64StringAsBytes(69) = 49
char = karak = 02 MyBase64StringAsBytes(70) = 2
char = karak = 19 MyBase64StringAsBytes(71) = 25 'This is part of a nibble (4 bits only). It is supposed to be 1998 but the incorrect value in the next byte/nibble!!!!
char = � karak = fffd MyBase64StringAsBytes(72) = -104 'Incorrect decoding!!!!!!! I expect this to be "98" and not "fffd"
char = karak = 12 MyBase64StringAsBytes(73) = 18
char = karak = 07 MyBase64StringAsBytes(74) = 7
char = karak = 19 MyBase64StringAsBytes(75) = 25
char = � karak = fffd MyBase64StringAsBytes(76) = -104 'Incorrect decoding!!!!!!! I expect this to be "98" and not "fffd"
char = karak = 12 MyBase64StringAsBytes(77) = 18
char = karak = 07 MyBase64StringAsBytes(78) = 7
char = � karak = fffd MyBase64StringAsBytes(79) = -86 'Incorrect decoding!!!!!!!
char = �� karak = 00 MyBase64StringAsBytes(80) = 0 'Possibly incorrect decoding!!!!!!
char = � karak = fffd MyBase64StringAsBytes(81) = -96 'Incorrect decoding!!!!!!!
char = karak = 11 MyBase64StringAsBytes(82) = 17
char = � karak = fffd MyBase64StringAsBytes(83) = -106 'Incorrect decoding!!!!!!!
char = karak = 20 MyBase64StringAsBytes(84) = 32
char = P karak = 50 MyBase64StringAsBytes(85) = 80
char = B karak = 42 MyBase64StringAsBytes(86) = 66 'This is data in nibbles. It relates to
char = karak = 01 MyBase64StringAsBytes(87) = 1 '20151010 and 20201025
char = Q karak = 51 MyBase64StringAsBytes(88) = 81 'It is the validity dates of the licence
char = karak = 01 MyBase64StringAsBytes(89) = 1
char = karak = 02 MyBase64StringAsBytes(90) = 2
char = karak = 02 MyBase64StringAsBytes(91) = 2
char = karak = 01 MyBase64StringAsBytes(92) = 1
char = karak = 02 MyBase64StringAsBytes(93) = 2
char = P karak = 50 MyBase64StringAsBytes(94) = 80
char = karak = 1a MyBase64StringAsBytes(95) = 26
char = W karak = 57 MyBase64StringAsBytes(96) = 87
char = I karak = 49 MyBase64StringAsBytes(97) = 73
char = karak = 04 MyBase64StringAsBytes(98) = 4
char = �� karak = 00 MyBase64StringAsBytes(99) = 0 'Possibly incorrect decoding!!!!!!
char = � karak = fffd MyBase64StringAsBytes(100) = -6 'Incorrect decoding!!!!!!!
char = �� karak = 00 MyBase64StringAsBytes(101) = 0 'Possibly incorrect decoding!!!!!!
char = � karak = fffd MyBase64StringAsBytes(102) = -56 'Incorrect decoding!!!!!!!
char = C karak = 43 MyBase64StringAsBytes(103) = 67
char = karak = 1c MyBase64StringAsBytes(104) = 28
char = ( karak = 28 MyBase64StringAsBytes(105) = 40
char = @ karak = 40 MyBase64StringAsBytes(106) = 64
char = �� karak = 00 MyBase64StringAsBytes(107) = 0 'Possibly incorrect decoding!!!!!!
char = 0 karak = 30 MyBase64StringAsBytes(108) = 48
char = karak = 20 MyBase64StringAsBytes(109) = 32
char = �� karak = 00 MyBase64StringAsBytes(110) = 0 'Possibly incorrect decoding!!!!!!
char = 1 karak = 31 MyBase64StringAsBytes(111) = 49
char = E karak = 45 MyBase64StringAsBytes(112) = 69
char = karak = 0e MyBase64StringAsBytes(113) = 14
char = 1 karak = 31 MyBase64StringAsBytes(114) = 49
char = karak = 0f MyBase64StringAsBytes(115) = 15
char = � karak = fffd MyBase64StringAsBytes(116) = -90 'Incorrect decoding!!!!!!!
char = karak = 19 MyBase64StringAsBytes(117) = 25
char = # karak = 23 MyBase64StringAsBytes(118) = 35
char = karak = 14 MyBase64StringAsBytes(119) = 20
char = � karak = fffd MyBase64StringAsBytes(120) = -53 'Incorrect decoding!!!!!!!
char = N karak = 4e MyBase64StringAsBytes(121) = 78
char = � karak = fffd MyBase64StringAsBytes(122) = -64 'Incorrect decoding!!!!!!!
char = Z karak = 5a MyBase64StringAsBytes(123) = 90
char = ʼ karak = 2bc MyBase64StringAsBytes(124) = -54
char = � karak = fffd MyBase64StringAsBytes(125) = -68 'Incorrect decoding!!!!!!!
char = � karak = fffd MyBase64StringAsBytes(126) = -21 'Incorrect decoding!!!!!!!

********************************
** Activity (main) Resume **

Strings / Chars colors in the above are interpreted as follows:
I understand these values
There must be an error getting "fffd" from various different byte values
The could possibly be an error with the "decoding" of these values

I have tried to change the byte values by Bit.And(a,0xFF) as well as byte+256 but it makes no difference - the negative byte values remains the same. So, I am obviously missing something here that will result in the correct "karak" values for the bytes that have "negative" values so that they don't result in "karak" being logged as "fffd". I guess it has something to do with bytes values being "signed" or "unsigned".

Any suggestions or solutions will be much appreciated.

Thanks

JS
 

Attachments

  • decoded block.png
    decoded block.png
    3.2 KB · Views: 96
  • b4aDecryptDL.zip
    9.1 KB · Views: 78
Last edited:

Johan Schoeman

Expert
Licensed User
OK - I have figured out the math required here but don't exactly know how to implement this. In the above result that I have logged I commented as follows:

char = � karak = fffd MyBase64StringAsBytes(76) = -104 'Incorrect decoding!!!!!!! I expect this to be "98" and not "fffd"

So, -104 + 256 = 152 which is 0x98. "98" is what I am looking for to somehow get in my b4a code....How do I get -104 to turn into 0X98 ? I am juts interested in the "98".
 
Upvote 0

Star-Dust

Expert
Licensed User
OK - I have figured out the math required here but don't exactly know how to implement this. In the above result that I have logged I commented as follows:

char = � karak = fffd MyBase64StringAsBytes(76) = -104 'Incorrect decoding!!!!!!! I expect this to be "98" and not "fffd"

So, -104 + 256 = 152 which is 0x98. "98" is what I am looking for to somehow get in my b4a code....How do I get -104 to turn into 0X98 ? I am juts interested in the "98".
Do not use Byte (-127...127) but use int or short
 
Upvote 0
Top