seinecle
seinecle

Reputation: 10778

Encrypted String encoded in Base64 not preserved after going through URL?

A "data" String is encoded in Base64 (URL safe version) then crypted into a byte array. This byte array is turned into a String and sent to a REST web service. When it arrives on the web service side, the length of the String has changed. I am not a specialist, what do I do wrong?

The code:

//CLIENT SIDE
import org.apache.commons.codec.binary.Base64;    

String data = "My message to be encrypted";        
PublicKey pubKey = readPublicKeyFromFile();
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encryptedData = cipher.doFinal(Base64.encodeBase64URLSafe(data.getBytes()));

System.out.println("length of original data string: " + data.length());
System.out.println("length of original data string turned into byte array: " + data.getBytes().length);
System.out.println("length of data string encrypted into byte array: " + encryptedData.length);

String encryptedDataToString = new String(encrypedData,"UTF-8");
System.out.println("length encrypted String into byte array, converted back to String for url: " + encryptedDataToString.length);

httpclient = new DefaultHttpClient();
builder = new URIBuilder();
builder.setScheme("http").setHost(xxx + "webresources/GetData/" + path)
                    .setParameter("data", encryptedDataToString);
            uri = builder.build();



//SERVER SIDE
import org.apache.commons.codec.binary.Base64;

@GET
@Path("path")
@Produces("text/plain")
public String getToken(@QueryParam("data") String data) {

System.out.println("length of data: " + data.length());
System.out.println("length of data to byte array wthout decoding: " + data.getBytes().length);
System.out.println("length of data to byte decoded: " + Base64.decodeBase64(data).length);


//CONSOLE

//CLIENT SIDE
length of original data string: 37
length of original data string turned into byte array: 37
length of data string encrypted into byte array: 256
length encrypted String into byte array, converted back to String for url: 256

//SERVER SIDE
INFO: length of data: 237
INFO: length of data to byte wthout decoding: 444
INFO: length of data to byte decoded: 4

The data String as it is appearing in the URL, client side: T%23%EF%BF%BD%0C%EF%BF%BD%EF%BF%BD7%EF%BF%BD%EF%BF%BDk%EF%BF%BDA%EF%BF%BD%10L%EF%BF%BD%EF%BF%BD%EF%BF%BD%14%EF%BF%BD%EF%BF%BD%5E%22%15B%1Ae%01%EF%BF%BDgmE%0C%EF%BF%BD%EF%BF%BDn%EF%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EF%BF%BDO%EF%BF%BD%EF%BF%BD%11%CB%9C%EF%BF%BD%1Dk%EF%BF%BDkL8%EF%BF%BD*%EF%BF%BD%1D%EF%BF%BDl%EF%BF%BD%EF%BF%BD%EF%BF%BD%02%EF%BF%BD

The data String as it is received, server side:
T#���7��k�A�L�����^"Be�gmE��n�=��܀��IM����eM�5��&�}�NAQ�lT��VXR�i���3U����ep�áb6�εy9x������|!�0����ˏ}�&A����-ލ���t���Tezs��,���ݔ�������%�t�Q�}�F���n� �φR�W�U�xo�d-?�/�[g�2��k^�m��%��^����U��P���{�O��˜�k�kL8���l���

(not sure these are the characters actually received, or just badly printed on the console?)

Upvotes: 0

Views: 1464

Answers (1)

cyph3r
cyph3r

Reputation: 373

You have to encrypt it first and then encode it with base64 and send it via net. Otherwise the encryption creates (pseuro)random data and some parts of that might not be properly encoded for transfer via web and will inevitably get corrupted.

The same applies if you want to compress your data. You do the compression before encrypting them because after the encryption it will be much harder for the algorithm to compress seemingly random data.
Generally the thing you send to a web service should be base64 encoded as a last step/alteration.

Upvotes: 2

Related Questions