Reputation: 2184
I'm working on a cross-platform encryption system. One of the requirements is to easily encrypt and decrypt strings in out application code.
The encryption class works flawlessly, but I'm having trouble with string encoding on the java side.
Currently, I have the following static methods:
public static String encrypt(String key, String data)
{
byte[] decoded_key;
byte[] decoded_data;
try
{
decoded_key = key.getBytes("UTF-8");
decoded_data = data.getBytes("UTF-8");
}
catch (Exception e)
{
//Not Supposed to happen.
throw new RuntimeException();
}
if(decoded_key.length != 16)
throw new IllegalArgumentException("Key length must be of 16 bytes. Given is " + decoded_key.length + ".");
try
{
return(IOUtils.toString(encrypt(decoded_key, decoded_data), "UTF-8"));
}
catch (Exception e)
{
//Not Supposed to happen.
throw new RuntimeException();
}
}
public static String decrypt(String key, String data)
{
byte[] decoded_key;
byte[] decoded_data;
try
{
decoded_key = key.getBytes("UTF-8");
decoded_data = data.getBytes("UTF-8");
}
catch (Exception e)
{
//Not Supposed to happen.
throw new RuntimeException();
}
if(decoded_key.length != 16)
throw new IllegalArgumentException("Key length must be of 16 bytes. Given is " + decoded_key.length + ".");
try
{
return(IOUtils.toString(decrypt(decoded_key, decoded_data), "UTF-8"));
}
catch (Exception e)
{
//Not Supposed to happen.
throw new RuntimeException();
}
}
My unit tests are failing when decrypting. I ran a test where I compared a byte array of encoded UTF-8 data encoded_data
with IOUtils.toString(
encoded_data, "UTF-8").getBytes("UTF-8")
and for some reason they turned out to be different arrays altogether. No wonder my decryption algorithm is failing.
What is the proper procedure to convert from a java string to a UTF-8 byte array and back to a java string?
Upvotes: 2
Views: 9092
Reputation: 53694
the problem is that you are converting your encrypted data to a String. encrypted data is binary, not String data. UTF-8 is a charset with a specific encoding format. arbitrary binary data is not valid UTF-8 data. when you convert the encrypted data into a String, the "invalid" characters are most likely getting replaced with the ?
invalid char.
If you want to convert arbitrary binary data (aka encrypted data) into a String, you need to use some binary->text conversion like Base64.
Upvotes: 4
Reputation: 1126
I would try out checking first that the output of your encrypt method matches the one you are expecting with a unit test.
Also it's a good idea to use Base64 after the encryption so you can convert it to a string.
Another common issue is converting int to bytes as if they were unsigned ints. Bytes range is -128 to 127.
Upvotes: 0