Java实验一(下):信息关键内容加密

实验目的

了解及掌握正则表达式及简单的字符加密解密。

实验内容

1、有一段信息,“本次见面时间为2021年5月7日,中午12点30分,地点位于卫星路7186号,车牌号是吉AB27D,如果临时有事,请及时与我联系,电话是18812342234”
2、里面包括一些特定的日期、时间等,请对里面的包含数字的信息进行提取,并加密和解密
3、所用的技术包括正则表达式和字符加密解密

实验代码

package experiment_one;
import java.util.Scanner;

class jiajiemi{
	String jiami(String sourceString,String password){
		char[] p = password.toCharArray();
		int n = p.length;
		char[] c = sourceString.toCharArray();
		int m = c.length;
		for(int k=0;k<m;k++){
			int mima = c[k]+p[k%n];
			c[k] = (char)mima;
		}
		return new String(c);
	}
	String jiemi(String sourceString,String password){
		char[] p = password.toCharArray();
		int n = p.length;
		char[] c = sourceString.toCharArray();
		int m = c.length;
		for(int k=0;k<m;k++){
			int mima = c[k]-p[k%n];
			c[k] = (char)mima;
		}
		return new String(c);
	}
}

public class project_one {
	public static void main(String[] args){
		String sourceString = "本次见面时间为2021年5月7日,"
				+ "中午12点30分,"
				+ "地点位于卫星路7186号,"
				+ "车牌号是吉AB27D,"
				+ "如果临时有事请与我联系,"
				+ "电话是18812342234";
		jiajiemi test = new jiajiemi();
		String digitword[] = sourceString.split("\\D+");
		String sourceString1 = "";
		for(int i=0;i<digitword.length;i++){
			sourceString1 = sourceString1+digitword[i]+" ";
		}
		String word[] = sourceString.split("\\d+");

		System.out.println("原内容为:"+sourceString);
		System.out.println("加密内容为:"+sourceString1);
		System.out.print("输入加密密码:");
		Scanner input = new Scanner(System.in);
		String password = input.nextLine();
		String miwen = test.jiami(sourceString1, password);
		System.out.println("密文:" + miwen);
		System.out.print("输入解密密码:");
		password = input.nextLine();
		String mingwen = test.jiemi(miwen, password);
		String mingwen1[] = mingwen.split("\\D+");
		String mingwen2 = "";
		for(int i=1;i<=word.length;i++){
			mingwen2 = mingwen2+word[i-1]+String.valueOf(mingwen1[i]);
		}
		System.out.println("明文:"+mingwen2);
	}
}

 

1.腾龙梦屋文章内容无特殊注明皆为源儿原创,转载请注明来源,谢谢!
2.若有相关文章侵犯您的权益,请联系源儿删除,谢谢!
3.相关软件、资料仅供学习参考使用,在24h内务必删除!
腾龙梦屋 » Java实验一(下):信息关键内容加密
加速支持