I am doing a simple AES encryption in Java:
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, getAES128SecretKey());
byte[] encrypted = cipher.doFinal(input);
The output is converted to hex and stored in a database.
A later process (one written in C/C++) comes along and reads the hex, converts it to bytes and decrypts it.
The issue is apparently the C implementation correctly decrypts the text, but also keeps extra bytes at the end that are unnecessary.
For example (not real values):
Java: encrypt("eric") -> 1234567890FFFFFF1234567890FFFFFF (hex)
Java: decrypt("1234567890FFFFFF1234567890FFFFFF") -> "eric"
C: decrypt("1234567890FFFFFF1234567890FFFFFF") -> "eric XXXX XXXX XXXX"
I do not own the C decryption algorithm and the party using it has suggested I append a null terminator character '\0' to the Java bytes before encryption. My question is, would that work and should I even entertain that idea?
Reading the first answer (while “unaccepted”, it sounds correct to me) to the question Padding error when using AES encryption in Java and decryption in c, an explicit encoding of the padding size seems to be the correct way to go about this.
However, what if the string is encrypted in C and decrypted in C? Will the C encrypted string now have issues when decrypted in C, and removing the padding as if it were a Java encrypted string? Or would this be a non-issue?
The encryption / decryption process is largely irrelevant. After all, what comes in should be the same as what comes out. The question is what do the bytes representing the String object look like when written to a file.
A quick test confirms that when java writes a string to a stream neither the string nor the stream are null terminated. Consider the following code:
Doing a hexdump of the file that it produces the following.
Therefore, when you encrypt that you end up with an 15 bytes of encrypted string and blocksize – 15 bytes of encrypted padding. When you decrypt this you get not only the string but also the plain-text padding, and unless you know the length of the plain-text or have an embeded plain-text terminator there is no way to know where the plain-text ends.
Using NUL (‘\0’) as the terminator makes sense, because this is also the terminator that C uses to mark the end of string. The ony possible problem is if NUL can somehow occur in your string already. Given that this would probably cause other issues with C/C++ programs I kind of doubt that, but you could always escape it away if it was a problem.
Oh, and I checked changing
messagesas follows:Gives a hex dump of