Skip to content

Instantly share code, notes, and snippets.

@paralax
Created June 16, 2015 14:57
Show Gist options
  • Save paralax/90d9420c2aa7632f08ed to your computer and use it in GitHub Desktop.
Save paralax/90d9420c2aa7632f08ed to your computer and use it in GitHub Desktop.
simple LCG-based stream cipher
import sys
# def xor(b, s): return "".join(map(lambda x: chr(x^b), map(lambda x: ord(x), s)))
def xor(b, s): return map(lambda x: x^b, map(lambda x: ord(x), s))
M = sys.maxsize
M = 128
def lcg(m, a, c, x): return (a*x + c) % m
def enc(msg, seed):
res = []
for ch in msg:
res.extend(xor(lcg(M, 1664525, 1013904223, seed), ch))
seed = lcg(M, 1664525, 1013904223, seed)
return res
def dec(msg, seed):
res = []
for ch in msg:
res.append(lcg(M, 1664525, 1013904223, seed)^ch)
seed = lcg(M, 1664525, 1013904223, seed)
return ''.join(map(chr, res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment