import 'dart:convert';

void main() {
  String mmst = 'security_token12345678';
  String encrypted = encryptXOR('this is a sensitive API key or access token', mmst);
  String encBase64 = base64.encode(encrypted.codeUnits);
  
  print('encrypted and base64: $encBase64');
  
  String decoded = String.fromCharCodes(base64.decode(encBase64));
  String decrypted = decryptXOR(decoded, mmst);
  
  print('decoded: $decoded');
  
  print('decrypted: $decrypted');
}

String encryptXOR(String plaintext, String key) {

  List<int> plainBytes = utf8.encode(plaintext);

  List<int> keyBytes = utf8.encode(key);

  List<int> cipherBytes = [];

  for (int i = 0; i < plainBytes.length; i++) {

    cipherBytes.add(plainBytes[i] ^ keyBytes[i % keyBytes.length]);

  }

  return utf8.decode(cipherBytes);

}



String decryptXOR(String ciphertext, String key) {

  List<int> cipherBytes = utf8.encode(ciphertext);

  List<int> keyBytes = utf8.encode(key);

  List<int> plainBytes = [];

  for (int i = 0; i < cipherBytes.length; i++) {

    plainBytes.add(cipherBytes[i] ^ keyBytes[i % keyBytes.length]);

  }

  return utf8.decode(plainBytes);

}