Created
June 16, 2015 14:57
-
-
Save paralax/90d9420c2aa7632f08ed to your computer and use it in GitHub Desktop.
simple LCG-based stream cipher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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