1

Problem

Im working at a company where there is an application where guests have to register using their Belgian ID, this is a smartcard containing data to the person(name, age, sex,...). But it reads certain characters wrong: é, ç,... I have figured out that this is a problem between the library that is reading the ID and the java-program. The library must read the strings in a different encoding or no incoding en when they get placed in a java-application the strings are being read incorrectly.

Attempts

I have tried before the string gets displayed to catch it, look for incorrect characters and transform them back into the correct ones. ex: search for ç and replace it with ç. But this doesn't seem to work. I have also tried upgrading the library and downgrading it to see if it would make a difference but these same reading errors persist. The library used to read the ID's is commens-eid.

Reading eID code

This is the code that reads the eID, this code works for every version of the eID except when the name contains a special character like: é, ù, ... The string that is then retrieved gets placed in a javafx textfield. But as java is encoded in utf-16 (or something like it) it seems highly unlikely that the problem occurs when placing the text in the textfield. The library used is commens-eid.

System.out.println("card connected.");
BeIDCards beIDCards = new BeIDCards();
try {
      final BeIDCard beIDCard = beIDCards.getOneBeIDCard();

      System.out.println("reading identity file");
      final byte[] identityFile = beIDCard.readFile(FileType.Identity);
      final Identity identity = TlvParser.parse(identityFile,
                    Identity.class);
      final String firstName = StringUtilities.format(identity.firstName);
      final String lastName = identity.name;

      //geslacht selecteren.
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    int index = 0;
                    switch (identity.gender) {
                        case MALE:
                            index = 1;
                            break;
                        case FEMALE:
                            index = 2;
                            break;
                        default:
                            index = 3;
                            break;
                    }

                    //zorgt evoor dat de volledige node weergegeven wordt en niet een deel.
               cbTitle.getSelectionModel().select(index);
                    laadcirkel.setVisible(false);
                    txtFirstName.setText(firstName);
                    txtLastName.setText(lastName);
                    txtFirstName.autosize();
                    txtLastName.autosize();
                }
            });

            if (beIDCards.getAllBeIDCards().contains(beIDCard)) {
                System.out.println("waiting for card removal");
                beIDCards.waitUntilCardRemoved(beIDCard);
            }

            System.out.println("card removed");
        } catch (final CancelledException cex) {
            System.out.println("Cancelled By User");
        } catch (CardException ex) {
            Logger.getLogger(AanmeldPagina.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AanmeldPagina.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InterruptedException ex) {
            Logger.getLogger(AanmeldPagina.class.getName()).log(Level.SEVERE, null, ex);
        }
namlik
  • 182
  • 1
  • 12
  • Try printing the names to the console or writing it to a file. Most likely this is an issue with the code converting the raw data to `String`s... – fabian Sep 21 '16 at 11:11
  • I have, the problem is that i have no access to the library that is spitting out the names, after it has read the card it returns a string that already has these errors in it. – namlik Sep 21 '16 at 11:13
  • 1
    Do you know which encoding is used on the ID card? You could try to "reencode" the `byte[]` data of the string, e.g. `new String(string.getBytes(), correctCharSet)` (not sure this would work in all cases). This assumes the library just uses the `String(byte[])` constructor instead of `String(byte[], Charset)`. Alternatively add command line parameters for the jvm specifying the correct encoding as the default encoding... – fabian Sep 21 '16 at 11:26
  • Not a clue as to which encoding they are using(documentation is abhorrent) but ill give it a go. – namlik Sep 21 '16 at 11:28
  • 1
    I do not know what StringUtilities.format is intended to do, but you seem to suspect TlvParser. If TlvParser wrongly uses the `String(byte[])` constructor without encoding, you can do `System.setProperty("file.encoding", "Cp1252");` or `"UTF-8"` (if you get two chars for one). – Joop Eggen Sep 21 '16 at 11:31
  • StringUtilities is an own written class because the ID returns firstname and middlename together, the utilitiesclass returns just the firstname of this. – namlik Sep 21 '16 at 11:32
  • @JoopEggen does that work now? http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding – fabian Sep 21 '16 at 11:33
  • `new String(firstname.getBytes(), "UTF-8")`seems to be working(getting no errors at the moment, i'll try this method first and ill repost here if i have found one that works(dont have enough eID's on hand to properly test and emulation is a **** in itself) or if i have found none. – namlik Sep 21 '16 at 11:44
  • @fabian good point, should be done in the main already, or with `java -Dfile.encoding=UTF-8`. The repair with `new String(firstname.getBytes(), "UTF-8")` might even be more understandable. – Joop Eggen Sep 21 '16 at 12:19
  • Does this only happen for the first name (which you pass through `StringUtilities.format`) or also for the last name? `TlvParser` correctly treats the bytes of the name fields as UTF-8 strings (and explicitly specifies that encoding). – Michael Roland Sep 21 '16 at 12:28
  • hmm yea it happens for both – namlik Sep 21 '16 at 12:28
  • @MichaelRoland Where did you find that TlvParser uses UTF-8? There is no mention of that on the sources of that fedict(belgian gov) provides (or i missed it multiple times) – namlik Sep 21 '16 at 13:30
  • 1
    See [TlvParser on line 127](https://github.com/Fedict/commons-eid/blob/master/commons-eid-consumer/src/main/java/be/fedict/commons/eid/consumer/tlv/TlvParser.java#L127) – Michael Roland Sep 21 '16 at 13:48
  • @MichaelRoland guess i did miss it, thx alot as said ill see how it performs now, and ill say when i have a solution. – namlik Sep 21 '16 at 14:30

0 Answers0