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

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%BF%BD%3D%EF%BF%BD%EF%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%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?)

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.

Related

String of byte array to byte array (rsa and java)

I am working on a web service, and I want to send a byte array as a String, then get the original byte array.
I explain again, my server side has the role of encrypting a message, so I have a byte array.
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(cipher.ENCRYPT_MODE,clefPrivee);
byte[] cipherText= cipher.doFinal(msgEnOctets);
then to send this encrypted message, I send it as a String because I am sending an entire data frame
code :
cipherText.toString();
So I have the array as a string but nothing has changed.
How can I get my original painting back?
thanks
A common way to send byte array is to encode it in Base64 before sending it, on the other side when receiving the string it must be decoded the Base64 to get the original byte array. For example:
Sender:
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(cipher.ENCRYPT_MODE,clefPrivee);
byte[] cipherText= cipher.doFinal(msgEnOctets);
return Base64.getEncoder().encodeToString(cipherText);
Receiver:
public void getMessage(String message) {
byte[] decodeMessage = Base64.getDecoder().decode(message);
//...
}
Please do NOT use the conversion from #andy jason (https://stackoverflow.com/a/63489562/8166854) as a byte array (especially when used with data used for
encryption) cannot get converted to a string and vice verse with new String(bytes, charset).
One method for a byte array -> String -> byte array conversion is to use the Base64-encoding:
result:
ByteToString and reverse test
bytes: ee99c01c47185dbd6b62dd9bcfed94d7
method as by comment andy jason
s: ��G]�kbݛ���
tab: efbfbdefbfbd1c47185defbfbd6b62dd9befbfbdefbfbdefbfbd
bytes equal to tab: false
method with base64
s2: 7pnAHEcYXb1rYt2bz+2U1w==
tab2: ee99c01c47185dbd6b62dd9bcfed94d7
bytes equal to tab2: true
code:
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;
public class ByteToString {
public static void main(String[] args) {
System.out.println("https://stackoverflow.com/questions/63489517/string-of-byte-array-to-byte-array-rsa-and-java");
System.out.println("ByteToString and reverse test");
byte[] bytes = new byte[16];
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextBytes(bytes);
System.out.println("bytes: " + bytesToHex(bytes));
// method by andy jason
System.out.println("\nmethod as by comment andy jason");
Charset charset = StandardCharsets.UTF_8;
String s = new String(bytes, charset);
System.out.println("s: " + s);
byte [] tab = s.getBytes (charset);
System.out.println("tab: " + bytesToHex(tab));
System.out.println("bytes equal to tab: " + Arrays.equals(bytes, tab));
// method with base64
System.out.println("\nmethod with base64");
String s2 = Base64.getEncoder().encodeToString(bytes);
System.out.println("s2: " + s2);
byte[] tab2 = Base64.getDecoder().decode(s2);
System.out.println("tab2: " + bytesToHex(tab2));
System.out.println("bytes equal to tab2: " + Arrays.equals(bytes, tab2));
}
private static String bytesToHex(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
}
If you want to convert your byte array to String reversibly, you have to use the String constructor which expects a byte array:
String s = new String(bytes, charset);
Then to find your byte array, you have to be careful to use the same charset:
byte [] tab = s.getBytes (charset);

Possible error of the encoding of a message

I'm trying to make a simple class which display the message to be coded, the encoded message and the decoded message. But I think that my class is wrong. Here is what I did with the explenation of the function:
String messageToEncode = "a";
try {
//We create a key
SecretKey key = KeyGenerator.getInstance("AES").generateKey();
// We choose method AES in order to encode message
Cipher cipher = Cipher.getInstance("AES");
//We enter the encoding phase of the message
cipher.init(Cipher.ENCRYPT_MODE, key);
//We transform the encoded message from String to Byte
byte[] res = cipher.doFinal(messageToEncode.getBytes());
//We transform the encoded message from Byte to String. Here we have a coded message that needs the key to be read
String codedMessage = Base64.getEncoder().encodeToString(res);
//We enter the decoding phase of the message
cipher.init(Cipher.DECRYPT_MODE, key);
//We decode the encoded message
byte[] res2 = cipher.doFinal(Base64.getDecoder().decode(codedMessage));
//We display the decoded message
String decodedMessage = new String(res2);
//We display the message sent at the beggin
System.out.println("Message:" + messageToEncode);
//We display the encoded message
System.out.println("Encoded message:" + codedMessage);
//We display the decoded message
System.out.println("Decoded message:" + decodedMessage);
//We recover the key
byte[] keyByte = key.getEncoded();
//We display the key
System.out.println(Base64.getEncoder().encodeToString(keyByte));
} catch (Exception ex) {
}
}
And I have at the output:
Message to code:a
Encoded message:oIgc5kuv8ROgCqNkpndCPQ==
Decoded message:a
u645vsT3RP5FRHLtGfIhrA==
I think that my class is false because the message to be coded is composed of only one letter but the encoded message is composed of 26 letters ! Shouldn't it be composed of also one letter ? So I would like if what I got is normal please.
I thanks in advances for anyone who takes time to help me.
P.S: I use JDK 12 with NetBeans 11.
What you see is what you should expect.
AES is a block cipher: it encrypts data in blocks of 16 bytes. If the input data is not an even multiple of 16 bytes, it's padded. I would expect the original length of the data to be included somehow so it wouldn't surprise me if the output from encrypting a single byte in AES was a little longer than 16 bytes.
Base64 outputs 4 bytes for every block of 3 bytes in the input. In 16 input bytes there are 6 such blocks, so the encrypted message becomes at least 24 bytes in base64.

How to decode base64 in java

I am getting a base64encoded xml data in webservice response and I want to decode it in Java .
I tried org.apache.commons.codec.binary.Base64;
byte bytesEncoded[] = base64encodedStringfromWebservice.getBytes();
// Decrypt data on other side, by processing encoded data
byte[] valueDecoded= Base64.decodeBase64(bytesEncoded );
System.out.println("Decoded value is " + new String(valueDecoded));
but still some characters like (< , />) are not getting decoded properly .
Please suggest how to decode it correctly?
The getBytes method from String is platform specific. You need to specify an encoding, and later use that same encoding to decode the string. You can just use UTF8.
You also need to do the steps in reverse order: string -> base64 -> raw utf8 bytes -> base64 -> string
// ENCODE data
byte bytesEncoded[] = base64encodedStringfromWebservice.getBytes("UTF8");
// DECODE data on other side, by processing encoded data
String base64encodedStringfromWebservice = new String(bytesEncoded, "UTF8");
byte[] valueDecoded = Base64.decodeBase64(base64encodedStringfromWebservice);
System.out.println("Decoded value is " + new String(valueDecoded));
Try specifiying the charset you are using. for example, utf-8, and as you can see here: Decoding a Base64 string in Java
byte[] valueDecoded= Base64.decodeBase64(bytesEncoded);
System.out.println("Decoded value is " + new String(valueDecoded, "UTF-8"));

Convert byte[] to String Java Android Encryption

I am using the JNCryptor library to encrypt a string before sending it to my server as an encrypted string. Here is my code:
String teststring = "Hello World";
JNCryptor cryptor = new AES256JNCryptor();
byte[] plaintext = teststring.getBytes();
String password = "test";
try {
byte[] ciphertext = cryptor.encryptData(plaintext, password.toCharArray());
String a = new String(ciphertext);
return a;
} catch (CryptorException e) {
// Something went wrong
e.printStackTrace();
return "0";
}
However, when I send my string "a" to the server, it has a bunch of unrecognizable characters. I read an explanation
regarding this:
String is not a suitable container for binary data and ciphertext is
binary data. For any given character encoding not all bytes and byte
sequences represents characters and when an un-representable byte or
sequence is found it is converted to some error character. Obviously
this error character cannot be converted back to a unique byte or byte
sequence (it is a many->one mapping).
Is this advice correct? In that case, how do I convert the byte[] to a string correctly? So that I can readably store it on my server?
There is no standard way for converting from a byte array to a string. You have to encode the byte array. A common way to do this is base64 encoding.
For an explanation of how base64 encoding works: http://en.wikipedia.org/wiki/Base64
Then once it gets to your server, base64 decode it back into your original byte array and store it, done!

BadPaddingException : Data must start with zero

I implemented data encryption/decryption with RSA. It works if I just encrypt/decrypt locally, however if I send my encrypted data I get BadPaddingException: Data must start with zero.
In order to send my data over network I need to change it from byte array to String (I'm sending it in a header) on the client side and then retrieve it and change it back to byte array on the server side.
Here's my code for local encryption/decryption (I'm using private key to encrypt and public key do decrypt):
// Encryption:
String message = "HELLO";
Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.ENCRYPT_MODE, privateKey); // privateKey has type java.security.PrivateKey
byte [] encryptedBytes = rsa.doFinal(message.getBytes());
// Decryption:
rsa.init(Cipher.DECRYPT_MODE, publicKey); // type of publicKey: java.security.PublicKey
byte [] ciphertext = rsa.doFinal(encryptedBytes);
String decryptedString = new String(ciphertext, "UTF-8");
DecryptedString and message are the same and everything works fine.
Then I use the same code on the client side just for encryption plus I change ciphertext to a String using:
String encryptedString = new String(ciphertext, "UTF-8");
And on the server side I do:
String message = request.getHeader("Message");
byte [] msgBytes = message.getBytes("UTF-8");
Cipher rsa = Cipher.getInstance("RSA");
rsa.init(Cipher.DECRYPT_MODE, publicKey);
byte [] decryptedMsg = rsa.doFinal(msgBytes);
String decryptedString = new String(decryptedMsg, "UTF-8");
This doesn't work and I get BadPaddingException.
I have tried using different instance of cipher, e.g. "RSA/ECB/PKCS1Padding" or "RSA/ECB/NoPadding" but this didn't help. I also tried converting Strings using BASE64 but then I get a different exception: IllegalBlockSizeException.
I know I probably do sth wrong with converting Strings into byte arrays and vice versa, but I just can't figure out the correct way of doing that. Please help!
You can't just convert arbitrary binary data (the encrypted text) into a String. If you want to send the data as text, you need to use some sort of binary -> text encoding like Base64.

Categories