Skip to content

Instantly share code, notes, and snippets.

@clzola
Last active May 22, 2016 15:50
Show Gist options
  • Save clzola/c42f22f27a76388a02e28bc7c208ba47 to your computer and use it in GitHub Desktop.
Save clzola/c42f22f27a76388a02e28bc7c208ba47 to your computer and use it in GitHub Desktop.
var LfsrCipher = function(s, p) {
this.key = s;
this.p = p;
this.generate = function(i) {
var t = 0;
for(var j=0; j<=p.length-1; j++)
t += p[j] * s[i+j];
t = t % 2;
s.push(t);
return t;
}
this.encrypt = function(message) {
this.s = this.key;
var cipherText = [];
for(var i=0; i<message.length; i++) {
cipherText.push(message[i] ^ this.generate(i));
}
return cipherText;
}
this.decrypt = function(cipherText) {
return this.encrypt(cipherText)
}
}
var cipher = new LfsrCipher([0, 1, 1, 0, 0], [1, 1, 0, 0, 1]);
var text = cipher.encrypt([1, 1, 1, 0, 0, 0, 1, 0, 1, 0]);
var message = cipher.decrypt(text);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment