1

I am using javax.crypto.Cipher to encrypt a String I want to send back to the client side:

public byte[] encrypt(String message)
        throws Exception {
    Cipher cipher = Cipher.getInstance(m_ALGORITHM);
    PrivateKey privateKey = m_kp.getPrivate();
    cipher.init(Cipher.ENCRYPT_MODE, privateKey);
    return cipher.doFinal(message.getBytes());
}

My encrypt and decrypet function works well at the server side but when the encrypted String goes back to the client as :

�zXi�Xq�����.��PiO��dM-�d��a��\`�"����uJ��yu

The client console show:

Uncaught SyntaxError: Invalid or unexpected token

I have tried to UTF-8 format the String:

String utfEnc = new String(encrypetBytes, "UTF-8");

but it does not work and I am out of ideas.

The encryption is Java function my jsp page uses(in my jsp):

Encryptor enc  = new Encryptor(request);
byte[] encData = enc.encrypt(data);

The goal is the send this data via http:

$.ajax({
  type: "POST",
  url: serverUrl,
  data: <%=encData%>, //Uncaught SyntaxError: Invalid or unexpected token
  success: function(){
     console.log('done');
  }
});

Thanks for any help.

14
  • When you encrypt a String it becomes bytes (well, technically a String is bytes too, but once you encrypt it, you're not working with readable characters anymore). You need to decrypt those bytes if you want to treat it as a String again. Commented Jan 1, 2018 at 12:11
  • But if the decryption happens in another server? and all I need to do is to send it for now. Commented Jan 1, 2018 at 12:13
  • 3
    A byte array is not a String. It contains bytes, not characters. And those bytes don't represent characters encoded in UTF8. We have no idea why you get this "Uncaught SyntaxError: Invalid or unexpected token" because you haven't said wht you're doing with that byte array, what the client is, what it tried to do with the byte array, etc. Every piece of data that travels on the network is ultimately bytes. So there should be no problem sending bytes to your client. Commented Jan 1, 2018 at 12:13
  • passing the data over network? Commented Jan 1, 2018 at 12:15
  • i hope you are serializing the object Commented Jan 1, 2018 at 12:15

1 Answer 1

5

When you called doFinal you gave it an array of bytes rather than a string. The output is an array of bytes which does not necessarily correspond to a string. This is why you see all these question marks. As you wish to send it over the network as a string, the common solution is to use either Base64 encoding or hexadecimal to convert the byte array to a string. Java 8 includes the Base64.Encoder class and in it there is the encodeToString method which you could use. The Base64.Decoder class contains the corresponding method to convert the encoded string back to the original byte array which you could then decipher.

Sign up to request clarification or add additional context in comments.

3 Comments

Just thinking the same thing: base64 encode the string value, as it's likely getting corrupted by the html transmission. The server side will need to decode the value again when reading from the client.
@EdH: The byte array rather than the string value.
Well you don't need java 8 Base64.Encoder, a simple js base64 encoder works for me. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.