XOREncryption/Groovy/XOREncryption.groovy

19 lines
471 B
Groovy
Raw Permalink Normal View History

2013-10-07 00:40:20 +04:00
def encryptDecrypt(String input) {
def key = ['K', 'C', 'Q'] //Can be any chars, and any length array
def output = []
for(int i = 0; i < input.length(); i++) {
2013-10-07 00:44:44 +04:00
int a = input.charAt(i) as int;
int b = (key[i % key.size()] as char) as int
output.add((a ^ b) as char)
2013-10-07 00:40:20 +04:00
}
output.join("")
}
def encrypted = encryptDecrypt("kylewbanks.com")
println("Encrypted:" + encrypted)
def decrypted = encryptDecrypt(encrypted)
println("Decrypted:" + decrypted)