⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encrypt.java

📁 图书馆管理系统 能满足 查询
💻 JAVA
字号:
package com.jxyd.common;

import java.text.SimpleDateFormat;
import java.util.*;
import java.security.Key;
import javax.crypto.Cipher;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.KeyGenerator;
import java.security.SecureRandom;
//这个类用于加密解密用户密码

public class Encrypt
 {
	
	public static String getDate(){
		
		//获取日,小时,分钟字符串
	//	java.util.Calendar ca=java.util.Calendar.getInstance();
   //     java.util.Date da=ca.getTime();
  //      String dates=da.toString();
 //       String d=ca.get(1)+"-"+(ca.get(2)+1)+"-"+dates.substring(8, 10)+" "+dates.substring(11, 13)+":"+dates.substring(14, 16);
//		String datekey=d.substring(7,9)+d.substring(10,12)+d.substring(13,15);

	//	System.out.println(datekey+"     d");
	//	return datekey;
		//固定的密码
		return "221801";
	}
	
	static Key  key=Encrypt.getKey(Encrypt.getDate());
	 public static Key getKey(String strKey) {
		 Key keys=null;
		    try {
		      KeyGenerator _generator = KeyGenerator.getInstance("DES");
		      _generator.init(new SecureRandom(strKey.getBytes()));
		      keys = _generator.generateKey();
		      _generator = null;
		    }
		    catch (Exception e) {
		      e.printStackTrace();
		    }
		    return keys;
		  }
	//这个方法用于获取当前时间
	public static String getNowString() {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        GregorianCalendar now = new GregorianCalendar();
        Date date = now.getTime();
        return f.format(date);
	}
	//这个方法用于将传入的字符串加密
	public static String stringToEnc(String strMing) {
	    byte[] byteMi = null;
	    byte[] byteMing = null;
	    String strMi = "";
	    BASE64Encoder base64en = new BASE64Encoder();
	    try {
	      byteMing = strMing.getBytes("UTF8");
	      byteMi = Encrypt.encToByte(byteMing);
	      strMi = base64en.encode(byteMi);
	    }
	    catch (Exception e) {
	      e.printStackTrace();
	    }
	    finally {
	      base64en = null;
	      byteMing = null;
	      byteMi = null;
	    }
	    return strMi;
	  }
	
	//这个方法用于将加密后的字符串解密
	 public static String encToString(String strMi) {
		    BASE64Decoder base64De = new BASE64Decoder();
		    byte[] byteMing = null;
		    byte[] byteMi = null;
		    String strMing = "";
		    try {
		      byteMi = base64De.decodeBuffer(strMi);
		      byteMing = Encrypt.byteToEnc(byteMi);
		      strMing = new String(byteMing, "UTF8");
		    }
		    catch (Exception e) {
		      e.printStackTrace();
		    }
		    finally {
		      base64De = null;
		      byteMing = null;
		      byteMi = null;
		    }
		    return strMing;
		  }
	 //设置加密类型为DES
	 public static byte[] encToByte(byte[] byteS) {
		    byte[] byteFina = null;
		    Cipher cipher;

		    try {

		      cipher = Cipher.getInstance("DES");
		      cipher.init(Cipher.ENCRYPT_MODE, key);
		      byteFina = cipher.doFinal(byteS);
		    }
		    catch (Exception e) {
		      e.printStackTrace();
		    }
		    finally {
		      cipher = null;
		    }
		    return byteFina;
		  }

		  public static byte[] byteToEnc(byte[] byteD) {
		    Cipher cipher;
		    byte[] byteFina = null;
		    try {
		      cipher = Cipher.getInstance("DES");
		      cipher.init(Cipher.DECRYPT_MODE, key);
		      byteFina = cipher.doFinal(byteD);
		    }
		    catch (Exception e) {
		      e.printStackTrace();
		    }
		    finally {
		      cipher = null;
		    }
		    return byteFina;

		  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -