`

二维易位——通过密钥对明文进行加密或对密文进行解密

阅读更多
  这其实是湖大一个同学的作业,前几天我这个隔壁学校的被求助,然后翘了一节课帮她写出来的,若不是妹纸我才没那么积极呢[偷笑]。 所以,湖大的同学若果想要用这份代码交作业请慎重,以免老师追究。
现已知一份密文: hdqtrnyoenahauiniorpaiduaubidofdaletcndnnoertenteavcfhacukeatpv.
fcyynatyneucusanedthrepetoctnhukrlhnadiywieosrsifooueeoitaanevui
osjtosvtmfnflfnunnamentntemfoehmtiouttrgpnyrihaaceerleihvohhhasrc,
svtnttnszycteetieelhoifiehde9tshtpturolrhlaveatrei'8aoahcensfliiy,
inmysmss5nroeheiitapsrcnto.iinpfdlypuavthnpteacmuitnorociuanneyedl
onpeistiiworoueradr,poiroi,lciesijpnkytntssinniwtilesstnerseuyuhii
ctgcnasenoarccitlasnettthxaelhntnfyltorireipyuueildcoaeefou2ruaecvr
.arwalcinrnoifd1icnrreeateenyitoinflee1ttdeersbessgatyraitiddayips
tsic
  已知它是使用二维易位方法加密 但现在密钥未知(不超过25),明文中的空格在加密时去除,所有字符全小写 加密时若最后一行不全,则用a到z按顺序填充, 解密后把填充在末尾的a-z去掉得到明文。
  例如: 明文为:today is friday
     去掉空格后:todayisfriday
  密钥为3:
today
isfri
dayab
在明文后填充了ab
密文为:tidosadfyarayib
解密后得:todayisfridayab [img]

手动断句后去掉填充的ab,可得明文。
  所以,对明文进行加密的思路是:
1、将明文中的空格去掉;
2、将明文的字符数根据密钥(key)进行补齐(补以abc....);
3、将明文按照先行后列的顺序把字符放入二维数组矩阵中;
4、将二维数组中的字符按照先列后行的顺序取出并串成密文。
以下是几个关键方法的代码实现
/**
	 * 加密算法
	 * @param key 密钥
	 * @param foremsg 明文
	 * @return
	 */
	public String encrypt(int key,String foremsg) {
		  int len = foremsg.length() / key;
		  int low = key;//行
		  int col = len; //列
		  String newString = "";
		  array = new String[low][col];//数组矩阵
		  
		  System.out.println("正序数组:");
		  for (int i = 0; i < low; i++) {//循环依次放入数组中 先行后列
		   for (int j = 0; j < col; j++) {
			   if(count<foremsg.length()){//避免数组越界
				   array[i][j] = foremsg.substring(count,count+1);//返回指定索引处的 char 值
				   System.out.print(array[i][j]+"  ");
			   }
			   count ++;
		   }
		  }
		  System.out.println();
		 
		  System.out.println("逆序数组:");
		  for (int j = 0; j < col; j++) {//循环输出(按矩阵反序)PPT第6页 先列后行
			   for (int i = 0; i < low; i++) {
				   newString = array[i][j];
				   jiami_msg += newString;//连接成串
				   System.out.print(newString+"  ");
			   }
			  }
		  System.out.println();
		  return jiami_msg;
		 }
	 
	/**
	 * 删除空格
	 */
	 private void deteleSpace() {
		  String stringNoSpace = "";
		  while (foremsg.indexOf(" ") != -1) {
		   stringNoSpace += foremsg.substring(0, foremsg.indexOf(" "));//存空格前
		   foremsg = foremsg.substring(foremsg.indexOf(" ") + 1);//存空格后
		  }
		  stringNoSpace += foremsg;//连接
		  aftermsg= stringNoSpace;//赋值
		 }
	 
	 /**
	  * 整理字符串
	  * @return 整理之后的字符串
	  */
	private String addDivide() {// 后面补充ab....
		  int len = aftermsg.length();
		  String str1 = aftermsg;
		  if (len % key != 0) {
		   for (int i = 0; i < key - len % key; i++) {
		    str1 += String.valueOf((char) (97 + i));//将char变量转换为字符串 i = 0 时为a
		   }
		   foremsg = str1;//此时的foremsg为去空格且补齐的串
		  }
		  return foremsg;
		 }

有了加密算法,解密的过程就显得容易多了,无非是将加密过程倒过来。
1、将密文的字符按照先列后行的顺序放入二维数组;
2、将二维数组中的字符按照先行后列的顺序取出数组并串成明文。
由于计算机只负责计算,所以需要手动断句并将补齐的字符去掉。
解密代码如下:
/**
	 * 解密的方法
	 * @param key 密钥
	 * @param minwen_msg 密文
	 * @return mingwen_msg 明文
	 */
	public String decrypt(int key,String miwen_msg) {
		
		  int len = miwen_msg.length() / key;
		  
		  int low = key;
		  int col = len; 
		  String newString = "";
		  array = new String[low][col];//数组矩阵
		  
		  System.out.println("密文正序数组:");
		  for (int j = 0; j < col; j++) {//循环依次放入数组中 先列后行
		   for (int i = 0; i < low; i++) {
			   if(count <miwen_msg.length()){//避免数组越界
				   array[i][j] = miwen_msg.substring(count,count+1);//返回指定索引处的 char 值
				   System.out.print(array[i][j]+"  ");
			   }
			   count ++;
		   }
		  }
		  System.out.println();
		 
		  System.out.println("密文解密后数组:");
		  for (int i = 0; i < low; i++) {//循环输出(按矩阵正序)PPT第6页 先行后列
			   for (int j = 0; j < col; j++) {
				   newString = array[i][j];
				   mingwen_msg += newString;
				   System.out.print(newString+"  ");
			   }
			  }
		  System.out.println();
		  return mingwen_msg;
		 }
	


解密之后并断句可得明文:
/**
* 密钥为23,明文如下。
* hunan university,situated at the foot of the picturesque yuelu hilland on the west bank of the
* rippling xiang river in the historically and culturally renowned city of changsha,capital city of hunan province,
* is honored as an ancient millenarian academy,famous centennial university.it is one of the key universities affiliated
* with the ministry of education and is now included in the state's project 211 and project 985 for priority investment and construction.
* professor liu keli and professor zhao yueyu are respectively the party secretary of the cpchunan university committee and the president
* of the university.
*/


其实有了加解密算法,这个程序可以通过IO实现对文件的加密解密,再用SWing界面加以可视化操作,简直上了一个档次,无奈近来事儿多,暂且搁置吧。
附件中有程序源代码。
  • 大小: 10.8 KB
1
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics