维吉尼亚密码的实现
《应用密码学》课程实验
报告
软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载
--------------- 维吉尼亚密码的实现 一、实验目的
维吉尼亚密码是古典密码中非常具有代表的例子。本实验要求用高级语言编写和调试一个简单的维吉尼亚密码实现及
分析
定性数据统计分析pdf销售业绩分析模板建筑结构震害分析销售进度分析表京东商城竞争战略分析
破译程序。通过本实验可以加深理解维吉尼亚密码及其分析破译。 二、实验内容与要求
(1)
设计
领导形象设计圆作业设计ao工艺污水处理厂设计附属工程施工组织设计清扫机器人结构设计
一个函数实现维吉尼亚密码;
(2)在已知明文及其对应密文的基础上,分析破译维吉尼亚密码,编程求解密钥;
(3)要求有程序实现,有实验结果截图,有测试例子。 (4)编程语言不限制,可以用tc2.0,vc6.0,.net或java 三、实验设备
硬件环境: Intel Pentium Processor 1.8G ,512M内存 ,windows
操作系统
软件环境:VC++
四、实验步骤
#include
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;
}
五、实验结果与分析
密钥:AF
待加密字符串:WCTHF