toDecrypt static method

Future<String> toDecrypt(
  1. String cipherString
)

Implementation

static Future<String> toDecrypt(String cipherString) async {
  _print(cipherString);

  try {
    final dynamic jsonMap = jsonDecode(cipherString);
    String? jsonStr = jsonMap['encrypted'];
    if (jsonStr == null || jsonStr.isEmpty) {
      logger.severe('It is Empty');
      return cipherString;
    }
    cipherString = jsonStr;
    String encVer = jsonMap['encryptVersion'] ?? '1.0.000000';
    if (encryptVersion.compareTo(encVer) != 0) {
      logger.warning('encryptVersion of json is different from sourceCode !!!');
      logger.warning('encryptVersion(sourceCode)=$encryptVersion');
      logger.warning('encryptVersion(json)=$encVer');
    } else {
      _print('encryptVersion=$encVer');
    }
  } catch (e) {
    logger.severe('It is not json file');
    return cipherString;
  }

  if (cipherString.length <= 2) {
    logger.severe('String is too short');
    return cipherString;
  }

  int ivTextLength = int.parse(cipherString.substring(0, 2));
  String ivText = cipherString.substring(2, ivTextLength + 2);
  cipherString = cipherString.substring(2 + ivTextLength);

  int fakeOffset = 0;
  try {
    fakeOffset = int.parse(cipherString.substring(0, 2));
  } catch (e) {
    logger.severe('Invalid String');
    return cipherString;
  }
  int offset = (fakeOffset - 16) * 2; // realOffset,
  int keyPosition = offset + 2;

  String keyString = cipherString.substring(keyPosition, keyPosition + 32);

  if (cipherString.length <= keyPosition + 32 + offset) {
    logger.severe('String is too short2');
    return cipherString;
  }

  String context = cipherString.substring(keyPosition + 32, cipherString.length - offset);

  encrypt.Key key = encrypt.Key.fromUtf8(keyString);
  encrypt.IV iv = encrypt.IV.fromBase64(ivText);
  final encryptor = encrypt.Encrypter(encrypt.AES(key));
  final normalText = encryptor.decrypt64(context, iv: iv);

  _print('offset=$offset');
  _print('key=$keyString');
  _print('text=$normalText');

  return normalText;
}