Created
May 30, 2017 12:52
-
-
Save eruffaldi/c0a883f4374ede7677d04dc4e2204af9 to your computer and use it in GitHub Desktop.
Minimal Example of LFSR
This file contains 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
#https://en.wikipedia.org/wiki/Linear-feedback_shift_register | |
from bitstring import BitArray | |
def pad(x,K): | |
N = len(x) | |
PAD = N % K | |
if PAD > 0: | |
PAD = K-PAD | |
# tell me better | |
x = x + BitArray(bin='0'*PAD) | |
return x | |
def asbits(x): | |
if isinstance(x,BitArray): | |
return x | |
else: | |
return BitArray(bytes=x) | |
def lfsr(t,k,wn=0): | |
t = asbits(t) | |
k = asbits(k) | |
q = BitArray() | |
K = len(k) | |
N = len(t) | |
t = pad(t,K) | |
print "processing: ",N,"with key",K,"extended to",len(t) | |
for i in range(0,len(t),K): | |
q += t[i:i+K] ^ k | |
k.rol(1) | |
if wn != 0: | |
q = q[0:wn] | |
return q,N | |
def main(): | |
key = "ciao" | |
input = "ciaomondo" | |
w,wn = repeated(input,key) | |
print "encrypted as",len(w),"original len",wn | |
print repeated(w,key,wn)[0].bytes | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment