采用替代密码算法中的维吉尼亚密码方法,密文C=“HEADVIGENERE”,密钥K=KEY,求明文P
将密文HEADVIGENERE用密钥替换后为KEYKEYKEYKEY
替换前:HEADVIGENERE
替换后:KEYKEYKEYKEY
解密求明文:
按替换后的内容找到第一行的K所在位置向下寻找,找到H的位置,当前行最左侧第一列对应的就为明文X
加密求密文:
按明文找到第一列对应的H,在从第一行中找到对应的密钥K,两个位置相交的值就为密文.
答案:
HEA DVI GEN ERE
KEY KEY KEY KEY
XAC TRK WAP UNG
维吉尼亚密码引入了“密钥”的概念,即根据密钥来决定用哪一行的密表来进行替换,以此来对抗字频统计。假如以上面第一行代表明文字母,左面第一列代表密钥字母,对如下明文加密:
TO BE OR NOT TO BE THAT IS THE QUESTION
当选定RELATIONS作为密钥时,加密过程是:明文一个字母为T,第一个密钥字母为R,因此可以找到在R行中代替T的为K,依此类推,得出对应关系如下:
密钥:RE LA TI ONS RE LA TION SR ELA TIONSREL
明文:TO BE OR NOT TO BE THAT IS THE QUESTION
密文:KS ME HZ BBL KS ME MPOG AJ XSE JCSFLZSY
与凯撒密码类似,进行一下运算两次即可
给,网上的C++的基本都有问题,我给你改好一个,已经编译运行确认,
#includeiostream
using namespace std;
#define MINCHAR 32
#define CHARSUM 94
char table[CHARSUM][CHARSUM];
bool Init();
bool Encode(char* key, char* source, char* dest);
bool Dncode(char* key, char* source, char* dest);
int main()
{
if(!Init())
{
cout "初始化错误!" endl;
return 1;
}
char key[256];
char str1[256];
char str2[256];
int operation;
while(1)
{
do
{
cout "请选择一个操作:1. 加密; 2. 解密; -1. 退出\n";
cin operation;
}while(operation != -1 operation != 1 operation != 2);
if(operation == -1)
return 0;
else if(operation == 1)//加密
{
cout "请输入密钥:";
cin key;
cout "请输入待加密字符串:";
cin str1;
Encode(key, str1, str2);
cout "加密后的字符串:" str2 endl;
}
else if(operation == 2)//解密
{
cout "请输入密钥:";
cin key;
cout "请输入待解密字符串:";
cin str1;
Dncode(key, str1, str2);
cout "解密后的字符串:" str2 endl;
}
cout endl;
}
return 0;
}
// 初始化维吉尼亚方阵
bool Init()
{
int i, j;
for(i = 0; i CHARSUM; i++)
{
for(j = 0; j CHARSUM; j++)
{
table[i][j] = MINCHAR + (i + j) % CHARSUM;
}
}
return true;
}
// 加密
// key:密钥
// source:待加密的字符串
// dest:经过加密后的字符串
bool Encode(char* key, char* source, char* dest)
{
char* tempSource = source;
char* tempKey = key;
char* tempDest = dest;
do
{
*tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR];
tempDest++;
if(!(*(++tempKey)))
tempKey = key;
}while(*tempSource++);
dest[strlen(source)] = 0;
return true;
}
// 解密
// key:密钥
// source:待解密的字符串
// dest:经过解密后的字符串
bool Dncode(char* key, char* source, char* dest)
{
char* tempSource = source;
char* tempKey = key;
char* tempDest = dest;
char offset;
do
{
offset = (*tempSource) - (*tempKey);
offset = offset = 0 ? offset : offset + CHARSUM;
*tempDest = MINCHAR + offset;
tempDest++;
if(!(*(++tempKey)))
tempKey = key;
}while(*++tempSource);
dest[strlen(source)] = 0;
return true;
}