2
votes

I am trying to encrypt a message, which works and returns it as a byte array. I then convert this byte array to a string, in order to send via a tcp network message. On the other end, I convert the string back to a byte array, however the resulting array is larger and I can't figure out why. I think it may be something to do with the encoding as if I use "MacRoman", I do not have this problem, however the program needs to be able to run on systems which do not support this encoding, so I decided to use UTF-8.

        String message="222233332221";

        //Encrypt message
        byte[] encryptedMsg = encryptString(message, temp.loadCASPublicKey());
        System.out.println("ENCRYPTED MESSAGE byte Length: "+encryptedMsg.length);


        //Convert to String in order to send
        String stringMessage = new String(encryptedMsg);
        System.out.println("ENCRYPTED MESSAGE String Length: "+stringMessage.length());

        //Convert String back to Byte[] and decrpt
        byte[] byteMessage = stringMessage.getBytes("UTF-8");
        System.out.println("ENCRYPTED MESSAGE byte Length: "+byteMessage.length);

Outputs:

ENCRYPTED MESSAGE byte Length: 256

ENCRYPTED MESSAGE String Length: 235

ENCRYPTED MESSAGE byte Length: 446

Can any one please point me in the right direction as to why the resulting byte array is 446 bytes not 256 bytes.

The encryptString part is as follows. I believe this returns a byte array in UTF-8?

private static byte[] encryptString(String message, Key publicKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    byte[] cipherData = cipher.doFinal(message.getBytes("UTF-8"));     
    return cipherData;
}
3
Why don't you send the bytes directly over socket? Why change to String? - Nikhar
@Nikhar - That would be my preferred way of doing it, but unfortunately I am integrating into an existing system so that isnt an option. - Joseph
You should pass a charset to the String constructor. - Guillaume Polet
Construct your String as: String stringMessage = new String(encryptedMsg, "UTF-8"); - anubhava
Strings are desigend to store text, not binary data. You shouldn't expect to be able to store binary in a text component like a String, Writer or Reader without corruption. - Peter Lawrey

3 Answers

4
votes

Managed to fix it using Base64.

        byte[] encryptedMsg = Base64.encodeBase64(encryptString(message, temp.loadCASPublicKey()));


        System.out.println("ENCRYPTED MESSAGE byte Length: "+encryptedMsg.length);


        //Convert to String in order to send
        String stringMessage = new String(encryptedMsg, "UTF-8");
        System.out.println("ENCRYPTED MESSAGE String Length: "+stringMessage.length());

        //Convert String back to Byte[] and decrpt
        byte[] byteMessage = Base64.decodeBase64(stringMessage.getBytes("UTF-8"));
        System.out.println("ENCRYPTED MESSAGE byte Length: "+byteMessage.length);
2
votes

It's an encoding issue.

1) You have a byte array. It contains bytes
2) You convert it to a string. As soon as you do this, you have a UTF16 encoded String. So you have taken the bytes and changed them to characters.
3) You now convert those characters back to bytes. But if the original bytes were not UTF8 or UTF16, you might not have the same number of bytes. If the default encoding of the platform is MacRoman, then in step 3 you are translating your UTF16 String into bytes, but treating the characters as MacRoman.

0
votes

I guess there is a good reason of doing encryption manually, however just in case... Do you consider using SSLSocket instead?