文件防篡改系统(MD5加密实现)Word格式.doc
《文件防篡改系统(MD5加密实现)Word格式.doc》由会员分享,可在线阅读,更多相关《文件防篡改系统(MD5加密实现)Word格式.doc(9页珍藏版)》请在冰豆网上搜索。
![文件防篡改系统(MD5加密实现)Word格式.doc](https://file1.bdocx.com/fileroot1/2022-10/4/afc917a6-da15-4ec2-9909-1d48d8c53538/afc917a6-da15-4ec2-9909-1d48d8c535381.gif)
系统实现
第一步,Index.jsp中的源代码,
<
!
--Index.jsp 在这里添加系统外存中的文件-->
<
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.app