现代密码学实验题目代码
实 验 报 告
实验课程名称 现代密码学
学 院 理学院 年级 大三 专业班 信科0802
学 生 姓 名 王尔林 学 号 200810010210
开 课 时 间 2010 至 2011 学年第 二 学期
总 成 绩
教师签名
实验项目 成绩
实验一、古典密码 (认识密码学)
名 称
一、实验目的
通过实现简单的古典密码算法,理解密码学的相关概念如明文 (plaintext)、密文
(ciphertext)、加密密钥(encryption key)、解密密钥 (decryptionkey)、加密算法(encryption
algorithm)、解密算法(decryption algorithm)等。
二、实验内容
1)用C\C++语言实现仿射变换 (Affine)加/解密算法;2 )用C\C++语言实现统计26
个英文字母出现的频率的程序;3 )利用仿射变换加/解密程序对一段较长的英文文章进行
加密,再利用统计软件对明文和密文中字母出现的频率进行统计并作对比,观察有什么规
律。
放射变换:
加密:c E a ,b m am b mod26
1
解密:m Da ,b c a c b mod26
其中a,b 为密钥,0 a,b 25,且gcd(a,26) 1
实验要求:加/解密程序对任意满足条件的a、b 都能够处理。
三、实验步骤
(1)统计26个英文字母出现的频率的程序
#include
#include
#include
usingnamespace std;
voidmain(){
ifstream in("a.txt");
vector s;
vectorn(26,0);
for(int i 0;i<26;++i)
s.push_back(97+i);
for(charx;in>>x;)
for(int i 0;i<26;++i)
if(int(x) s[i]){
n[i]++;}
float sum 0.0;
for(intj 0;j<26;++j)
sum+ n[j];
cout<
for(intk 0;k<26;++k){
//n[k] n[k]/sum;
cout<
//cout<
}
(2)仿射变换加/解密程序对一段较长的英文文章进行加密
#include
#include
#include
usingnamespace std;
//判断两个数是不是互素 (辗转相除)
boolgcd(int a){
int f 26,g,r;
g a;
do{
r f%g;
f g;
g r;
}while(r);
if(f 1)
return 1;
else
return 0;
}
//求逆//
int inv(int a){
intx,i;
for(i 1;i< 30;++