fun xorEncryptDecrypt(input: String, key: String): String {
return input.mapIndexed { index, char ->
char xor key[index % key.length]
}.joinToString("")
}
infix fun Char.xor(other: Char): Char {
return this.code.xor(other.code).toChar()
}
fun main() {
val originalText = "May the Force be with you."
val key = "Obelus" // String key for XOR operation
// Encrypt the string
val encryptedText = xorEncryptDecrypt(originalText, key)
println("Encrypted: $encryptedText")
// Decrypt the string (same function, same key)
val decryptedText = xorEncryptDecrypt(encryptedText, key)
println("Decrypted: $decryptedText")
}
/*
run:
Encrypted: L *B# *B U& L :L
Decrypted: May the Force be with you.
*/