Logo

Programming-Idioms

Create a function that XOR encrypts/decrypts a string
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
sysutils
encrypted := xorstring(key, original);
sub xor_crypt {
    my ($b, $k) = @_;
    return $b ^ $k;
}
from itertools import cycle
def xor(data, key):
	return ''.join(chr(ord(x)^ord(y)) for x,y in zip(data, cycle(key)))
def xor_string(str, key)
  ords = key.chars.map(&:ord).cycle
  str.chars.zip(ords).inject(""){|res, (c,o)| res << (c.ord ^ o) }
end
fn xor(s: Vec<u8>, key: &[u8]) -> Vec<u8> {
    let mut b = key.iter().cycle();
    s.into_iter().map(|x| x ^ b.next().unwrap()).collect()
}