decrypt function

Future<String> decrypt({
  1. required String str,
  2. String? key,
})

Implementation

Future<String> decrypt({required String str, String? key}) async {
  String encrypted = str.trim().replaceAll(RegExp(r'\s+'), '').replaceAll("\\r", '').replaceAll("\\n", "");
  key = key ?? MD<ApiConstants>().encryptKey;
  m.debugPrint("key: $key");
  try {
    final encrypter = _buildEncrypter(key);
    final decrypted = encrypter.decrypt(Encrypted.from64(encrypted),
        iv: IV(Uint8List.fromList(_buildIV(key))));
    // jsonDecode is used to check if the decrypted string is a valid json
    // if it is not a valid json, it will throw an exception
    // try {
    //   jsonDecode(decrypted);
    // } catch (e) {
    //   return decrypted;
    // }
    return decrypted;
  } catch (e) {
    throw Exception('Decryption error: $e');
  }
}