the code of MH backpack en/decipher algorithm
来源:互联网 发布:centos 安装 vmware 编辑:程序博客网 时间:2024/04/29 07:00
/*made by vito_hdq*/#include<stdio.h>#include<iostream>#include<time.h>#include<fstream> #include<math.h>#include<conio.h>#define N 16using namespace std;class People{public: People(); void Calc_My_PK();public: int A[N];private: int B[N]; int M; int w; int w_1; int Tao[N]; int Tao_1[N];friend void MH_encipher(char *cleartext ,int num_of_cleartext,int * & ciphertext,People & P_you);friend void MH_decipher(int * ciphertext ,int num_of_ciphertext,char * &cleartext,People & P_me);};People::People(){ ifstream in("MH.txt"); for(int a=0;a<N;a++) in>>B[a]; in>>M; in>>w; for(int a=0;a<N;a++) in>>Tao[a]; in>>w_1; for(int a=0;a<N;a++) in>>Tao[a]; Calc_My_PK();}void People::Calc_My_PK(){ int C[N]; for(int i=0;i<N;i++) { A[i]=(B[i]*w)%M; } //for(int i=0;i<N;i++) //{ //A[Tao[i]]=C[i]; // } }void Two_char_map_Int_16(char *Mychar,int * temp){ int num=(int)*Mychar; for(int i=0;i<8;i++) { if(num>=pow(2,7-i)) {temp[i]=1;num-=pow(2,7-i);} else temp[i]=0; } num=(int)*(Mychar+1); int temp2[N]; for(int i=0;i<8;i++) { if(num>=pow(2,7-i)) {temp2[i]=1;num-=pow(2,7-i);} else temp2[i]=0; } for(int i=0;i<8;i++) temp[i+8]=temp2[i];}void * Int16_map_Two_char(int *Myint,char * tempchar){ int tempint=0; for(int i=0;i<8;i++) { if(*(Myint+7-i)==1) tempint+=pow(2,i); } *tempchar=tempint; tempint=0; for(int i=8;i<16;i++) { if(*(Myint+15+8-i)==1) tempint+=pow(2,i-8); } *(tempchar+1)=tempint;} void MH_encipher(char * cleartext ,int num_of_cleartext,int * & ciphertext,People & P_you){ for(int k=0,i=0;k<(num_of_cleartext/2);k++,i+=2) { char temp_cleart[2]; temp_cleart[0]=*(cleartext+i); temp_cleart[1]=*(cleartext+i+1); int *temp=new int [N]; Two_char_map_Int_16(temp_cleart,temp); *(ciphertext+k)=0; for(int j=0;j<N;j++) { *(ciphertext+k)+=( P_you.A[j]*(*(temp+j)) ); } }}void MH_decipher(int * ciphertext ,int num_of_ciphertext,char * &cleartext,People & P_me){ for(int i=0,k=0;i<num_of_ciphertext;i++,k+=2) { int Temp[N]; int temp_ciphert;temp_ciphert=( *(ciphertext+i)*P_me.w_1 )%P_me.M;for(int i=0;i<N;i++) { if(temp_ciphert>=P_me.B[N-1-i]) { Temp[N-i-1]=1; temp_ciphert-=P_me.B[N-1-i]; } else Temp[N-i-1]=0;} char Tempchar[2];Int16_map_Two_char(Temp,Tempchar); *(cleartext+k)= Tempchar[0];*(cleartext+k+1)= Tempchar[1]; }}int main(){ People Alice; char *cleartext=new char[1000]; int *ciphertext=new int[1000]; //cleartext[0]='b'; //cleartext[1]='b'; //Two_char_map_Int_16(cleartext,ciphertext); //for(int i=0;i<16;i++) cout<<*(ciphertext+i)<<" "; //Int16_map_Two_char(ciphertext,cleartext); //for(int i=0;i<2;i++) cout<<*(cleartext+i)<<" "; //for(int i=0;i<2;i++) //MH_decipher(ciphertext,1,cleartext,Alice); int num; cout<<"please input the length of cleartext:"; cin>>num; //getch();cout<<"发送明文:";scanf(" "); for(int i=0;i<num;i++)cin>>noskipws>>cleartext[i];MH_encipher(cleartext,num,ciphertext,Alice); cout<<"密文:"; for(int i=0;i<num/2;i++)cout<<ciphertext[i]<<" "; cout<<"\n解密为:\n"; MH_decipher(ciphertext,num/2,cleartext,Alice);for(int i=0;i<num;i++)cout<<cleartext[i]; }
0 0
- the code of MH backpack en/decipher algorithm
- Decipher the internal structure of some webpage
- The power of algorithm
- the code of love
- Backpack
- backpack
- backpack
- backpack
- Backpack
- Algorithm: The implementation of Trie Tree (C++)
- the Algorithm Complexity of Lazy Evaluation
- Overview of the Marching Cubes Algorithm
- Algorithm of the Week: Merge Sort
- [the summarization of algorithm]Heap and Heapsort
- 算法的重要性-The Importance of Algorithm()
- The introduction of first order algorithm
- Efficient Variants of the ICP Algorithm
- deassemble the code of MBR boot code
- Solr6.2.1 学习笔记(二)从数据库导入数据
- IOS界面开发之UIView
- 小记4
- 人家的js笔记
- nodejs 配置shadowsocks
- the code of MH backpack en/decipher algorithm
- SSM框架——使用MyBatis Generator自动创建代码
- Spring - 注解实现AOP的五种通知
- Linux安装ftp组件
- 外观模式
- request.setAttribute()用法
- ASP.NET管道和.NET Core管道
- Swift 懒加载
- (图文)手把手教你怎样集成极光推送