多字符代换密码

一、什么是多字符代换密码

所谓多字符代换密码,其实是单表代换的一个简单改进,也就是把原来的代换表,由单个字符到单个字符的映射,变成了多个字符到多个字符的映射。比如,原来在单表代换中,明文字符a用b代换,字符b用d代换,故ab,加密成bd;而多字符代换则是一次加密多个明文字符,比如ab,直接用另一个字符对ac代替,形成密文ac。
多字符代换密码的典型例子是Playfair密码。
本质上来说,多字符代换并没有引入新的东西,只是把原来代换表变大了,也就是扩充了明文字符集。不妨设一个明文字符集为{a,b,c},那么其对应的双字符代换就是把明文字符集扩充为{aa,ab,ac,bb,ba,bc,cc,ca,cb}。但是,由于明文字符集增大了,其代换表的可能个数就增大了,增加了破译密码的难度。
但是,我们在设计多字符代换的时候,需要考虑一个填充的问题。比如双字符代换密码,那么,一次加密必须要加密两个字符。也就是说,明文字符的个数必须是偶数。比如要加密bad,我们需要在加一个字符填充成4个字符长度的明文,再加密,如再bad后填充x,即变成了badx,这样就可以加密了。相应的,在解密的时候要把填充的字符去掉。
一种可能的填充方式是,填充一般不会在明文中出现的字符,这样,可以便于去除填充的字符。比如在26个英文字符的明文字符集中,填充数字,并设定相应的代换规则,在解密的时候遇到了数字,便知道是填充的,可以丢弃。

二、一个简单的双字符代换密码

考虑明文字符集为26个小写英文字母,代换的长度为2,也就是双字符代换。填充的字符为z(假定z在正常明文中不会出现)。

#include<iostream>
#include<vector>
#include<stdlib.h>
#include<time.h>
using namespace std;
//generate the random table,table[0] means pair characters aa,
//table[1] means ab, table[2] means ac and so on.
vector<int> genTable(){
	srand((unsigned)time(NULL));
	vector<bool> flag(676);
	vector<int> table(676);
	bool finish=false;
	int temp;
	int i=0;
	while(i<676){
		temp=rand()%676;
		if(flag[temp]){
			continue;
		}
		table[i++]=temp;
		flag[temp]=true;
	}
	return table;
}
//encrypt function, 
string encrypt(string message, vector<int> &table){
	int f1,s1,f2,s2;
	if(message.size()%2==1){
		message.push_back('z');
	}
	for(int i=0;i<message.size();i+=2){
		f1=message[i]-'a';
		s1=message[i+1]-'a';
		f2=table[f1*26+s1]/26;
		s2=table[f1*26+s1]%26;
		message[i]=f2+'a';
		message[i+1]=s2+'a';
	}
	return message;
}
//decipher
string decipher(string cipher, vector<int> &table){
	//generator r_table
	vector<int> r_table(676);
	for(int i=0;i<table.size();i++){
		r_table[table[i]]=i;
	}
	int f1,s1,f2,s2;
	for(int i=0;i<cipher.size();i+=2){
		f1=cipher[i]-'a';
		s1=cipher[i+1]-'a';
		f2=r_table[f1*26+s1]/26;
		s2=r_table[f1*26+s1]%26;
		cipher[i]=f2+'a';
		cipher[i+1]=s2+'a';
	}
	if(cipher.back()=='z'){
		cipher.pop_back();
	} 
	return cipher;
}
int main(){
	vector<int> table;
	table=genTable();
	string message,cipher;
	cout<<"Please input message:";
	cin>>message;
	cipher=encrypt(message,table);
	cout<<"The ciphertext is:"<<cipher<<endl;
	cout<<"After deciphering:"<<decipher(cipher,table)<<endl;
}

比如下面对hello加密,可以看到相同字母l在密文中不再相同,对明文的信息有一定的掩盖作用。

Please input message:hello
The ciphertext is:yifuqe
After deciphering:hello

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
多表代换密码是一种基于替换的密码算法,它将明文字符替换为密文字符,从而实现加密。在多表代换密码中,通常采用多个替换表来进行加密,以增加密码的复杂度和安全性。 下面是一个简单的用 C 语言实现多表代换密码的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_TABLES 26 #define TABLE_SIZE 26 // 生成随机的替换表 void generate_tables(char tables[MAX_TABLES][TABLE_SIZE+1]) { int i, j; for (i = 0; i < MAX_TABLES; i++) { char c = 'a' + i; // 生成替换表的第一个字符 tables[i][0] = c; for (j = 1; j < TABLE_SIZE; j++) { tables[i][j] = c + j; if (tables[i][j] > 'z') { tables[i][j] -= 26; } } tables[i][TABLE_SIZE] = '\0'; } } // 加密函数 char* encrypt(const char* plain, const char tables[MAX_TABLES][TABLE_SIZE+1]) { int len = strlen(plain); char* cipher = (char*) malloc(len+1); int i, j; for (i = 0; i < len; i++) { char c = plain[i]; if (c >= 'a' && c <= 'z') { int table = c - 'a'; // 根据明文字符选择替换表 j = rand() % TABLE_SIZE; // 随机选择替换字符 cipher[i] = tables[table][j]; } else { cipher[i] = c; } } cipher[len] = '\0'; return cipher; } // 解密函数 char* decrypt(const char* cipher, const char tables[MAX_TABLES][TABLE_SIZE+1]) { int len = strlen(cipher); char* plain = (char*) malloc(len+1); int i, j; for (i = 0; i < len; i++) { char c = cipher[i]; if (c >= 'a' && c <= 'z') { int table = c - 'a'; // 根据密文字符选择替换表 j = strcspn(tables[table], &cipher[i]); // 查找替换字符在替换表中的位置 plain[i] = 'a' + j; // 明文字符为替换字符在替换表中的位置加上 'a' } else { plain[i] = c; } } plain[len] = '\0'; return plain; } int main() { char tables[MAX_TABLES][TABLE_SIZE+1]; generate_tables(tables); const char* plain = "hello world"; printf("Plain text: %s\n", plain); char* cipher = encrypt(plain, tables); printf("Cipher text: %s\n", cipher); char* decrypted = decrypt(cipher, tables); printf("Decrypted text: %s\n", decrypted); free(cipher); free(decrypted); return 0; } ``` 在这个示例中,我们首先生成了 26 个大小为 26 的随机替换表,然后使用 `encrypt()` 函数对明文进行加密,使用 `decrypt()` 函数对密文进行解密。在加密过程中,对于明文中的每个小写字母,我们根据它在字母表中的位置选择一个替换表,然后随机选择一个替换字符进行替换。在解密过程中,对于密文中的每个小写字母,我们根据它所代表的替换表和替换字符进行逆替换,得到明文字符。 需要注意的是,这个示例中的多表代换密码算法并不是非常安全,因为替换表是固定的,而且对于明文中的每个小写字母都使用了不同的替换表,这使得攻击者可以通过分析密文中的字母频率来破译密码。实际应用中,应该使用更加复杂的算法来生成替换表,以及使用更加灵活的方法来选择替换字符和替换表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值