1

Program Flow

Sender - Generate encryption key as byte[] and show its Base64.encodeToString value to the user then sent that byte[] (key) to the Receiver as udp packet

Receiver - Receive UDP packet and get the key (byte[]), and Getting the Key from user as String (user know the string becoz I shown in Sender program) and convert received key (byte[]) to String using Base64.encodeToString

Here is the Problem, when I compare Both String using equals it return false

When I print both string in logcat

the Output is

12-05 15:39:32.047: V/userkey(2210): oIAfhtGnf+tBX8NBB5ONVQ==

12-05 15:39:32.047: V/reckey(2210): oIAfhtGnf+tBX8NBB5ONVQ==

here you can see both string has same character sequence

Important

I searched lot and coming to know about contentEquals, I tried this too with hope that it will solve my issue becoz its just compare the characters and sequence of both string, But I can't get this working, its too returning false for these Strings...

I get the String from user in EditText as

txtDeKey.getText().toString()

and converting byte[] to String as (tried both)

Base64.encodeToString(key1, Base64.DEFAULT).toString()
Base64.encodeToString(key1, Base64.DEFAULT)
Community
  • 1
  • 1
Thamilan S
  • 1,045
  • 3
  • 18
  • 30
  • 5
    Inspect both strings in the debugger. Check the lengths and look for leading/trailing nulls. – Simon Dec 06 '12 at 08:16
  • 1
    Yes, there may be some non-printable characters in it, that you can't see on standard output. – Udo Klimaschewski Dec 06 '12 at 08:17
  • @Simon then wat is the solution for this problem..., how to trim nulls... – Thamilan S Dec 06 '12 at 09:14
  • @UdoKlimaschewski how to trim non-printable characters – Thamilan S Dec 06 '12 at 09:16
  • http://stackoverflow.com/questions/6198986/how-can-i-replace-non-printable-unicode-characters-in-java but have a look at the bytes in your string first, the problem may be that those character(s) came into the String in the first place. Removing them is curing the symptoms, not the cause. – Udo Klimaschewski Dec 06 '12 at 09:21
  • Thanks @UdoKlimaschewski I solved the problem by using `Base64.encodeToString(keyDe, Base64.DEFAULT).replaceAll("\\p{C}", "?")` wherever I'm encoding byte[] to String... – Thamilan S Dec 06 '12 at 09:53

0 Answers0