# coding: utf-8 # Add Reed-Solomon code # usage: python ecc.py flag.jpg flag_ecc.jpg import copy import sys import random # finit field # primitive polynomial: x^8+x^4+x^3+x^2+1=0 class GF: A = [1<>8: a ^= 0b100011101 A += [a] A += [0] Ai = [A.index(i) for i in range(256)] def __init__(s, v): assert not isinstance(v, GF) s.v = v.v if isinstance(v, GF) else v def __mul__(s, o): return GF(255) if 255 in (s.v, o.v) else GF((s.v+o.v)%255) def __add__(s, o): return GF(s.Ai[s.A[s.v]^s.A[o.v]]) def __str__(s): return str(s.v) def __repr__(s): return "GF(%s)"%repr(s.v) def __eq__(s, o): return s.v==o.v def __ne__(s, o): return s.v!=o.v def inv(s): assert s.v!=255 return GF((255-s.v)%255) # polynomial class Poly: def __init__(s, v): s.v = [x if isinstance(x,GF) else GF(x) for x in v] def __len__(s): return len(s.v) def __add__(s, o): return Poly([ (s.v[i] if i=len(o) assert o.v[-1]==GF(0) t = copy.deepcopy(s) for i in range(len(s)-len(o)+1)[::-1]: t += (o * Poly([t.v[len(o)+i-1]])).shift(i) del t.v[len(o)-1:] return t def __str__(s): return "[" + ", ".join(str(x) for x in s.v) + "]" def __repr__(s): return "Poly([" + ", ".join(repr(x) for x in s.v) + "])" def apply(s, x): t = GF(0) r = GF(255) for i in range(len(s)): r += s.v[i]*t t *= x return r def toarray(s): return [x.v for x in s.v] N = 255 K = 64 def ecc(data): assert len(data)==K I = Poly(map(ord, data)) # G(x) = Π[0<=i