【密码学】多表代换

本文介绍了一种使用矩阵乘法进行加密和解密的算法,通过cryption类实现字符串加密,利用定义的加密函数和解密函数,配合密钥验证函数确保正确性。实例展示了如何输入密钥并处理不同长度的字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解析都在代码里

/*
     _/_/_/                                                    
   _/          _/_/_/    _/_/      _/_/_/    _/_/_/  _/  _/_/   
  _/        _/    _/  _/_/_/_/  _/_/      _/    _/  _/_/        
 _/        _/    _/  _/            _/_/  _/    _/  _/           
  _/_/_/    _/_/_/    _/_/_/  _/_/_/      _/_/_/  _/         
                                                                      */
#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;
}	



代换密码是一种替代加密技术,它将明文中的每个字母都替换成密文字母。可以使用C语言来实现这种密码。 以下是一个简单的代换密码的C语言代码示例: ```c #include <stdio.h> #include <string.h> // 定义密钥 char table[26] = {'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'}; // 加密函数 void encrypt(char *plaintext, char *ciphertext) { int len = strlen(plaintext); for (int i = 0; i < len; i++) { if (plaintext[i] >= 'a' && plaintext[i] <= 'z') { ciphertext[i] = table[plaintext[i] - 'a']; } else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') { ciphertext[i] = table[plaintext[i] - 'A']; } else { ciphertext[i] = plaintext[i]; } } ciphertext[len] = '\0'; } // 解密函数 void decrypt(char *ciphertext, char *plaintext) { int len = strlen(ciphertext); for (int i = 0; i < len; i++) { if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') { for (int j = 0; j < 26; j++) { if (table[j] == ciphertext[i]) { plaintext[i] = 'a' + j; break; } } } else if (ciphertext[i] >= 'A' && ciphertext[i] <= 'Z') { for (int j = 0; j < 26; j++) { if (table[j] == ciphertext[i]) { plaintext[i] = 'A' + j; break; } } } else { plaintext[i] = ciphertext[i]; } } plaintext[len] = '\0'; } int main() { char plaintext[100]; char ciphertext[100]; char decryptedtext[100]; printf("请输入明文:"); fgets(plaintext, 100, stdin); encrypt(plaintext, ciphertext); printf("加密后的密文:%s\n", ciphertext); decrypt(ciphertext, decryptedtext); printf("解密后的明文:%s\n", decryptedtext); return 0; } ``` 在这个例子中,我们定义了一个密钥,它包含了26个字母的替代字母。加密函数接收明文作为输入,并将其转换为密文。解密函数接收密文作为输入,并将其转换回明文。最后,我们在主函数中使用这些函数来加密和解密一段文字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赤城封雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值