/*
// 线性移位反馈寄存器
function LFSR() {
this.n = 0;
this.c = [];
this.buffer = [];
}
LFSR.prototype.init = function (n, c, vals) {
//n: 级数
//c: 特征多项式系数【length=n】
//vals: 初始状态【length=n】
this.n = n;
this.c = c;
this.buffer = vals;
}
LFSR.prototype.touch = function () {
// 反馈
var anplus1 = 0;
for (var k = 0; k < this.n; k++) {
anplus1 = (anplus1 + this.c[k] * this.buffer[this.n - k - 1]) % 2;
}
// 输出值
var output = this.buffer[0];
// 移位
for (var k = 0; k < this.n - 1; k++) {
this.buffer[k] = this.buffer[k + 1];
}
// 反馈填充
this.buffer[this.n - 1] = anplus1;
return output;
}
// var lfsr = new LFSR();
// lfsr.init(8, [0, 0, 1, 0, 1, 1, 1, 0], [1, 1, 1, 0, 0, 0, 1, 0]);
// for (var k = 0; k < 10; k++) {
// console.log('------------')
// console.log(lfsr.touch());
// console.log(lfsr.buffer);
// }
// Geffe序列生成器
function Geffe() {
this.lfsr1 = new LFSR();
this.lfsr2 = new LFSR();
this.lfsr3 = new LFSR();
}
Geffe.prototype.init = function (n1, n2, n3, c1, c2, c3, vals1, vals2, vals3) {
this.lfsr1.init(n1, c1, vals1);
this.lfsr2.init(n2, c2, vals2);
this.lfsr3.init(n3, c3, vals3);
}
Geffe.prototype.touch = function () {
var o1 = this.lfsr1.touch();
var o2 = this.lfsr2.touch();
var o3 = this.lfsr3.touch();
if (o2 == 1) {
return "1" + o1;
} else {
return "0" + o3;
}
}
var g = new Geffe();
g.init(
8, 6, 7,
[0, 0, 1, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 0],
[1, 1, 1, 0, 0, 0, 1, 0], [1, 1, 0, 0, 1, 0], [1, 1, 0, 0, 0, 1, 0]
);
// for (var k = 0; k < 10; k++)
// console.log(g.touch());
// 加密
var mtext = "信息与计算科学";
var mbinstr = ""
for (var k = 0; k < mtext.length; k++) {
mbinstr += parseInt(encodeURI(mtext[k]).replace(/%/g, ""), 16).toString(2);
}
console.log(mbinstr)
// 对二进制字符串进行加密
var cbinstr = "";
for (var k = 0; k < mbinstr.length; k += 2) {
var output = g.touch();
var c1 = (parseInt(mbinstr[k]) + parseInt(output[0])) % 2;
var c2 = (parseInt(mbinstr[k + 1]) + parseInt(output[1])) % 2;
cbinstr += c1 + "" + c2;
}
console.log(cbinstr)
// 解密
g.init(
8, 6, 7,
[0, 0, 1, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 1, 0],
[1, 1, 1, 0, 0, 0, 1, 0], [1, 1, 0, 0, 1, 0], [1, 1, 0, 0, 0, 1, 0]
);
var mm = ""
for (var k = 0; k < cbinstr.length; k += 2) {
var output = g.touch();
var m1 = (parseInt(cbinstr[k]) + parseInt(output[0])) % 2;
var m2 = (parseInt(cbinstr[k + 1]) + parseInt(output[1])) % 2;
mm += m1 + "" + m2;
}
console.log(mm)
for (var k = 0; k < mm.length; k += 24) {
var mmblock1 = mm.substr(k, 8);
var mmblock2 = mm.substr(k + 8, 8);
var mmblock3 = mm.substr(k + 16, 8);
var uri = "%" + parseInt(mmblock1, 2).toString(16) + "%" + parseInt(mmblock2, 2).toString(16) + "%" + parseInt(mmblock3, 2).toString(16);
console.log(decodeURI(uri))
}
*/