I have set up a server that is listening for an encrypted byte array through the java function mentioned below. Earlier i was using java (android) to build my app so it was easy using the same java function but i am unable to figure out what is the dart equivalent(flutter) of that function which takes a string as input and returns an AES encrypted byte array as output ,which i can write to the tcp socket .
Also i'd really appreciate help on knowing how to write the generated byte array to the server and then read a similar response and decrypting it through dart (flutter)
I have been successful in writing simple strings and receiving simple strings as input and output to the tcp server through dart but cant do the same for encrypted byte arrays . In java i used DataOutputStream to send the response to server like this
DataOutputStream dOut = newDataOutputStream(socket.getOutputStream());
byte[] s2 = Encrypt3.encrypt2(myString);
dOut.writeInt(s2.length); // write length of the message
dOut.write(s2);
Here is the java function i used for aes encryption
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Encrypt3 {
public static String key = "mykey";
public static byte[] encrypt2(String text ){
String encrypt ="";
try{
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
return encrypted ;
}catch(Exception e){
System.out.println(e);
}
return null ;
}
public static String decrypt2(byte[] encrypted2){
String decrypt ="";
try{
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
decrypt = new String(cipher.doFinal(encrypted2));
}catch(Exception e){
System.out.println(e);
}
return decrypt ;
}
}
I expect the equivalent bytearray to be produced in dart when provided with a string as input,and then writing the byte array to a tcp server.
Thank You in advance