密码学学完序列密码后要做关于Geffe的攻击。。。
看来看去就分割攻击比较好写但是这个LFSR2的级数太高了好像写的有点问题
不管了,快期未考试了
非线性组合模型GeffeGeffeGeffe分割攻击
一、非线性组合模型GeffeGeffeGeffe发生器的基本介绍
GeffeGeffeGeffe发生器是1973年GeffeGeffeGeffe设计的一个密钥流生成器,具体描述如下:

如图所示,GeffeGeffeGeffe发生器使用了3个LFSRLFSRLFSR移位寄存器他们以非线性方式组合,其中,LFSR1LFSR1LFSR1和LFSR3LFSR3LFSR3作为复合器的输入,LFSR2LFSR2LFSR2作为控制复合器。
其中,三个LFSRLFSRLFSR的反馈多项式分别为:
f1(x)=x20⊕x3⊕1f_1(x)=x^{20}\oplus x^3\oplus 1f1(x)=x20⊕x3⊕1
f2(x)=x39⊕x4⊕1f_2(x)=x^{39}\oplus x^4\oplus 1f2(x)=x39⊕x4⊕1
f3(x)=x17⊕x3⊕1f_3(x)=x^{17}\oplus x^3\oplus 1f3(x)=x17⊕x3⊕1
非线性组合函数FFF为:
g(x1,x2,x3)=x1x2⊕(x2⊕1)x3={x1,x2=1x3,x2=0
g(x_1,x_2,x_3)=x_1x_2\oplus(x_2\oplus1)x_3=
\begin{cases}
x_1,\quad x_2=1\\
x_3,\quad x_2=0
\end{cases}
g(x1,x2,x3)=x1x2⊕(x2⊕1)x3={x1,x2=1x3,x2=0
二、非线性组合模型GeffeGeffeGeffe的初始化过程
(1)密钥K=(k63,…,k0)K=(k_{63},…,k_0)K=(k63,…,k0)按照如下方式填充:
先将密钥重复链接,得到76比特密钥K’K’K’
K’=(k11,…,k1,k0,k63,…,k0)K’=(k_{11},…, k_1,k_0,k_{63},…,k_{0})K’=(k11,…,k1,k0,k63,…,k0)
将K’K’K’由低位到高位按照从右到左的顺序依次填入LFSR1LFSR1LFSR1、LFSR2LFSR2LFSR2和LFSR3LFSR3LFSR3。
2)按照密钥流生成方式空转100个时刻
将此时LFSRLFSRLFSR的内部状态做为密钥流生成的初态。
三、非线性组合模型GeffeGeffeGeffe分割攻击流程
1.穷举LFSR2LFSR2LFSR2的初态
2.获得关于LFSR1LFSR1LFSR1和LFSR3LFSR3LFSR3初态的线性方程;
3.解方程得到初态
4.根据前100个时刻的乱数,验证初态是否正确
四、分割攻击具体实现代码及注释
1.定义密钥流KKK的值并将KKK填充到三个LFSRLFSRLFSR的初态中
string K="1010010010000101010100101010110101101100100111001011010001110011";//K为初始密钥
string s1,s2,s3;//s1,s2,s3中分别为LFSR1 2 3的初始填充
s1="11001011010001110011";
s2="100100001010101001010101101011011001001";
s3="01000111001110100";
2.将LFSRLFSRLFSR空转100个时刻并记录下产生的密钥流
void LFSR_f1(int l_xunhuan,string chutai){//LFSR1的实现代码,其中l_xunhuan为循环轮数,chutai为LFSR的初态
bitset<20>f1(chutai);//初态赋值
//cout<<f1.to_string();
for(int i=1;i<=l_xunhuan;i++){//移位寄存
int j=f1[0]^f1[17] ;
len_f1++;
//string s_out = f1.to_string();
f1_out += '0'+j;
f1.operator>>=(1);
f1[19]=j;
}
}
void LFSR_f2(int l_xunhuan,string chutai){//LFSR2的实现代码,其中l_xunhuan为循环轮数,chutai为LFSR的初态
bitset<39>f2(chutai);//初态赋值
//cout<<f2.to_string();
for(int i=1;i<=l_xunhuan;i++){//移位寄存
int j=f2[0]^f2[35] ;
len_f2++;
//string s_out = f2.to_string();
f2_out += '0'+j;
f2.operator>>=(1);
f2[38]=j;
}
}
void LFSR_f3(int l_xunhuan,string chutai){//LFSR2的实现代码,其中l_xunhuan为循环轮数,chutai为LFSR的初态
bitset<17>f3(chutai);//初态赋值
//cout<<f3.to_string();
for(int i=1;i<=l_xunhuan;i++){//移位寄存
int j=f3[0]^f3[14] ;
len_f3++;
//cout<<i<<endl;
//cout<<"f3_out--->"<<f3_out<<endl;
//string s_out = f3.to_string();
f3_out += j+'0' ;
f3.operator>>=(1);
f3[16]=j;
}
}
//此为主函数的调用语句
LFSR_f1(100,s1);
LFSR_f2(100,s2);
LFSR_f3(100,s3);
3.根据LFSR2LFSR2LFSR2的值来决定最后输出密钥流的每一位取LFSR1LFSR1LFSR1还是取LFSR3LFSR3LFSR3的值
string f0_out;//为最终输出密钥流
for(int i=0;i<100;i++){
if(f2_out[i]=='1') f0_out+=f1_out[i];
else f0_out+=f3_out[i];
}
4.枚举LFSR2LFSR2LFSR2的初态
int cnt =0;
int x_len = 1<<39;
for(int i=0;i<x_len;i++){
//中间流程由后面具体解释
}
5.根据LFSR2LFSR2LFSR2的初态,推算出LFSR1LFSR1LFSR1和LFSR3LFSR3LFSR3的初态
int cnt =0;
int x_len = 1<<39;
for(int i=0;i<x_len;i++){
string sheng_chu; //生成枚举LFSR2的字符串
for(int j=0;j<39;j++){
int k=(i>>j) & 1;
sheng_chu+=(k+'0');
}
f2_out="";
f3_out="";
f1_out="";
LFSR_f2(100,sheng_chu);
string sheng_f1(100,'2');//初始化LFSR1
string sheng_f3(100,'2');//初始化LFSR2
for(int j=0;j<100;j++){
if(f2_out[j]=='0') sheng_f1[j]=f0_out[j];
else sheng_f3[j]=f0_out[j];
}
string sheng(20,'2');
sheng_f1=sheng+sheng_f1;
bool flag =true;
//不断的根据已有关系求解 f1
while(flag){
flag=false;
for(int j=119;j>=20;j--){
int x=sheng_f1[j-20]-'0',y=sheng_f1[j-3]-'0';
if(sheng_f1[j]!='2' && (x==2) && (y!=2)){
sheng_f1[j-20]= (y ^ (sheng_f1[j]-'0'))+'0';
flag=true ;
}
else if(sheng_f1[j]!='2' && (x!=2) && (y==2)){
sheng_f1[j-3]= (x ^ (sheng_f1[j]-'0'))+'0';
flag=true ;
}
else if(sheng_f1[j]=='2' && (x!=2) && (y!=2)){
sheng_f1[j]=(x^y)+'0';
flag=true;
}
}
}
// for(int i=0;i<20;i++) cout<<sheng_f1[i];
// cout<<endl;
string sheng3(17,'2');
sheng_f3=sheng3+sheng_f3;
flag =true;
//不断的根据已有关系求解 f3
while(flag){
flag=false;
for(int j=116;j>=17;j--){
int x=sheng_f3[j-17]-'0',y=sheng_f3[j-3]-'0';
if(sheng_f3[j]!='2' && (x==2) && (y!=2)){
sheng_f3[j-17]= (y ^ (sheng_f3[j]-'0'))+'0';
flag=true ;
}
else if(sheng_f3[j]!='2' && (x!=2) && (y==2)){
sheng_f3[j-3]= (x ^ (sheng_f3[j]-'0'))+'0';
flag=true ;
}
else if(sheng_f3[j]=='2' && (x!=2) && (y!=2)){
sheng_f3[j]=(x^y)+'0';
flag=true;
}
}
}
// for(int i=0;i<17;i++) cout<<sheng_f3[i];
// cout<<endl;
}
}
6.判断是否存在解不出来LFSR1LFSR1LFSR1或LFSR3LFSR3LFSR3的情况
bool check_f1=true,check_f3=true;
string temp_f1="",temp_f3="";
for(int i=0;i<20;i++){
if(sheng_f1[i]=='2') {
check_f1=false;//判断是否存在有解不出来的情况
break;
}
temp_f1+=sheng_f1[i];
}
for(int i=0;i<17;i++){
if(sheng_f3[i]=='2') {
check_f3=false;//判断是否存在有解不出来的情况
break;
}
temp_f3+=sheng_f3[i];
}
7.如果LFSR1LFSR1LFSR1和LFSR3LFSR3LFSR3的初态均已解出,判断由此时LFSR1LFSR1LFSR1,LFSR3LFSR3LFSR3和LFSR2LFSR2LFSR2所生成的密钥与真实值是否相等
if(check_f1 && check_f3) {
//cnt++;
f1_out="";
f3_out="";
//cout<<"-1"<<endl;
LFSR_f1(100,temp_f1);//根据f1初始值生成密钥流
//cout<<"-2"<<endl;
//cout<<sheng_f3<<endl;
LFSR_f3(100,temp_f3);//根据f3初始值生成密钥流
//cout<<sheng_f3<<endl;
//cout<<"-3"<<endl;
string f_sheng_out;
//if() f_sheng_out=;
for(int i=0;i<100;i++){ //根据枚举的f2,f1,f3的值, 生成密钥流
if(f2_out[i]=='1') f_sheng_out+=f1_out[i];
else f_sheng_out+=f3_out[i];
}
if(f_sheng_out == f0_out) {//判断是否匹配成功
printf("YES\n");
cout<<temp_f1<<"\n"<<temp_f3<<endl;
}else{
printf("NO\n");
cout<<temp_f1<<"\n"<<temp_f3<<endl;//输出此时计算出的f1,f3
}
}
五、完整代码及运行结果
#include <iostream>
#include <cstdlib>
#include <bitset>
using namespace std;
string f1_out,f2_out,f3_out;
int len_f1,len_f2,len_f3;
void LFSR_f1(int l_xunhuan,string chutai){
bitset<20>f1(chutai);
//cout<<f1.to_string();
for(int i=1;i<=l_xunhuan;i++){
int j=f1[0]^f1[17] ;
len_f1++;
//string s_out = f1.to_string();
f1_out += '0'+j;
f1.operator>>=(1);
f1[19]=j;
}
}
void LFSR_f2(int l_xunhuan,string chutai){
bitset<39>f2(chutai);//初态赋值
//cout<<f2.to_string();
for(int i=1;i<=l_xunhuan;i++){
int j=f2[0]^f2[35] ;
len_f2++;
//string s_out = f2.to_string();
f2_out += '0'+j;
f2.operator>>=(1);
f2[38]=j;
}
}
void LFSR_f3(int l_xunhuan,string chutai){
bitset<17>f3(chutai);//初态赋值
//cout<<f3.to_string();
for(int i=1;i<=l_xunhuan;i++){
int j=f3[0]^f3[14] ;
len_f3++;
//cout<<i<<endl;
//cout<<"f3_out--->"<<f3_out<<endl;
//string s_out = f3.to_string();
f3_out += j+'0' ;
f3.operator>>=(1);
f3[16]=j;
}
}
int main(){
// string sy="01010101100010110111";
// // 01234567890123456789
// LFSR_f1(100,sy);
// cout<<f1_out;
string K="1010010010000101010100101010110101101100100111001011010001110011";
string s1,s2,s3;
s1="11001011010001110011";
s2="100100001010101001010101101011011001001";
s3="01000111001110100";
{
// bitset<20> sy(s1);
// for(int i=0;i<20;i++) cout<<sy[i]<<" ";
// sy.operator<<=(1);
// string ssyy=sy.to_string();
// cout<<endl;
// for(int i=0;i<20;i++) cout<<sy[i]<<" ";
// cout<<endl;
}
LFSR_f1(100,s1);
LFSR_f2(100,s2);
LFSR_f3(100,s3);
string f0_out;
for(int i=0;i<100;i++){
if(f2_out[i]=='1') f0_out+=f1_out[i];
else f0_out+=f3_out[i];
}
//
cout<<"LFSR1--->"<<f1_out<<endl;
cout<<"LFSR2--->"<<f2_out<<endl;
cout<<"LFSR3--->"<<f3_out<<endl;
cout<<"LFSR输出--->"<<f0_out<<endl;
//long long ii=1<<39-1;
// string
// for(int i=0;i<39;i++){
// if((ii>>i & 1)==1)
// }
int cnt =0;
int x_len = 1<<30;
for(int i=0;i<x_len;i++){
string sheng_chu;
for(int j=0;j<39;j++){
int k=(i>>j) & 1;
sheng_chu+=(k+'0');
}
//sheng_chu="100100001010101001010101101011011001001";
f2_out="";
f3_out="";
f1_out="";
LFSR_f2(100,sheng_chu);
string sheng_f1(100,'2');
string sheng_f3(100,'2');
for(int j=0;j<100;j++){
if(f2_out[j]=='0') sheng_f1[j]=f0_out[j];
else sheng_f3[j]=f0_out[j];
}
// cout<<sheng_f1<<endl;
// cout<<sheng_f3<<endl;
string sheng(20,'2');
sheng_f1=sheng+sheng_f1;
bool flag =true;
//不断的根据已有关系求解 f1
while(flag){
flag=false;
for(int j=119;j>=20;j--){
int x=sheng_f1[j-20]-'0',y=sheng_f1[j-3]-'0';
if(sheng_f1[j]!='2' && (x==2) && (y!=2)){
sheng_f1[j-20]= (y ^ (sheng_f1[j]-'0'))+'0';
flag=true ;
}
else if(sheng_f1[j]!='2' && (x!=2) && (y==2)){
sheng_f1[j-3]= (x ^ (sheng_f1[j]-'0'))+'0';
flag=true ;
}
else if(sheng_f1[j]=='2' && (x!=2) && (y!=2)){
sheng_f1[j]=(x^y)+'0';
flag=true;
}
}
}
// for(int i=0;i<20;i++) cout<<sheng_f1[i];
// cout<<endl;
string sheng3(17,'2');
sheng_f3=sheng3+sheng_f3;
flag =true;
//不断的根据已有关系求解 f3
while(flag){
flag=false;
for(int j=116;j>=17;j--){
int x=sheng_f3[j-17]-'0',y=sheng_f3[j-3]-'0';
if(sheng_f3[j]!='2' && (x==2) && (y!=2)){
sheng_f3[j-17]= (y ^ (sheng_f3[j]-'0'))+'0';
flag=true ;
}
else if(sheng_f3[j]!='2' && (x!=2) && (y==2)){
sheng_f3[j-3]= (x ^ (sheng_f3[j]-'0'))+'0';
flag=true ;
}
else if(sheng_f3[j]=='2' && (x!=2) && (y!=2)){
sheng_f3[j]=(x^y)+'0';
flag=true;
}
}
}
// for(int i=0;i<17;i++) cout<<sheng_f3[i];
// cout<<endl;
bool check_f1=true,check_f3=true;
string temp_f1="",temp_f3="";
for(int i=0;i<20;i++){
if(sheng_f1[i]=='2') {
check_f1=false;//判断是否存在有解不出来的情况
break;
}
temp_f1+=sheng_f1[i];
}
for(int i=0;i<17;i++){
if(sheng_f3[i]=='2') {
check_f3=false;//判断是否存在有解不出来的情况
break;
}
temp_f3+=sheng_f3[i];
}
if(check_f1 && check_f3) {
//cnt++;
f1_out="";
f3_out="";
//cout<<"-1"<<endl;
LFSR_f1(100,temp_f1);//根据f1初始值生成密钥流
//cout<<"-2"<<endl;
//cout<<sheng_f3<<endl;
LFSR_f3(100,temp_f3);//根据f3初始值生成密钥流
//cout<<sheng_f3<<endl;
//cout<<"-3"<<endl;
string f_sheng_out;
//if() f_sheng_out=;
for(int i=0;i<100;i++){ //根据枚举的f2,f1,f3的值, 生成密钥流
if(f2_out[i]=='1') f_sheng_out+=f1_out[i];
else f_sheng_out+=f3_out[i];
}
if(f_sheng_out == f0_out) {//判断是否匹配成功
printf("YES\n");
cout<<temp_f1<<"\n"<<temp_f3<<endl;
}else{
printf("NO\n");
cout<<"LFSR1="<<temp_f1<<"\n"<<"LFSR3="<<temp_f3<<endl;//输出此时计算出的f1,f3
}
}
}
//cout<<cnt;
}
程序运行展示:

因LFSR2LFSR2LFSR2级数太高,无法在短时间内结束
本文介绍了Geffe发生器,一个基于三个高阶线性反馈移位寄存器(LFSR)的密钥流生成器。详细阐述了其初始化过程,并详细讨论了针对该系统的分割攻击方法,包括穷举LFSR2的初态,建立线性方程,解方程以及验证解的正确性。同时,提供了C++代码实现攻击流程,但指出由于LFSR2的高阶,攻击在实际中可能面临计算复杂性问题。
2928





