I'm now able to use my binding with openHAB, but there are still issues to solve.
This time I got a problem with using the java.security library for decrypting the AES/CBC encoded frames coming from the W-MBus devices.
I use a method for decryption as shown below. But everytime i try a .doFinal() on the encrypted frame part i get a BadPaddingException.
I do know that the frame part must be of n*16 bytes. Any suggestions?
public static byte[] AES_decryption(byte[] aESblocks, byte[] coronaKey, AlgorithmParameterSpec ivSpec) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException
{
Cipher a = Cipher.getInstance("AES/CBC/NoPadding");
byte[] aesPlain = new byte[aESblocks.length];
byte[] encrypted = new byte[16];
byte[] decrypted = new byte[16];
Byte[] plain = new Byte[16];
SecretKeySpec sKeySpec = new SecretKeySpec(coronaKey, "AES");
a.init(Cipher.DECRYPT_MODE, sKeySpec, ivSpec);
for (int i=0;i<aESblocks.length/16;i++)
{
System.arraycopy(aESblocks, 16*i, encrypted, 0, 16);
WriteConsole("Encrypted Bytearray", encrypted);
decrypted = a.doFinal(encrypted);
for (int j = 0; j < 16; j++)
plain[j] = (byte)(ivSpec.toString().getBytes()[j] ^ decrypted[j]);
System.arraycopy(plain, 0, aesPlain, 16*i, 16);
System.arraycopy(encrypted, 0, ivSpec, 0, 16); // New initialisation vector
logger.warn("number of successful decrypted blocks: " + i);
}
return aesPlain;
}