textEncryption static method

String textEncryption(
  1. String plainText
)

Encrypts plainText with a new random key and returns the cipher text.

The key is hidden inside the returned cipher text, so TextDecryption.textDecryption needs nothing else to decrypt it.

Supported characters are English and Arabic letters, digits, the space and a set of symbols. Any other character (for example , or a line break) throws an ArgumentError.

Implementation

static String textEncryption(String plainText) {
  // Setup letter to encryption and decryption
  LettersInit.instance.setupLetters();

  // generate Key
  KeyModel keyModel = GenerateKey.generateNewKey();

  // Convert Plain String to list of plain int
  List<int> plainToIntList = _convertPlainToInt(plainText);

  // Encrypt list<int>
  int i = 0;
  List<int> encryptedInts = [];
  while (i < (keyModel.key) + (keyModel.key % 263)) {
    encryptedInts = _encryptListInt(
      i == 0 ? plainToIntList : encryptedInts,
      keyModel.key,
    );
    ++i;
  }

  //  Convert list<int> to cipher text
  String encryptedString =
      _convertEncryptedIntToEncryptedString(encryptedInts);

  // Hide key in cipher text to extract it when decrypt
  String encryptedStringWithKey = _addKeyToCipheredText(
    encryptedString,
    keyModel.letters,
  );

  String encryptedStringWithKeyWithLength =
      _addLengthToCipheredText(encryptedStringWithKey);

  return encryptedStringWithKeyWithLength;
}