西安理工大学实验报告
西安理工大学实验报告用纸 第 页(共 页)
西安理工大学实验报告
课 程: 信息安全
班 级: 计升本101
学 号: 2100912011 指导老师: 吕林涛、王勇超 姓 名: 张亚妮 报告退发: 日 期: 2011-12-09 至 2011-12-16 教师审批签字: 一、实验题目:置换密码技术实现程序开发
二、实验目的
通过置换密码算法的实现,进一步掌握置换密码算法的原理,为今后的工程应用打下坚实的基础。
三、实验原理
置换密码亦称换位密码。置换只不过是一个简单的换位。每个置换都可以用一个置换矩阵Ek来表示。每个置换都有一个与之对应的逆置换Dk。置换密码的特点是仅有一个发送方和接收方知道的加密置换(用于加密)和对应的逆置换(用于解密)。它是对明文L长字母组中的字母位置进行重新排列,而每个字母本身并不改变。
令明文为 mm1m2mL。令置换矩阵所决定的置换为,则
加密置换 CEk(c1c2cL)m(1)m(2)m(L)
解密置换 dDk(c1(1)1(2)cc1(L))
四、实验环境
运行windows操作系统的PC机,具有VC、C、VB等语言编译环境。
五、实验内容
(1)预习准备:实验目的、实验原理;
(2) 建立置换矩阵模型和明文分段;
(3)分别设计置换密码技术加密和解密程序;
(4)设计实验过程和环境,例如可以通过电子邮件相互传送;
(5)设计实验用的测试用例(加密文件);
(6)实验结果分析:分析影响置换密码技术加密体制安全性的因素,其他发现和想到的问题。
六、源代码
package Practice1;
import java.util.Arrays;
import java.util.Scanner;
public class ReplaceDemo {
private String sourceString =
private String keyString =
private int[] secretMatrix;
private char groupedSource[][];
private void getSource(){//Get the source String
Scanner scan = new Scanner(System.in);
System.out.print(
System.out.print(
keyString = scan.next();
}
public ReplaceDemo(){//constructor for this class
getSource();
}
private void groupSourceString(){//make groups for sourceString according by the keyString
int sourceLen = sourceString.length();
int keyLen = keyString.length();
groupedSource = new char[sourceLen/keyLen+1][keyLen];
if (sourceLen%keyLen!=0){
for (int i = 0;i
sourceString +=
}
}
sourceLen = sourceString.length();
for(int i = 6,j=0;i
groupedSource[j] = sourceString.substring(i-keyLen, i).toCharArray(); }
}
private void getTheMatrix(){//get the Matrix for the encrypt char[] temp01 = keyString.toCharArray();
secretMatrix = new int[keyString.length()];
Arrays.sort(temp01);
for (int i = 0;i
for (int j = 0;j
if (keyString.toCharArray()[i]==temp01[j]){
secretMatrix[i] = j;
}
}
}
}
private void changeSourceMatix(){
groupSourceString();
char temp[][] = new char[groupedSource.length][keyString.length()]; for (int i = 0;i
for (int j = 0;j
temp[i][j] = groupedSource[i][j];
}
}//请教Java老师
for (int i = 0;i
for (int j = 0;j
groupedSource[i][j] = temp[i][secretMatrix[j]];
}
}
}
private void getEncryptedString(){//get the matrix which will be printed. groupSourceString();
changeSourceMatix();
char temp[][] = new char[groupedSource.length][keyString.length()]; for (int i = 0;i
for (int j = 0;j
temp[i][j] = groupedSource[i][j];
}
}
for (int i = 0;i
for (int j = 0;j
groupedSource[i][j] = temp[i][secretMatrix[j]];
}
}
}
public void print1(){//print the code have Encrypted
getEncryptedString();
for(int i = 0;i
for(int j = 0;j
System.out.print(groupedSource[j][i]);
}
}
System.out.println();
}
//decryption process
private void groupEncryptedString(){
int sourceLen = sourceString.length();
int keyLen = keyString.length();
char[][] temp = new char[keyLen][sourceLen/keyLen+1];
for (int i = 0;i
temp[i] = sourceString.substring(i*sourceLen/keyLen,
(i+1)*sourceLen/keyLen).toCharArray();
}
groupedSource = new char[sourceLen/keyLen+1][keyLen];
for(int j = 0; j
for (int i = 0;i
groupedSource[j][i] = temp[i][j];
}
}
}
private void getDecryptString(){//get the matrix which will be printed. groupEncryptedString();
char temp[][] = new char[groupedSource.length][keyString.length()]; for (int i = 0;i
for (int j = 0;j
temp[i][j] = groupedSource[i][j];
}
for (int i = 0;i
for (int j = 0;j
groupedSource[i][j] = temp[i][secretMatrix[secretMatrix[j]]]; }
}
}
public void print2(){//print the code have Encrypted
getDecryptString();
for(int j = 0;j
for(int i = 0;i
System.out.print(groupedSource[j][i]);
}
}
System.out.println();
}
public static void main(String args[]){
ReplaceDemo RD = null;
Scanner scanin = null;
System.out.println(
operate:\t(select “0” for encrypte and select “1” for decrypt.)
int selected = scanin.nextInt();
while(selected==0 || selected==1){
RD = new ReplaceDemo();
RD.getTheMatrix();
if (selected == 0){
RD.print1();
}
else{
RD.print2();
}
System.out.println(
operate:\t(select “0” for encrypte and select “1” for decrypt.)
selected = scanin.nextInt();
System.out.println(
}
}
七、实验结果
例如,明文为attack begins at five,密钥为cipher,将明文按照每行6个字母的形式排在矩阵中,形成如下形式:
根据密钥cipher中各个字母在字母表中出现的先后顺序,给定一个置换:
由密钥cipher通过getTheMatrix方法得到f矩阵。
根据上面的置换,将原有居住中的字母按照第1列、第4裂、第5裂、第3裂、第2列、第6列的顺序排列,则有下面的形式:
这里经过了一点处理就是当输入的字母数不是密钥长度的整数倍时,在输入的备加密字符串加上几个“#”来完成补全,然后参加到加密的过程中去,从而得到如下的密文:
a siteaetb vci #agt#knf#
其解密过程是根据密钥的字母数作为列数,将密文按照列、行的顺序写出,再根据由 密钥给出的矩阵置换产生新的矩阵,从而恢复明文。
本实验中也使用了上述的测试用例,主要经过两次依据矩阵的变换,都是使用矩阵f中的下面一行中的数值所在列来替换上面一行数值表示的列的值,从而来改变起序列、完成加密。
解密中用到groupedSource[i][j]=temp[i][secretMatrix[secretMatrix[j]]]; 来完成将原来经过矩阵转变来的字符序列逆转,从而得到起加密前的矩阵。
八、 实验总结
通过对本次实验的练习,使我们大家都对置换密码技术实现程序开发有了一定的了解。矩阵换位法是实现置换密码的一种常用方法。它将明文中的字母按照给定的顺序安排在一个矩阵中,然后用根据密钥提供的顺序重新组合矩阵中的字母,从而形成密文。
首先,置换密码技术的密钥是这样产生的:
密钥生成首先要使用概率算法来验证随机产生的大的整数是否质数,这样的...此外寻找质数的算法不能给攻击者任何信息,每个密码都有不同的用于密钥的格式,需要确保密钥的生成是随机的,需要遵循下面在步骤:
1>、为加密算法获取KeyGenerator;
2>、用随机源来初始化密钥发生器,如果密码块的长度是可变的,还需要指定期望的密码块长度。
3>、调用generateKey方法。
其次,置换密码算法的实现过程如下:
再次,置换密码算法的优缺点如下:
优点:加密效率高,硬件实现可达每秒数百兆字节(软件实现略慢一些)密钥相对比较短,可以用来构造各种密码机制,可以用来建造安全性更强的密码; 缺点:通信双方都要保持密钥的秘密性,,在大型网络中,每个人需持有许多密钥,为了安全,需要经常更换密钥。
九、参考文献
[1]吕林涛等.网络信息安全技术概论(第二版).北京:科学出版社,2010.6
[2]梁亚声等.计算机网络安全技术教程.北京:机械工业出版社,2005.2
[3]周明全,吕林涛,李军怀.网络网络与信息安全技术(第二版).西安:西安电子科技大学出版社,2010.7
[4] 吕林涛等.《信息安全》实验指导书.校编,2011