码迷,mamicode.com
首页 > 其他好文 > 详细

一个非常简陋的基于LFSR和Feistel网络的加解密运算,有位处理。C

时间:2016-11-29 13:51:33      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:lap   ima   复制   log   ges   ever   str   nbsp   getc   

刚布置的热乎作业,马上就写了。。

如果是正在做这个作业的同学搜到别抄,写得垃圾不要抄不要抄不要抄,

特别是吉大的同学,谢谢= =

技术分享
  1 #include<stdio.h>
  2 #include<math.h>
  3 #define N 128
  4 #define index int 
  5 
  6 
  7 void FFunc(char *Result, char *R, char key,int len)
  8 {
  9     char temp=(Result[0]&0x00000001)^(key&0x00000001) ;
 10     for(int i=1;i<len;i++)
 11         Result[i]=R[i-1];
 12     R[0]=temp;
 13 }
 14 
 15 bool compare(char*st1,char*st2,int len)
 16 {
 17     for(int i=0;i<len;i++)
 18         if(st1!=st2) return 0; 
 19 }
 20 void OrFunc(char* Result,char *A,char *B,int len)
 21 {
 22     for(index i=0;i<len;i++)
 23         Result[i]=(A[i]&0x00000001)^(B[i]&0x00000001); 
 24 }
 25 
 26 
 27 //复制 A[iA:iA+len-1]=B[iB:iB+len-1]
 28 void strCopy(char *A, int iA, char *B, int iB, int len) 
 29 {
 30     char*p = &A[iA], *q = &B[iB];
 31     while(len--) *p++ = *q++;
 32 }
 33 
 34 
 35 void strOutput(char* str,int len)
 36 {
 37     for(int i=0;i<len;i++)
 38         printf("%d",str[i]);
 39 }
 40 
 41 void Reverse(char* str1, char*str2,int len)
 42 {
 43     for(int i=0;i<len;i++)
 44         str1[i]=str2[len-i-1];
 45 }
 46 
 47 
 48 void getLFSR_8(char *outString,index n)//生成n长M序列 
 49 {
 50     char X[8] ={0,1,0,0,0,0,0,1};
 51     for(index i=0;i<8;i++) outString[i]=X[i];
 52     for(index i=8;i<n;i++)
 53     {
 54         outString[i] = X[7]^X[3]^X[0];
 55         for(index j=7;j>0;j--)
 56             X[j] = X[j-1];
 57         X[0] = outString[i];
 58     }
 59 } 
 60 
 61 
 62 void getKey(char *key)//生成密钥 
 63 {
 64     char temp[64];
 65     getLFSR_8(temp,64);
 66     for(index i=34,j=0;i<50;i++,j++) 
 67     {
 68         key[j]=temp[i];
 69     }
 70 }
 71 
 72 
 73 void tranStrToBit(int length, char* sStr, /*out*/char* sNum)
 74 {
 75     index iStr=0,iBit=0,k=0;
 76     char cOut=0;
 77     
 78     for(iStr=0; iStr+7<length; iStr+=8)
 79     {
 80         // 每八位转化成一个unsigned char
 81         for(iBit=0; iBit<8; iBit++)
 82             cOut |= sStr[iStr+iBit]<<(7-iBit);
 83         sNum[k++] = cOut;
 84         cOut = 0;
 85     }
 86     if(iStr==length) return;
 87     
 88     //处理最后剩下的,不足则补全为0 
 89     cOut = 0; 
 90     for(iBit=0; iStr<length; iStr++,iBit++)
 91         cOut |= sStr[iStr+iBit]<<(7-iBit);
 92 }
 93 
 94 
 95 void tranBitToStr(int codelength, char* sNum, /*out*/char* sStr)
 96 {
 97     index k=0;
 98     int  strLengtha = codelength/8.0;
 99     index icoded=0;
100     unsigned char cTmp;
101     unsigned char flag=0x80;//10000000
102     while(k<strLengtha)
103     {
104         flag = 0x80;
105         cTmp = (unsigned char)sNum[k++];
106         for(index i=0; i<8; ++i,++icoded)
107         {
108             if(cTmp & flag)
109                 sStr[icoded] = 1;
110             else sStr[icoded] = 0;
111             flag = flag >> 1;
112         }
113         
114     }
115 }
116 
117 
118 void FeistailEncry(char* Result,int length,char* Key,char *Clear)
119 {
120     if(length%2 != 0) {printf("异常\n");return;}
121     int w = length/2;
122     char R[w], L[w];
123     char TempF[w];
124     char TempL[w];
125     
126     strCopy(L, 0, Clear, 0, w);
127     strCopy(R, 0, Clear, w, w);
128     strOutput(L,w);strOutput(R,w);
129     for(int turn=1; turn<17; turn++)
130     {
131         strCopy(TempL,0,L,0,w);
132         strCopy(L, 0, R, 0, w);//L=Ri-1
133         FFunc(TempF, R, Key[turn-1],w);
134         OrFunc(R, TempL, TempF, w);
135         printf("\n第%d轮后:\n",turn);
136         strOutput(L,w);strOutput(R,w);
137     }
138     strCopy(Result,0,R,0,w);
139     strCopy(Result,w,L,0,w);
140     printf("\n输出为:\n");
141     strOutput(Result,length); 
142 }
143 
144 
145 
146 int main()
147 { 
148     char sKey[16];
149     char sKey_16[2];
150     char sKey_reverse[16];
151     char sClearString[N];
152     char sClearString1[N];
153     char sCipherString[N];
154     char sCipherString1[N];
155     char sClearText[17];
156     char sCipherText[17];
157     char sClearText1[17];     
158     
159     getKey(sKey);
160     tranStrToBit(16,sKey,sKey_16);
161     printf("生成密钥为:\n\t");
162     for(int i=0;i<16;i++)
163         printf("%d",sKey[i]); 
164     
165     printf("\n请输入明文: \n\t");
166     scanf("%s",&sClearText);
167     printf("\n正在加密……\n");
168     tranBitToStr(N,sClearText,sClearString);
169     strOutput(sClearString,N); 
170     FeistailEncry(sCipherString,N,sKey,sClearString);    
171     printf("加密结果为:\n\t");
172     tranStrToBit(N,sCipherString,sCipherText);
173     sCipherText[17]=\n;
174     printf("%s",sCipherText);
175     
176     getchar();
177     getchar();
178     printf("\n\n正在解密……\n");
179     tranBitToStr(N,sCipherText,sCipherString1);
180     FeistailEncry(sClearString1,N,sKey_reverse,sCipherString1);
181     tranStrToBit(N,sClearString1,sClearText1);
182     sClearText1[17]=\n;
183     printf("\n解密结果:\n\t%s\n",sClearText);
184 }  
View Code

 

一个非常简陋的基于LFSR和Feistel网络的加解密运算,有位处理。C

标签:lap   ima   复制   log   ges   ever   str   nbsp   getc   

原文地址:http://www.cnblogs.com/bnpop/p/6113106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!