It seems like the issue lies in how you're updating the cipher with the message. When you call `cipher.update(msg)`, it's only processing a single block of data. For short messages like "Hello, World!", this might work fine, but for longer messages, it's not sufficient. The `update()` method of `Cipher` is typically used for streaming encryption, where you encrypt or decrypt data in chunks.
To fix this issue, you need to handle longer messages correctly. You can either read the message in chunks and call `update()` for each chunk, or you can directly pass the entire message to `doFinal()`. Since you're encrypting the entire message at once, you can use `doFinal()` directly.
Here's the modified part of your code:
```java
// Configure cipher
ctrl.cipher = Cipher.getInstance("AES");
ctrl.cipher.init(Cipher.ENCRYPT_MODE, ctrl.key);
// Generate ciphertext
byte[] cipherTxt = ctrl.cipher.doFinal(msg);
String encrypted = new String(cipherTxt);
System.out.println("Encryption: " + encrypted);
// Decrypt Cipher
ctrl.cipher.init(Cipher.DECRYPT_MODE, ctrl.key);
byte[] decryptedMsg = ctrl.cipher.doFinal(cipherTxt);
String decrypted = new String(decryptedMsg, "UTF8");
System.out.println("Decrypted: " + decrypted);
```
This change ensures that the entire message is encrypted and decrypted correctly, regardless of its length.
Furthermore, for those seeking further
help with Java assignment, there are various online platforms like
programminghomeworkhelp.com and communities available that offer support and guidance in programming tasks.