實現原理:通過判斷用戶的登陸次數以及設置到期時間與系統現有時間對比,兩者之中有一為非法則提示軟件到期。 其中字符串對比通過MD5。 我想一開始把txt設置為隱藏模式,而試用期一過就刪除txt文件,應該做到比較好的效果,歡迎討論! msn:[email protected] QQ:17664554 package Admin.Control; import java.io.*; import java.security.MessageDigest; import Admin.Control.MD5; import Java.util.*; /** *
Title: *****系統
*
Description: ****系統
*
Copyright: Copyright (c) 2004
*
Company: 北京大學軟件學院
* @author 雲廣 * @version 1.0 */ public class ControlValid { private final String REGISTERNUMBER; //加密的原始字符串 private String md5Number; //MD5加密後的字符串 private String writeContent; //需要寫入文本的內容 private int loginTimesLimit; //登陸次數限制 private static int loginTimesTrue; //用戶的實際登陸次數 private MD5 md5Object; //實例化MD5對象 private Calendar cal; //日期時間類 private int orderTime; //設定的系統到期時間 //構造方法進行初始化賦值 public ControlValid() { REGISTERNUMBER = "你想輸入的正確驗證字符串"; md5Object = new MD5(); loginTimesLimit = 99; loginTimesTrue = 0; writeContent = null; cal = Calendar.getInstance(); orderTime = 0; } //返回經過MD5加密過的字符串 public String returnMd5String() { try { //得到經過MD5加密過的字符串 md5Number = md5Object.MD5(REGISTERNUMBER); return md5Number; } catch (Exception e) { return null; } } //將字符串寫進指定文本 public void writeFile() { try { String returnFileString; int returnLastLoginTimes = 0; //Calendar cal = Calendar.getInstance(); //測試獲得系統時間 int currentDate = cal.get(cal.YEAR) + (cal.get(cal.MONTH) + 1) + cal.get(cal.DAY_OF_MONTH); //System.out.println(currentDate); //需要寫入文本文件的字符串 String returnInitDate = this.readFile("InitDate.txt"); orderTime = Integer.parseInt(returnInitDate); returnFileString = this.readFile("ControlValid.txt"); //獲得換行的位置 int pos = returnFileString.indexOf("\n"); if (pos != -1) { returnLastLoginTimes = Integer.parseInt(returnFileString.substring(pos+1, pos+3)); } loginTimesTrue = returnLastLoginTimes; this.setLoginTimesTrue(); //根據日期和用戶登陸次數判斷寫入的字符串 if ( (currentDate < orderTime) && (this.getLoginTimesTrue() < loginTimesLimit)) { //writeContent = this.returnMd5String() + "\n" + this.getLoginTimesTrue(); writeContent = this.returnMd5String() + "\r" + this.getLoginTimesTrue(); } else { //隨機產生一數字進行輸入 Random ranInt = new Random(); //int randomInt = ; writeContent = String.valueOf(ranInt.nextInt(10000000))+ "\r" + this.getLoginTimesTrue(); } //將加密過的字符串寫入文本文件 File f = new File("ControlValid.txt"); if (!f.exists()) { f.createNewFile(); } DataOutputStream outFile = new DataOutputStream( new BufferedOutputStream( new FileOutputStream(f))); byte[] byteWriteContent = writeContent.getBytes(); outFile.write(byteWriteContent); outFile.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } //在指定文本中讀取數據 public String readFile(String fileName) { String sLine = "",sResult = ""; boolean testFirstLine=true; try { DataInputStream inFile = new DataInputStream( new BufferedInputStream( new FileInputStream(fileName))); while ( (sLine = inFile.readLine()) != null) { if (testFirstLine) { sResult = sLine; testFirstLine=false; } else { sResult += "\n" + sLine; } } inFile.close(); return sResult; } catch (Exception e) { return null; } } //注冊字符串與文本中返回的字符串對比 public boolean ContractRegisterString() { String returnFileString; String md5String; String returnMd5String = ""; int i = 0; returnFileString = this.readFile("ControlValid.txt"); //獲得換行的位置 int pos = returnFileString.indexOf("\n"); if (pos != -1) { returnMd5String = returnFileString.substring(0,pos); } md5String = this.returnMd5String(); //System.out.println(returnFileString); //得到經過MD5加密過的字符串 if (md5String.equals(returnMd5String)) { return true; } else { return false; } } //用戶每登陸一次就把實際登陸次數變量加1 public void setLoginTimesTrue() { loginTimesTrue += 1; } //返回用戶實際登陸次數 public static int getLoginTimesTrue() { return loginTimesTrue; } //主方法測試用 public static void main(String[] args) { // int i = 0; ControlValid controlValid = new ControlValid(); //設置登陸次數 // for (i = 0; i < 25; i++) { // controlValid.setLoginTimesTrue(); // } //寫入內容到文件 controlValid.writeFile(); controlValid.ContractRegisterString(); } }