#include<iostream>
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 = 0i <CHARSUMi++)
{
for(j = 0j <CHARSUMj++)
{
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
}
你所说的“这个维吉尼亚密码”指的是档世局哪个维吉尼亚密码?好吧,我知道了,是百度百科的(答你这题还得有点联想能力……)
“TO BE OR NOT TO BE THAT IS THE QUESTION
当选定RELATIONS作为密钥时,加密过程是:行让返旦明文一个字母为T,第一个密钥字母为R”
额,上面不是说了吗用"RELATIONS"作为密钥那么明文第一个字母T对应的密钥就是R。你要想问"RELATIONS"这个密钥砸来的?我告诉你,想来的,这个密钥你想用啥单词就用啥单词,越生僻的单词越好,选完密钥后加密,但密钥长度不够,那么就重复使用,如下:
密钥:RELAT IONSR ELATI ONSRE LATIO NSREL
明文:TOBEO RNOTT OBETH ATIST HEQUE STION
密文:KSMEH ZBBLK SMEMP OGAJX SEJCS FLZSY
给你个维吉尼亚密码加密解密用的程序,使用后就能更好理解:
http://hi.baidu.com/%B7%E7%F3%DD%B5%C4%CC%EC%BF%D5%C0%B6%D2%AB/blog/item/0f65c019326c000d35fa41f0.html
(别告诉我这是个只上一次的小号……我的采纳率被这种号害惨了……)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)