Search code examples
javaflutterserversockettcpsocket

is there a Dart Equivalent of the following Aes Encryption function in java


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


Solution

  • The way you are configuring the Java cipher defaults to Electronic Code Book (ECB) mode which shouldn't be used in a real cipher system. You are also relying on the default padding - PKCS5.

    Use the pointycastle package, with the following imports:

    import 'package:pointycastle/api.dart';
    import 'package:pointycastle/block/aes_fast.dart';
    import 'package:pointycastle/paddings/pkcs7.dart';
    import 'package:pointycastle/padded_block_cipher/padded_block_cipher_impl.dart';
    import 'package:pointycastle/block/modes/ecb.dart';
    

    An approximate equivalent in Dart is:

    List<int> encrypt2(String text) {
      // key must be 16 or 32 bytes - not sure how "mykey" could work
      // key should really be binary, not a String! Better to use a KDF.
      Uint8List key = Uint8List.fromList(
        utf8.encode('0123456789abcdef'),
      );
    
      PaddedBlockCipher cipher = PaddedBlockCipherImpl(
        PKCS7Padding(), // Java defaults to PKCS5 which is equivalent
        ECBBlockCipher(AESFastEngine()), // Very weak mode - don't use this in the real world
      );
    
      cipher.init(
        true,
        PaddedBlockCipherParameters<CipherParameters, CipherParameters>(
          KeyParameter(key),
          null,
        ),
      );
    
      return cipher.process(utf8.encode(text)); // this isn't the same as .toBytes, except for ASCII
    }