Encrypt a string in Java so that C can decrypt it without any additions to the decrypted text?

I am doing 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 the database.

Next is the process (one written in C / C ++) and reads the hexadecimal code, converts it to bytes and decrypts it.

The problem is that the C implementation correctly decrypts the text, but also saves extra bytes at the end that are not needed.

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 suggested that I add a null-terminator character '\0'to the Java bytes before caching . My question is, will this work, and should I even accept this idea?

( "", ) AES- Java c, , , .

, , C C? C C , Java? ?

+3
3

/ . , , , , , . , , String, .

, , java , , . :

import java.io.FileWriter;
import java.io.IOException;

public SimpleStringIoTest
{
  public static void main (String[] args) throws IOException
  {
    String message = "Raw Java String";
    FileWriter fileWriter = new FileWriter("javaoutput.bin");
    fileWriter.write(message);
    fileWriter.close();
  }
}

hexdump , .

00000000  52 61 77 20 41 61 76 61 20 53 74 72 69 6e 67     Raw Java String

, , 15 - 15 . , , , , , .

NUL ('\ 0') , , C . , NUL - . , , , C/++, , , .

, messages :

String message = "Raw Java String\0";

00000000  52 61 77 20 41 61 76 61 20 53 74 72 69 6e 67 00  Raw Java String
+1

AES , , 16 . C , , . .

+1

NULL . Java (PKCS # 5), , . . , . - '\ 0', , C.

Cipher.getInstance(), . , Cipher.getInstance("AES/CBC/PKCS5PADDING"), Sun JCE, .

+1
source

Source: https://habr.com/ru/post/1761140/


All Articles