没什么好说的,简单无脑!
#include<iostream>
using namespace std;int ECB()
{ int duan[4]; int messageLen = 16; //需要加密数据的长度 int encLen = 4; //加密分段的长度 int key[4] = {1,0,1,0}; // ECB OFB CFB CTR 初始密码 CBC IV// CTR 计数器从0开始,每组二进制加1,大小 4*sizeof(int) int initVec[8] = {0,1,1,0,1,0,0,1}; int message[16] = {1,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0}; //明文 int ciphertext[16]; //密文 int a; a = messageLen/encLen; int b=0; int c=0; for(int i=1;i<=a;i++) { for(int j=0;j<4;j++) { c=b+j; duan[j]=message[c]; if(duan[j]==key[j]) { duan[j]=0; } else { duan[j]=1; } ciphertext[c]=duan[j]; } b=b+encLen; } for(int w=0;w<messageLen;w++) { cout<<ciphertext[w]; cout<<" "; if(w%4==3) cout<<endl; } return 0;}int CBC()
{ int duanchu[4]; int duan[4]; int messageLen = 16; //需要加密数据的长度 int encLen = 4; //加密分段的长度 int key[4] = {1,0,1,0}; // ECB OFB CFB CTR 初始密码 CBC IV// CTR 计数器从0开始,每组二进制加1,大小 4*sizeof(int) int initVec[8] = {0,1,1,0,1,0,0,1}; int message[16] = {1,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0}; //明文 int ciphertext[16]; //密文 int a; a = messageLen/encLen; int b=0; int c=0; for(int o=0;o<4;o++) { duanchu[o]=message[o]; } for(int i=1;i<=a;i++) { for(int j=0;j<4;j++) { c=b+j; duan[j]=message[c]; if(duan[j]==duanchu[j]) { duan[j]=0; } else { duan[j]=1; } if(duan[j]==key[j]) { duan[j]=0; } else { duan[j]=1; } ciphertext[c]=duan[j]; } b=b+encLen; } for(int w=0;w<messageLen;w++) { cout<<ciphertext[w]; cout<<" "; if(w%4==3) cout<<endl; } return 0;}int OFB()
{ int xiangliang[4]={1,0,1,0}; int duan[4]; int messageLen = 16; //需要加密数据的长度 int encLen = 4; //加密分段的长度 int key[4] = {1,0,1,0}; // ECB OFB CFB CTR 初始密码 CBC IV// CTR 计数器从0开始,每组二进制加1,大小 4*sizeof(int) int initVec[8] = {0,1,1,0,1,0,0,1}; int message[16] = {1,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0}; //明文 int ciphertext[16]; //密文 int a; a = messageLen/encLen; int b=0; int c=0; for(int i=1;i<=a;i++) { for(int j=0;j<4;j++) { c=b+j; duan[j]=message[c]; if(xiangliang[j]==key[j]) { xiangliang[j]=0; } else { xiangliang[j]=1; } if(duan[j]==xiangliang[j]) { duan[j]=0; } else { duan[j]=1; } ciphertext[c]=duan[j]; } b=b+encLen; } for(int w=0;w<messageLen;w++) { cout<<ciphertext[w]; cout<<" "; if(w%4==3) cout<<endl; } return 0;}int CFB()
{ int xiangli[6]; int xiangliang[6]={1,0,1,0,0,0}; int duan[4]; int messageLen = 16; //需要加密数据的长度 int encLen = 4; //加密分段的长度 int key[4] = {1,0,1,0}; // ECB OFB CFB CTR 初始密码 CBC IV// CTR 计数器从0开始,每组二进制加1,大小 4*sizeof(int) int initVec[8] = {0,1,1,0,1,0,0,1}; int message[16] = {1,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0}; //明文 int ciphertext[16]; //密文 int a; a = messageLen/encLen; int b=0; int c=0; for(int i=1;i<=a;i++) { for(int j=0;j<4;j++) { c=b+j; duan[j]=message[c]; if(xiangliang[j]==key[j]) { xiangli[j]=0; } else { xiangli[j]=1; } if(duan[j]==xiangli[j]) { duan[j]=0; } else { duan[j]=1; }ciphertext[c]=duan[j];
} for(int w=0;w<6;w++) { if(w<=1) { xiangliang[w]=xiangliang[w+4]; } else { xiangliang[w]=duan[w-2]; } } b=b+encLen; } for(int w=0;w<messageLen;w++) { cout<<ciphertext[w]; cout<<" "; if(w%4==3) cout<<endl; } return 0;}int CTR()
{ int shuchuduan[4]; int couter[4]={0}; int duan[4]; int messageLen = 16; //需要加密数据的长度 int encLen = 4; //加密分段的长度 int key[4] = {1,0,1,0}; // ECB OFB CFB CTR 初始密码 CBC IV// CTR 计数器从0开始,每组二进制加1,大小 4*sizeof(int) int initVec[8] = {0,1,1,0,1,0,0,1}; int message[16] = {1,0,0,1,0,0,0,1,1,1,1,1,0,0,0,0}; //明文 int ciphertext[16]; //密文 int a; a = messageLen/encLen; int b=0; int c=0; for(int i=1;i<=a;i++) { for(int j=0;j<4;j++) { c=b+j; duan[j]=message[c]; if(couter[j]==key[j]) { shuchuduan[j]=0; } else { shuchuduan[j]=1; } if(duan[j]==shuchuduan[j]) { duan[j]=0; } else { duan[j]=1; } ciphertext[c]=duan[j]; } b=b+encLen; for(int s=3;s>=0;s--) { if(couter[s]==0) { couter[s]=1; if(s<3) { for(int r=3;r>s;r--) { couter[r]=0; } } break; } } } for(int w=0;w<messageLen;w++) { cout<<ciphertext[w]; cout<<" "; if(w%4==3) cout<<endl; } return 0;}int main()
{ // show init message cout<<"ECB"<<endl; ECB(); cout<<"CBC"<<endl; CBC(); cout<<"CTR"<<endl; CTR(); cout<<"CFB"<<endl; CFB(); cout<<"OFB"<<endl; OFB(); return 0;}