// ignore_for_file: depend_on_referenced_packages

import 'dart:convert';
import 'package:encrypt/encrypt.dart';
import 'package:convert/convert.dart';
import 'package:crypto/crypto.dart' as crypto;

//encrypt
String encrypt(String key, String text) {
  var md5Key = md5(key);
  var key32 = Key.fromUtf8(md5Key);
  var iv16 = IV.fromUtf8(md5Key.substring(0, 16));
  final e = Encrypter(AES(key32, mode: AESMode.cbc));
  final encryptedData = e.encrypt(text, iv: iv16);
  return encryptedData.base64;
}

//dycrypt
String decrypt(String key, String text) {
  var md5Key = md5(key);
  var key32 = Key.fromUtf8(md5Key);
  var iv16 = IV.fromUtf8(md5Key.substring(0, 16));
  final e = Encrypter(AES(key32, mode: AESMode.cbc));
  final decryptedData = e.decrypt(Encrypted.fromBase64(text), iv: iv16);
  return decryptedData;
}

String md5(String data) {
    var content = Utf8Encoder().convert(data);
    var md5 = crypto.md5;
    var digest = md5.convert(content);
    return hex.encode(digest.bytes);
  }