0

I am writing a UDP program to send byte[] to the host , i need to send a encrypted text ( RC4 ) to the host , whereby the host decrypt and get the plaintext but i met with some problems whereby where i convert char[] to byte[] to send to host , the host will decrypt the bytes by first convert it to string then get bytes.

    String data = new String(RC4().toString().getBytes()); // Simulate host recieve bytes then i convert to string to convert to char[] for decryption.
    char[] tester = data.toCharArray();
    System.out.println("EN:"+RC4());// Display [C@6d06d69c
    System.out.println(tester);     // Display [C@6d06d69c
    System.out.println(RC4D(tester)); // Display some gibberish.
    System.out.println(RC4D(RC4()));  // Display the correct plain text

Both RC4() encryption function and RC4D() decryption function returns char[] , i hardcode the msg to encrypt in RC4()

I don't get why both shows the same encrypted text but when i decrypt tester , the result differs from RC4().

what
  • 373
  • 2
  • 10
  • 20
  • 1
    We have no idea what `RC4()` is - but calling `toString()` on a `char[]` doesn't do what you might expect. Perhaps you just want `new String(RC4())`? It's hard to tell without a [mcve]. I'd strongly advise you not to use `getBytes()` without specifying an encoding though. – Jon Skeet Nov 03 '17 at 12:21
  • 1
    Additionally, if you're trying to convert encrypted binary data into a string just by using the `String(byte[])` constructor, that's a *really* bad idea. Use base64 or hex - encrypted data is *not* encoded text. – Jon Skeet Nov 03 '17 at 12:22
  • (or if you really want to print the char array - `Arrays.toString(array)`) - see also https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array – Adam Kotwasinski Nov 03 '17 at 12:26

0 Answers0