文件防篡改系统(MD5加密实现)Word文档格式.doc
《文件防篡改系统(MD5加密实现)Word文档格式.doc》由会员分享,可在线阅读,更多相关《文件防篡改系统(MD5加密实现)Word文档格式.doc(9页珍藏版)》请在冰点文库上搜索。
![文件防篡改系统(MD5加密实现)Word文档格式.doc](https://file1.bingdoc.com/fileroot1/2023-4/29/ce201abf-c5cd-4934-8846-2cf623b16e29/ce201abf-c5cd-4934-8846-2cf623b16e291.gif)
center>
<
formaction="
fileCrypt"
method="
get"
>
<
table>
<
tr>
<
td>
文件名:
<
/td>
inputtype="
file"
name="
Md5FilesEntity.filename"
/>
submit"
value="
start"
/tr>
/table>
/form>
/center>
第二步,struts配置文件中的源代码
--struts2配置文件-->
struts>
<
constantvalue="
true"
struts.devMode"
zh_CN"
struts.locale"
utf-8"
struts.i18n.encoding"
packagename="
default"
extends="
struts-default"
actionname="
mdeAction"
class="
com.crypt.action.mdeAction"
method="
codeCrype"
resultname="
success"
/index.jsp<
/result>
/action>
com.crypt.action.FilesCryptAction"
/success.jsp<
fileCrypt2"
SetCrypt2"
/FilnallySuccess.jsp<
fail"
/fail.jsp<
/struts>
第三步,经过struts2的跳转到FilesCryptAction
packagecom.crypt.action;
importjava.io.IOException;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
importorg.apache.struts2.ServletActionContext;
importcom.crypt.dao.Md5_dao;
importcom.opensymphony.xwork2.ActionSupport;
importcom.crypt.entity.Md5FilesEntity;
publicclassFilesCryptActionextendsActionSupport{
privateMd5FilesEntityMd5FilesEntity=newMd5FilesEntity();
HttpServletRequestrequest=ServletActionContext.getRequest();
HttpSessionsession=request.getSession();
publicMd5FilesEntitygetMd5FilesEntity(){
returnMd5FilesEntity;
}
publicvoidsetMd5FilesEntity(Md5FilesEntitymd5FilesEntity){
Md5FilesEntity=md5FilesEntity;
publicStringexecute()throwsIOException{
StringfileCode=null;
try{
fileCode=Md5_dao.getFileCode("
e:
"
+Md5FilesEntity.getFilename());
session.setAttribute("
fileCode"
fileCode);
//将第一次算出来的MD5值暂时保存在session中
}catch(Exceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
Md5FilesEntity.setFileAfterHashCode1(fileCode);
return"
;
publicStringSetCrypt2(){
StringfileCode2=null;
fileCode2=Md5_dao
.getFileCode("
Md5FilesEntity.setFileAfterHashCode2(fileCode2);
System.out.println("
session1"
+session.getAttribute("
));
if(fileCode2.equals(session.getAttribute("
))){
return"
}else{
}
第四步,action中开始带值跳转到Md5_dao中
packagecom.crypt.dao;
importcom.crypt.md5.Md5Crypt_1;
importcom.crypt.md5.Md5Crypt_2;
publicclassMd5_dao{
publicstaticStringgetFileCode(StringfileName)throwsException{
System.out.println(fileName);
StringhashCode=Md5Crypt_1.getHash(fileName,"
MD5"
);
System.out.println(hashCode);
returnhashCode;
}
第五步,在系统中已经做好了一个给某文件计算其MD5值得API,所以在DAO中直接调用,现在在Md5Crypt_1这个方法中直接处理
packagecom.crypt.md5;
importjava.io.*;
importjava.security.*;
publicclassMd5Crypt_1{
publicstaticchar[]hexChar={'
0'
'
1'
2'
3'
4'
5'
6'
7'
'
8'
9'
a'
b'
c'
d'
e'
f'
};
publicstaticvoidmain(String[]args)throwsException{
StringfileName="
/111.txt"
StringhashType="
System.out.println(hashType+"
=="
+getHash(fileName,hashType));
publicstaticStringgetHash(StringfileName,StringhashType)
throwsException{
InputStreamfis;
fis=newFileInputStream(fileName);
byte[]buffer=newbyte[1024];
MessageDigestmd5=MessageDigest.getInstance(hashType);
intnumRead=0;
while((numRead=fis.read(buffer))>
0){
md5.update(buffer,0,numRead);
fis.close();
returntoHexString(md5.digest());
publicstaticStringtoHexString(byte[]b){
StringBuildersb=newStringBuilder(b.length*2);
//构造一个字符串生成器,并初始化为指定的字符串内容
for(inti=0;
i<
b.length;
i++){
sb.append(hexChar[(b[i]&
0xf0)>
4]);
//append方法始终将这些字符添加到生成器的末端
sb.append(hexChar[b[i]&
0x0f]);
returnsb.toString();
第六步,成功的计算出文件的MD5的值以后,在action中将第一次的MD5存在Session中,并且通过struts2配置文件的配置,跳转到seccess.jsp中,显示第一次计算出的MD5值,并且提示开始进行第二次计算:
Success.jsp中的源代码:
body>
第一次计算的md5值是:
<
s:
iteratorvalue="
Md5FilesEntity"
id="
status="
L"
propertyvalue="
#Md5FilesEntity.fileAfterHashCode1"
/s:
iterator>
form>
textareastyle="
text-align:
left;
rows="
10"
cols="
41"
readonly="
readonly"
#Md5FilesEntity.Md5FileCode"
/textarea>
<
/body>
第七步,开始进行第二次提交文件,计算其MD5值,按照上述流程再进行一次计算以后,把第二次计算出来的MD5值与第一次的值进行比对,若两次的MD5 相同的,则说明没有篡改;
若两次的MD5值不同,则说明文件遭到篡改。
至此整个系统就完成了。
文件防篡改系统测试
将系统架在tomact6.0上来实现web运行。
下面是系统的测试图:
第一步:
将需要计算的文件添加进来。
并且打击“start”按钮开始计算。
第二步,计算后的MD5,在success.jsp中显示第一次计算出来的MD5值,并且显示。
下面还会提示进行第二次计算,以便于判断其是否遭过篡改。
第三步,在文件没有篡改的情况下,进行第二次计算MD5值。
若没有篡改,则会直接跳转没有跳转的页面,并且显示“文件没有被篡改”。
若是被篡改过,则会显示文件遭篡改。
第四步,对文件进行改动,然后重复上面的测试
第一次计算:
计算成功后:
第五步,对文件进行刻意篡改,并进行计算。
从原理上讲,文件遭到篡改,计算两次的MD5不一样,在进行比对的时候,应该会提示文件遭到篡改。
至此,整个系统的测试全部完成。