applyXorEncrypt method

String applyXorEncrypt(
  1. String key
)

Applies XOR operation between the this string and the key string.

The input string is converted to a list of UTF-16 code units, and the key string is also converted to a list of UTF-16 code units. The XOR operation is then applied between each code unit of the input string and the corresponding code unit of the key string. If the key string is shorter than the input string, the key will be repeated cyclically.

Returns the result of the XOR operation as a new string.

Implementation

String applyXorEncrypt(String key) {
  List<int> inputBytes = codeUnits;
  List<int> keyBytes = key.codeUnits;
  List<int> resultBytes = [];

  for (int i = 0; i < inputBytes.length; i++) {
    resultBytes[i] = inputBytes[i] ^ keyBytes[i % keyBytes.length];
  }

  return String.fromCharCodes(resultBytes);
}