1、center文件名:input type=file name=Md5FilesEntity.filename /submit value=start/tr/table/form/center第二步,struts配置文件中的源代码- struts2配置文件 -strutsconstant value=truestruts.devModezh_CNstruts.localeutf-8struts.i18n.encodingpackage name=default extends=struts-defaultaction name=mdeAction class=com.crypt.action.m
2、deActionmethod=codeCryperesult name=success/index.jsp/actioncom.crypt.action.FilesCryptAction/success.jspfileCrypt2SetCrypt2/FilnallySuccess.jspfail/fail.jsp 第三步,经过struts2的跳转到FilesCryptActionpackage com.crypt.action;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax
3、.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.crypt.dao.Md5_dao;import com.opensymphony.xwork2.ActionSupport;import com.crypt.entity.Md5FilesEntity;public class FilesCryptAction extends ActionSupport private Md5FilesEntity Md5FilesEntity = new Md5FilesEntity();H
4、ttpServletRequest request = ServletActionContext.getRequest();HttpSession session = request.getSession();public Md5FilesEntity getMd5FilesEntity() return Md5FilesEntity;public void setMd5FilesEntity(Md5FilesEntity md5FilesEntity) Md5FilesEntity = md5FilesEntity;public String execute() throws IOExcep
5、tion String fileCode = null;try fileCode = Md5_dao.getFileCode(e: + Md5FilesEntity.getFilename();session.setAttribute(fileCode, fileCode);/将第一次算出来的MD5值暂时保存在session中 catch (Exception e) / TODO Auto-generated catch blocke.printStackTrace();Md5FilesEntity.setFileAfterHashCode1(fileCode);return ;public
6、String SetCrypt2() String fileCode2 = null;fileCode2 = Md5_dao.getFileCode(Md5FilesEntity.setFileAfterHashCode2(fileCode2);System.out.println(session1+session.getAttribute();if (fileCode2 .equals(session.getAttribute() return else 第四步,action中开始带值跳转到Md5_dao中package com.crypt.dao;import com.crypt.md5.
7、Md5Crypt_1;import com.crypt.md5.Md5Crypt_2;public class Md5_dao public static String getFileCode(String fileName) throws Exception System.out.println(fileName);String hashCode = Md5Crypt_1.getHash(fileName, MD5);System.out.println(hashCode);return hashCode;第五步,在系统中已经做好了一个给某文件计算其MD5值得API,所以在DAO中直接调用,
8、现在在Md5Crypt_1这个方法中直接处理package com.crypt.md5;import java.io.*;import java.security.*;public class Md5Crypt_1 public static char hexChar = 0, 1234567,89abcdef ;public static void main(String args) throws Exception String fileName = /111.txtString hashType = System.out.println(hashType + = + getHash(fi
9、leName, hashType);public static String getHash(String fileName, String hashType)throws Exception InputStream fis;fis = new FileInputStream(fileName);byte buffer = new byte1024;MessageDigest md5 = MessageDigest.getInstance(hashType);int numRead = 0;while (numRead = fis.read(buffer) 0) md5.update(buff
10、er, 0, numRead);fis.close();return toHexString(md5.digest();public static String toHexString(byte b) StringBuilder sb = new StringBuilder(b.length * 2);/ 构造一个字符串生成器,并初始化为指定的字符串内容for (int i = 0; i 4);/ append 方法始终将这些字符添加到生成器的末端sb.append(hexCharbi & 0x0f);return sb.toString();第六步,成功的计算出文件的MD5的值以后,在act
11、ion中将第一次的MD5存在Session中,并且通过struts2配置文件的配置,跳转到seccess.jsp中,显示第一次计算出的MD5值,并且提示开始进行第二次计算:Success.jsp中的源代码:body 第一次计算的md5值是: formtextarea style=text-align: left; rows=10 cols=41readonly=readonly#Md5FilesEntity.Md5FileCode/textarea 第七步,开始进行第二次提交文件,计算其MD5值,按照上述流程再进行一次计算以后,把第二次计算出来的MD5值与第一次的值进行比对,若两次的MD5相同
12、的,则说明没有篡改;若两次的MD5值不同,则说明文件遭到篡改。至此整个系统就完成了。文件防篡改系统测试将系统架在tomact 6.0上来实现web运行。下面是系统的测试图:第一步: 将需要计算的文件添加进来。并且打击“start”按钮开始计算。第二步,计算后的MD5,在success.jsp中显示第一次计算出来的MD5值,并且显示。下面还会提示进行第二次计算,以便于判断其是否遭过篡改。第三步,在文件没有篡改的情况下,进行第二次计算MD5值。若没有篡改,则会直接跳转没有跳转的页面,并且显示“文件没有被篡改”。若是被篡改过,则会显示文件遭篡改。第四步,对文件进行改动,然后重复上面的测试第一次计算:计算成功后:第五步,对文件进行刻意篡改,并进行计算。从原理上讲,文件遭到篡改,计算两次的MD5不一样,在进行比对的时候,应该会提示文件遭到篡改。至此,整个系统的测试全部完成。
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2