mbuet2ner / JEasyCrypto

A project for educational purposes for the course "Open Source Software Development" at the University of Oulu
GNU General Public License v3.0
2 stars 23 forks source link

Fix bug so that non-ascii characters work #35

Closed jennitormala closed 4 years ago

jennitormala commented 4 years ago

1 #2 Fixed bug that didn't allow non-ascii characters to work. Non-ascii characters, such as Γ€ and ΓΆ take two bits instead of one which is why length() calculates the data length incorrectly. This caused incomplete JSON strings with EOF errors.

mbuet2ner commented 4 years ago

If someone wants to help with this issue (since it is a relatively big one compared to the average), that would be great! @anttijuu pointed out, that this might also be a good point to implement some unit tests (see #21) related to encoding and input. If someone wants to provide the tests, that would be a very welcomed enhancement.

anttijuu commented 4 years ago

Just a comment here, about what is the issue. From my test runs with the proposed PR:

I enter test characters "πžπž‘πž’πž“πž”β™›β™š\β‰οΈŽ" (encrypt with matrix method).

Give text to encrypt > πžπž‘πž’πž“πž”β™›β™š\β‰οΈŽ
...
Response received.
Received raw data: {"result":0,"id":17,"data":"πž”\\?β™›\u2049?β™šοΈŽπž’πž‘πž","operation":"encrypt-response"}
Parsing...
Result: 0
Data: πž”\?♛⁉?β™šοΈŽπž’πž‘πž
java.net.DatagramPacket@2c73168b
Starting to receive responses...

** Error - not a valid number ** !

As you can see, not all characters (code points) are in the response and there is the Error printout too. This is because code is still handling chars, not Unicode chars (code points). This applies not only to Client and Server not using UTF-8, but also different crypto methods crypting by 8 bit chars.

If you try to put the encrypted response back to server again using the same matrix method, you would not get the original string back. That is the issue. All text you encrypt with some method, you should be able to get back the original string when using decrypt. This doesn't happen now:

Give decryption method > matrix
Give text to decrypt > πž”\?♛⁉?β™šοΈŽπž’πž‘πž
...
Response received.
Received raw data: {"result":0,"id":18,"data":"πžπž‘πž’????β™›β™š\\\u2049︎","operation":"decrypt-response"}
Parsing...
Parsed, handing JSON object to observer.
Got response from reader...
Request ID: 18
Operation: decrypt-response
Result: 0
Data: πžπž‘πž’????β™›β™š\β‰οΈŽ

So you do not get the original string back because the data is not handled correctly. Pretty useless encryption software -- I encrypt something and send it to someone, who tries to decrypt it but gets garbage back.

jennitormala commented 4 years ago

Oops, I accidentally closed this PR by merging my own fork. Not sure how to re-open. 😬 We will try to work on this issue.

jennitormala commented 4 years ago

Now the Matrix method is working and it handles the characters by code points. The characters seem to display correctly:

The methods are: reverse, matrix, cyr
Give encryption method > matrix
Give text to encrypt > asd123πžπž‘πž’πž“πž”β™›β™š\β‰οΈŽ
...
Your choice > Response received.
Received raw data: {"result":0,"id":1,"data":"a2πž’β™šs3πž“\\dπžπž”\u20491πž‘β™›οΈŽ","operation":"encrypt-response"}
Parsing...
Parsed, handing JSON object to observer.
Got response from reader...
Request ID: 1
Operation: encrypt-response
Result: 0
Data: a2πž’β™šs3πž“\dπžπž”β‰1πž‘β™›οΈŽ

The same encrypted string of characters also was decrypted correctly:

Give decryption method > matrix
Give text to decrypt > a2πž’β™šs3πž“\dπžπž”β‰1πž‘β™›οΈŽ
...
Your choice > Response received.
Received raw data: {"result":0,"id":2,"data":"asd123πžπž‘πž’πž“πž”β™›β™š\\\u2049︎","operation":"decrypt-response"}
Parsing...
Parsed, handing JSON object to observer.
Got response from reader...
Request ID: 2
Operation: decrypt-response
Result: 0
Data: asd123πžπž‘πž’πž“πž”β™›β™š\β‰οΈŽ

This doesn't solve the issue that the same encoding and char handling should be used everywhere, but maybe it is a start.

mbuet2ner commented 4 years ago

Since this PR does not solve the problem, I am closing it right now. However, it lead to an important and fruitful discussion with some good insights. I have linked it in the relevant issues.