So, I and 2 friends are currently working on a project. One of them is programming a server, the other one a website, and me an app. At first, our programs should communicate with each other without encryption. This has also worked well. But now we wanted to encrypt the messages using AES 256 CBC. And that's when the problems started. At first, the encryption itself did not work properly, but now it works.
However, when sending the encrypted message, some characters are not sent. Apparently, the characters below 32, i.e. all values that have only the value "control" according to the UTF8 table. However, I do not know how to work around the problem. we have also tested if it is not the server, but neither the website nor a quickly written Java program had this problem. Therefore we assume that the error is on my side.
Here is the code I use to encrypt:
String encrypt(String pKey, pIV, msg){
final key = Key.fromUtf8(pKey);
final iv = IV.fromUtf8(pIV);
final encrypter = Encrypter(AES(key, mode: AESMode.cbc));
final encrypted = encrypter.encrypt(msg, iv: iv);
String utf8Msg = "";
for(int i = 0;i<encrypted.bytes.length;i++ ){
utf8Msg += String.fromCharCode(encrypted.bytes[i]);
}
print("###AES encrypted text: ${utf8Msg}") ;
return utf8Msg;
And this is the code I use to send my Messages:
Future<void> run(String ip, int port, String request, key, iv) async {
i = 0;
String _request = request;
Completer<String> _completer = new Completer<String>();
print("Request: $_request");
_errorData = "Server_Error";
if (_request != null) {
print("Request has data");
socket = await Socket.connect(ip, port);
socket.encoding = utf8;
socket.listen((data) {
//code for the received messages
},
onError: ((error, StackTrace trace) {
_secureResponse = _errorData;
print("(2) $_secureResponse");
_completer.completeError(_secureResponse);
}),
onDone: () {
print("finish");
_completer.complete(_secureResponse);
socket.destroy();
},
cancelOnError: false);
print("Socket sends data");
socket.write(encrypt(key, iv, msg))
} else {
_secureResponse = _errorData;
print("(4) $_secureResponse");
}
return _completer.future;
}
You should use a data-type that's not a UTF-8 string, but the dart equivalent of a Buffer, which I think might be ByteBuffer?