#include <iostream>
using namespace std;
//return next state
unsigned char fn_feedback(unsigned char n,
unsigned char c,
unsigned char curr_state)
{
unsigned char t = c & curr_state;
unsigned char s = t & (unsigned char)128;
//get first bit
for(int i =1; i< n; i++)
{
s = s ^ ( (t << i) & (unsigned char)128 );
}
return (unsigned char)((curr_state << 1) | (s >> (n-1))); //return next state
}
void lfsr_output_byte(int n,
unsigned char c,
unsigned char init_state,
unsigned char output_bytes[],
int byte_length)
{
unsigned char next_state = init_state;
for(int i = 0; i < byte_length; i++)
{
unsigned char temp = (unsigned char)0;
for(int j=0; j< 8; j++)
{
temp = temp | ( (next_state & (unsigned char)128) >> j );
next_state = fn_feedback(n, c, next_state);
}
output_bytes[i] = temp;
}
}
//output a char type of data in binary way
void output_binary(unsigned char c)
{
for(int i=0; i< 8; i++)
{
if( (c<< i) & (unsigned char)128 )
cout << "1";
else
cout << "0";
}
}
void Geffe(unsigned char buf[],
unsigned char buf1[],
unsigned char buf2[],
unsigned char b[])
{
for(int i=0; i< 10; i++)
{
b[i]=buf[i]*buf1[i]+buf2[i]*buf1[i]+buf2[i];
output_binary( (unsigned char)b[i]);
}
}
void JK(unsigned char buf[],unsigned char buf1[],unsigned char c[])
{
for(int i=1; i< 11; i++)
{
c[i]=(buf[i]+buf1[i]+1)*c[i-1]+buf[i];
output_binary( (unsigned char)c[i]);
}
}
void cypt(unsigned char b[])
{
unsigned char cypher[10],cyph[10];
cout<<"input the cyphertext:";
cin>>cypher;
int j;
for(j=0;cypher[j]!='\0';j++)
{
cyph[j]=cypher[j]^b[j];
cout<<cyph[j];
}
cout<<endl;
for(int k=0;k<j;k++)
{
cypher[k]=cyph[k]^b[k];
cout<<cypher[k];
}
cout<<endl;
}
int main(int argc, char* argv[])
{
unsigned char buf[10],buf1[10],buf2[10],b[100],c[100];
// 函数f 初始状态152
lfsr_output_byte(5,(unsigned char)144,(unsigned char)152, buf, 10);
lfsr_output_byte(5,(unsigned char)44,(unsigned char)152, buf1, 10);
lfsr_output_byte(5,(unsigned char)24,(unsigned char)152, buf2, 10);
Geffe(buf,buf1,buf2,b);
cout<<endl;
c[0]=0;
JK(buf,buf1,c);
cout<<endl;
cout<<"Geffe operate:"<<endl;
cypt(b);
cout<<endl;
cout<<"J-K operate:"<<endl;
cypt(c);
return 0;
}
jk.rar_JK密码_密码学_流密码c++实现
版权申诉
73 浏览量
2022-09-23
03:30:36
上传
评论
收藏 734KB RAR 举报
alvarocfc
- 粉丝: 112
- 资源: 1万+
最新资源
- 苹果CMS红色red1电影影视主题网站模板
- 光大证券-20170725-阻力支撑相对强度(RSRS)选股-技术指标系列报告之三.pdf
- EMV 4.4 Book 1~4
- 1_pt_cfix .xlsx
- EMV 4.3 Book 1~4
- EMV 4.2 Book 1~4
- EMV 4.1 Book 1~4
- 基于Python实现的遥感图像的语义分割,分别使用Deeplab V3+和unet模型+源代码+文档说明+数据集.zip
- Python基于Django服装仓库进销存库存管理系统+源代码+文档说明+数据库(高分毕设)
- 2024魔改php版大屏微信墙互动展示系统 大屏幕互动系统程序动态背景图和配乐素材含搭建教程
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0