密碼學學完序列密碼后要做關于Geffe的攻擊,,,
看來看去就分割攻擊比較好寫但是這個LFSR2的級數太高了好像寫的有點問題
不管了,快期未考試了
非線性組合模型 G e f f e Geffe Geffe分割攻擊
一、非線性組合模型 G e f f e Geffe Geffe???發生器的基本介紹
G e f f e Geffe Geffe發生器是1973年 G e f f e Geffe Geffe?設計的一個密鑰流生成器,具體描述如下:

如圖所示, G e f f e Geffe Geffe發生器使用了3個 L F S R LFSR LFSR??移位暫存器他們以非線性方式組合,其中, L F S R 1 LFSR1 LFSR1和 L F S R 3 LFSR3 LFSR3作為復合器的輸入, L F S R 2 LFSR2 LFSR2?作為控制復合器,
其中,三個 L F S R LFSR LFSR?的反饋多項式分別為:
f 1 ( x ) = x 20 ⊕ x 3 ⊕ 1 f_1(x)=x^{20}\oplus x^3\oplus 1 f1?(x)=x20⊕x3⊕1?
f 2 ( x ) = x 39 ⊕ x 4 ⊕ 1 f_2(x)=x^{39}\oplus x^4\oplus 1 f2?(x)=x39⊕x4⊕1
f 3 ( x ) = x 17 ⊕ x 3 ⊕ 1 f_3(x)=x^{17}\oplus x^3\oplus 1 f3?(x)=x17⊕x3⊕1
非線性組合函式
F
F
F為:
g
(
x
1
,
x
2
,
x
3
)
=
x
1
x
2
⊕
(
x
2
⊕
1
)
x
3
=
{
x
1
,
x
2
=
1
x
3
,
x
2
=
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?)=x1?x2?⊕(x2?⊕1)x3?={x1?,x2?=1x3?,x2?=0?
二、非線性組合模型 G e f f e Geffe Geffe的初始化程序
(1)密鑰 K = ( k 63 , … , k 0 ) K=(k_{63},…,k_0) K=(k63?,…,k0?)按照如下方式填充:
先將密鑰重復鏈接,得到76位元密鑰 K ’ K’ K’
K ’ = ( k 11 , … , k 1 , k 0 , k 63 , … , k 0 ) K’=(k_{11},…, k_1,k_0,k_{63},…,k_{0}) K’=(k11?,…,k1?,k0?,k63?,…,k0?)??
將 K ’ K’ K’由低位到高位按照從右到左的順序依次填入 L F S R 1 LFSR1 LFSR1、 L F S R 2 LFSR2 LFSR2和 L F S R 3 LFSR3 LFSR3,
2)按照密鑰流生成方式空轉100個時刻
將此時 L F S R LFSR LFSR?的內部狀態做為密鑰流生成的初態,
三、非線性組合模型 G e f f e Geffe Geffe?分割攻擊流程
1.窮舉 L F S R 2 LFSR2 LFSR2的初態
2.獲得關于 L F S R 1 LFSR1 LFSR1和 L F S R 3 LFSR3 LFSR3初態的線性方程;
3.解方程得到初態
4.根據前100個時刻的亂數,驗證初態是否正確
四、分割攻擊具體實作代碼及注釋
1.定義密鑰流 K K K?的值并將 K K K?填充到三個 L F S R LFSR LFSR??的初態中
string K="1010010010000101010100101010110101101100100111001011010001110011";//K為初始密鑰
string s1,s2,s3;//s1,s2,s3中分別為LFSR1 2 3的初始填充
s1="11001011010001110011";
s2="100100001010101001010101101011011001001";
s3="01000111001110100";
2.將 L F S R LFSR LFSR?空轉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.根據 L F S R 2 LFSR2 LFSR2?的值來決定最后輸出密鑰流的每一位取 L F S R 1 LFSR1 LFSR1還是取 L F S R 3 LFSR3 LFSR3??的值
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.列舉 L F S R 2 LFSR2 LFSR2的初態
int cnt =0;
int x_len = 1<<39;
for(int i=0;i<x_len;i++){
//中間流程由后面具體解釋
}
5.根據 L F S R 2 LFSR2 LFSR2?的初態,推算出 L F S R 1 LFSR1 LFSR1?和 L F S R 3 LFSR3 LFSR3?的初態
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.判斷是否存在解不出來 L F S R 1 LFSR1 LFSR1或 L F S R 3 LFSR3 LFSR3??的情況
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.如果 L F S R 1 LFSR1 LFSR1?和 L F S R 3 LFSR3 LFSR3?的初態均已解出,判斷由此時 L F S R 1 LFSR1 LFSR1??, L F S R 3 LFSR3 LFSR3?和 L F S R 2 LFSR2 LFSR2?所生成的??密鑰與真實值是否相等
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;
}
程式運行展示:

因
L
F
S
R
2
LFSR2
LFSR2級數太高,無法在短時間內結束
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/395030.html
標籤:其他
上一篇:你下載國家反詐中心APP了嗎
下一篇:Redteam2靶場攻略(從外網 log4j2 RCE 再到內網核彈組合拳漏洞 CVE-2021-42287、CVE-2021-42278 拿到 DC)
