RSA Android-Java Simple Example

Asked by Giovanni Palmer on 2015-06-10

Good Morning.

I need a simple RSA example to encrypt my telephone ID, send it using NFC, receive and decrypt it using Java and nfcpy.

At the moment i've already implemented a "clear" NFC communication, meaning that i'm able to send the "clear ID" (without encryption) of my smartphone through NFC and receive it on Java side using nfcpy.

When i receive my ID on Java side, i use these lines of code:

           --> String[] callAndArgs = {"/usr/bin/python","/home/giovanni/nfcpy/examples/beam.py","recv","print"};
                 Process p = Runtime.getRuntime().exec(callAndArgs);
   BufferedReader stdInput = new BufferedReader(new
  InputStreamReader(p.getInputStream()));
  BufferedReader stdError = new BufferedReader(new
  InputStreamReader(p.getErrorStream()));

               (used to receive the NFC id and to print the result inside a JFrame/JTextArea)

          --> ...
                String st = null;
                String ABC;
     while ((st=stdInput.readLine()) != null)
     {
      splitted = st.split("=");
      if(splitted.length > 1)
      {
       //Delete empty spaces from the first part of splitted
       ABC=splitted[0].replaceAll("\\s", "");
       if (ABC.equals("data"))
       {
        //Delete empty spaces from the second part of splitted
        ID = splitted[1].replaceAll("\\s","");
        //Delete quotes of ID
        ID = ID.replaceAll("'", "");

                                                               //Finally compare the obtained ID with the one inside an XML file
        XMLParsing parsing = new XMLParsing();
        String Codice = parsing.PARSE(ID);
                                                               ...
                                               }

This is the second part, where a line parsing is implemented to find the "effective" ID after the "data" field.

Thank you for the attention

Best regards

- Giovanni Palamà -

Question information

Language:
English Edit question
Status:
Answered
For:
nfcpy Edit question
Assignee:
No assignee Edit question
Last query:
2015-06-10
Last reply:
2015-06-16

This doesn't look like to be a question about nfcpy but rather Java programming. I would guess there are some good cryptographic libraries in the Java universe and the only issue is to encrypt before sending and decrypt the parsed result in Java. I'd suggest you ask this on Stackoverflow or some Java forum.

Thank you for the reply Mr. Tiedemann.

I understood what I was doing wrong.

Now my question is:

Is there a way to check if the crypted string of characters that I send with Android via NFC is "corrupted" (or changed) once it is received Java side using the library nfcpy?

Thanks

It is almost guaranteed that the data received is free of transmission errors. The low level communication layer uses 16 bit checksums to ensure that receive data is only accepted if there are no bit errors. You can of course always add an additional integrity check, depending on your encryption method you might already have it.

Thank you for the answer Mr. Tiedemann.

I use RSA Public Key encryption on Android Side to encrypt my string and RSA Private Key decryption on Java side.

I use an InputStreamReader to "buffer" the command results of the "beam.py" script (with the addiction of some options/parameters).

It could be a problem with the input stream and how it treats the characters received by NFC?

Please, any suggestion is welcome on my part because I'm stuck in this situation from several days.This may be a problem with the input stream and how it treats the characters received by NFC?

Please, any suggestion is welcome on my part because I'm stuck in this situation from several days.

Thanks

I would suspect that the data string you're parsing will contain non-printable characters, pretty likely when it's encrypted. The "beam.py recv print" output shows the data parts as string representations where non-printable characters (bytes) are shown as hexadecimal values, for example the character ordinal 1 is printed as '\x01' and 255 is shown as '\xff'. If think this is also acceptable input for a Java String type.

Can you help with this problem?

Provide an answer of your own, or ask Giovanni Palmer for more information if necessary.

To post a message you must log in.