AES 128 DOT NET and Java Compatibility

We tried a prototype scheme in which we encrypt decrypted data between two systems: one in .NET and the other in Java. We were going to use simple 128-bit AES encryption.

The problem I am facing is trivial, but I can not find the right solution. Perhaps my understanding of AES or Encryption is generally less.

Assuming we have a predefined key represented by the following hexadecimal string: "9c361fec3ac1ebe7b540487c9c25e24e". This is a 16 byte key. The Java encryption part will be

  final byte[] rawKey = hexStringToByteArray("9c361fec3ac1ebe7b540487c9c25e24e");
  final SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
  // Instantiate the cipher
  final Cipher cipher = Cipher.getInstance("AES");
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

  final byte[] encrypted = cipher.doFinal(plainText.getBytes());

The hexStringToByteArray function converts a hexadecimal string into an array of bytes. The problem is that bytes are signed in java. Thus, the value of 9C is -100, not 156 (as in .NET).

In Java, it becomes: -100,54,31,-20,58,-63,-21,-25,-75,64,72,124,-100,37,-30,78

However, in .NET this is: 156,54,31,236,58,193,235,231,181,64,72,124,156,37,226,78

: , , ? CBC PADDING.

: .

+3
1

, . . , - unsigned... .

, - .

+3

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


All Articles