解析都在代码里
/*
_/_/_/
_/ _/_/_/ _/_/ _/_/_/ _/_/_/ _/ _/_/
_/ _/ _/ _/_/_/_/ _/_/ _/ _/ _/_/
_/ _/ _/ _/ _/_/ _/ _/ _/
_/_/_/ _/_/_/ _/_/_/ _/_/_/ _/_/_/ _/
*/
#include <iostream>
#include <string>
#define N 4 // 定义分组数
using namespace std;
string multiplication(char* str,int a[N][N]); // 适用于 N*N 与 N*N 的矩阵相乘
class cryption{
string plaintext;// 明文
string encrypt;// 加密文
string decrypt;// 解密文
public:
cryption(string a)
: plaintext(a){}
string out_en(){return this->encrypt;}
string out_de(){return this->decrypt;} // 对密文的赋值,用于解密
void input_en(string en){this->encrypt=en;}
void encryption(int a[N][N],int b[N]); // 加密函数
void decryption(int aa[N][N],int b[N]); // 解密函数
friend string multiplication(char* str,int a[N][N]);
};
void cryption::encryption(int a[N][N],int b[N])
{
string ssr;
string str=this->plaintext;
int len=str.size();
for(int i=0;i<len/N;i++){
ssr+=multiplication(&(str[N*i]),a); // 矩阵乘法 a*str[N*i:N*i+N]=ssr[N*i:N*i+N]
}
for(int j=0;j<len;j++)ssr[j]=(ssr[j]+b[j%N])%128; // (ssr[j]+b[j%N])mod 128 ->ASCII码
this->encrypt=ssr;
}
void cryption::decryption(int aa[N][N],int b[N])
{
string ssr;
string str=this->encrypt;
int len=str.size();
for(int i=0;i<len;i++)str[i]=(str[i]-b[i%N])%128; // (str[i]-b[i%N])mod 128
for(int j=0;j<len/N;j++){
ssr+=multiplication(&(str[N*j]),aa); // 矩阵乘法 a*str[N*j:N*j+N]=ssr[N*i:N*i+N]
}
this->decrypt=ssr;
}
void is_right(int a[N][N],int aa[N][N]); // 判断密钥正确性
void is_right(int a[N][N],int aa[N][N])
{
int b[N][N]={0};
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
{
for(int k=0;k<N;k++)
b[i][j]+=a[i][k]*aa[k][j]; // 矩阵乘法
}
}
int flag=1;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
cout<<b[i][j]%128<<" ";
if(b[i][j]!=1&&i==j)flag=0;
if(b[i][j]!=0&&i!=j)flag=0;
}
cout<<endl;
}
if(flag)cout<<"密钥正确"<<endl;
else{
cout<<"密钥错误"<<endl;
exit(1);
}
}
string multiplication(char* str,int a[N][N])
{
string s="";
char c=0;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
c=(c+a[i][j]*str[j]+12800)%128; // 矩阵乘法
s+=c;
c=0;
}
return s;
}
int main()
{
// 输入部分
cout<<"请输入加密密钥 a:"<<endl;
int a[N][N],b[N],aa[N][N];
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cin>>a[i][j];
}
cout<<"请输入加密密钥 b:"<<endl;
for(int i=0;i<N;i++)
cin>>b[i];
cout<<"请输入解密密钥 a:"<<endl;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
cin>>aa[i][j];
}
//0 0 1 2 0 0 3 5 2 -1 0 0 -5 3 0 0
//1 21 8 17
//0 0 3 1 0 0 5 2 -5 2 0 0 3 -1 0 0
// int a[N][N]={0,0,1,2,0,0,3,5,2,-1,0,0,-5,3,0,0};
// int b[N]={1,21,8,17};
// int aa[N][N]={0,0,3,1,0,0,5,2,-5,2,0,0,3,-1,0,0}; // 题目所给密钥不适用于所有编码,且有错误
string str;
is_right(a,aa);
getchar();
getline(cin,str);
int len=str.size();
if(len%N){
for(int k=0;k<N-len%N;k++)
str+=" "; // 长度不是N的倍数补空格
}
cryption test(str);
test.encryption(a,b);
test.decryption(aa,b);
cout<<test.out_en()<<endl;
cout<<test.out_de()<<endl;
return 0;
}
贴个原始版本来丢人
#include <iostream>
#include <string>
// 标点符号支持 " " "," "."
using namespace std;
void is_right(int a[4][4],int aa[4][4]);
string encryption(string str,int a[4][4],int b[4]);
string decryption(string str,int aa[4][4],int b[4]);
string multiplication(char* str,int a[4][4]);
string multiplication(char* str,int a[4][4])
{
string s="abcd";
for(int i=0;i<4;i++){
s[i]='A'+(a[i][0]*(str[0]-'A')+a[i][1]*(str[1]-'A')+a[i][2]*(str[2]-'A')+a[i][3]*(str[3]-'A')+2900)%29;
}
return s;
}
void is_right(int a[4][4],int aa[4][4])
{
int b[4][4];
int sum=0,flag=1;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++)
{
b[i][j]=a[i][1]*aa[1][j]+a[i][2]*aa[2][j]+a[i][3]*aa[3][j]+a[i][0]*aa[0][j];
}
}
int i,j;
for(i=0;i<4;i++){
for(j=0;j<4;j++)
{
cout<<b[i][j]%29<<" ";
}
cout<<endl;
}
}
string encryption(string str,int a[4][4],int b[4])
{
string ss="";
int len=str.size();
int sum=0;
for(int i=0;i<len/4;i++){
ss+=multiplication(&(str[4*i]),a);
}
for(int i=0;i<len;i++)ss[i]=(ss[i]+b[i%4]-'A')%29 +'A';
return ss;
}
string decryption(string str,int aa[4][4],int b[4])
{
string ss="";
int len=str.size();
int sum=0;
for(int i=0;i<len;i++)str[i]=(str[i]-b[i%4]-'A'+29)%29+'A';
for(int i=0;i<len/4;i++){
ss+=multiplication(&(str[4*i]),aa);
}
for(int k=0;k<len;k++){
if(ss[k]=='[') ss[k]=' ';
else if(ss[k]=='/') ss[k]=',';
else if(ss[k]==']') ss[k]='.';
}
return ss;
}
int main()
{
int n=4,i,j;
int a[4][4]={0,0,1,2,0,0,3,5,2,-1,0,0,-5,3,0,0};
int b[4]={1,21,8,17};
int aa[4][4]={0,0,3,1,0,0,5,2,-5,2,0,0,3,-1,0,0};
// for(i=0;i<n;i++){
// for(j=0;j<n;j++)
// cin>>a[i][j];
// }
// for(i=0;i<n;i++)
// cin>>b[i];
// for(i=0;i<n;i++){
// for(j=0;j<n;j++)
// cin>>aa[i][j];
// }
is_right(a,aa);
string str,encrypt,decrypt;
getline(cin,str);
int len=str.size();
if(len%4){
for(int k=0;k<4-len%4;k++)
str+=" ";
}
for(int k=0;k<len;k++){
if(str[k]==' ') str[k]='[';
else if(str[k]==',') str[k]='/';
else if(str[k]=='.') str[k]=']';
}
encrypt=encryption(str,a,b);
cout<<encrypt<<endl;
decrypt=decryption(encrypt,aa,b);
cout<<decrypt<<endl;
return 0;
}