I have an AES 128-bit encryption method which encrypts messages. But it does not encrypt messages which are longer than 45 characters. Therefore, i was thinking if i can do that by splitting messages with longer than 45 chars into an array of size 45 chars or array list, then i encrypt each block at a time.
So, if i have a message of long 90, i should to have two arrays of size 45 each (or array of size two and each of size 45chars). or an arraylist to do that. if i have a message of long 180 then i should have a block of 4 arrays.
UPDATE: So my initial issue was in not being able to send long messages using AES encryption in a client-server chat.
The following are the codes of AES implementation but without any padding or IV in it.
First i have a client; who sends messages encrypted using AESmsgEncryption class below.
Client class
public void send(String sendMessage) {
userName = GUI.getUserName();
//each index of the array will contain a substring of the send message (maximum length 45 char)
if (sendMessage.length() > 45) {
// String [] arrayOfStrings = new String[sendMessage.length()/45];
int sizeOfArray = sendMessage.length()/45;
System.out.println("CANT SEND MESSAGES LONGER THAN 45");
} else {
String ciphertext = msgAESEnDe.encryptAES(sendMessage, scrtkey);
System.out.println(userName + ": " + ciphertext + " <-- encrypted before sending");
out.println(userName + "#ciphertext$" + ciphertext);
out.flush();
}
}
Once the client sends out a message, the receiver then receives it at it is(encrypted) and sends it back to the online clients. Once the online clients receives the encrypted text message they decrypt the message using the following code In another receive method in the client class
String username = recMessage.substring(0, recMessage.indexOf(' '));
String ciphertxtWithNoName = recMessage.substring(recMessage.indexOf(':') + 2);
scrtkey = new SecretKeySpec(secrtKeyByte, "AES");
String plaintext = msgAESEnDe.decryptedPlain(ciphertxtWithNoName, scrtkey);
System.out.println(username +" " + plaintext +" <-- after decryption");
//current time
String time = log.format(new Date());
// Displaying received message.
ClientGUI.TA_CONVERSATION.append(time+ " ["+username.substring(0, username.length()-1) + "]: " + plaintext + "\n");
So in AESmsgEncryption class i have a method as follows;
public final String encryptAES(final String plaintext, SecretKey key) {
String ciphertext = new String();
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bytePlaintext = plaintext.getBytes();
byte[] byteCiphertext = cipher.doFinal(bytePlaintext);
ciphertext = new BASE64Encoder().encode(byteCiphertext);
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException: " + e);
} catch (NoSuchPaddingException e) {
System.out.println("NoSuchPaddingException: " + e);
} catch (InvalidKeyException e) {
e.printStackTrace();
System.out.println("InvalidKeyException: " + e);
} catch (IllegalBlockSizeException e) {
System.out.println("IllegalBlockSizeException: " + e);
} catch (BadPaddingException e) {
System.out.println("BadPaddingException: " + e);
}
return ciphertext;
}
public final String decryptedPlain(String ciphertext, SecretKey key) {
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte [] decodedValue = new Base64().decode(ciphertext.getBytes());
plaintext = cipher.doFinal(decodedValue);
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException: " + e);
} catch (NoSuchPaddingException e) {
System.out.println("NoSuchPaddingException: " + e);
} catch (InvalidKeyException e) {
System.out.println("InvalidKeyException: " + e);
} catch (IllegalBlockSizeException e) {
System.out.println("IllegalBlockSizeException: " + e);
} catch (BadPaddingException e) {
System.out.println("BadPaddingException: " + e);
}
return new String(plaintext);
}
The AES secret key is generated in the server side and sent encrypted using RSA, and then sent to each client, that is why in the client i have to give the scrtkey in the calling to the encryptAES and decryptedPlain methods.
To re state my problem; the above code cannot send messages longer than 45 chars, how can i make it send messages longer than 45 chars? If i needed to use padding and IV, the server should send the IV along with the secret key, right?
I hope the above description is clear.
How can i do that in JAVA?
Thanks