文件加密与解密Java课程设计报告.docx

上传人:b****5 文档编号:14367339 上传时间:2023-06-22 格式:DOCX 页数:15 大小:284.84KB
下载 相关 举报
文件加密与解密Java课程设计报告.docx_第1页
第1页 / 共15页
文件加密与解密Java课程设计报告.docx_第2页
第2页 / 共15页
文件加密与解密Java课程设计报告.docx_第3页
第3页 / 共15页
文件加密与解密Java课程设计报告.docx_第4页
第4页 / 共15页
文件加密与解密Java课程设计报告.docx_第5页
第5页 / 共15页
文件加密与解密Java课程设计报告.docx_第6页
第6页 / 共15页
文件加密与解密Java课程设计报告.docx_第7页
第7页 / 共15页
文件加密与解密Java课程设计报告.docx_第8页
第8页 / 共15页
文件加密与解密Java课程设计报告.docx_第9页
第9页 / 共15页
文件加密与解密Java课程设计报告.docx_第10页
第10页 / 共15页
文件加密与解密Java课程设计报告.docx_第11页
第11页 / 共15页
文件加密与解密Java课程设计报告.docx_第12页
第12页 / 共15页
文件加密与解密Java课程设计报告.docx_第13页
第13页 / 共15页
文件加密与解密Java课程设计报告.docx_第14页
第14页 / 共15页
文件加密与解密Java课程设计报告.docx_第15页
第15页 / 共15页
亲,该文档总共15页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

文件加密与解密Java课程设计报告.docx

《文件加密与解密Java课程设计报告.docx》由会员分享,可在线阅读,更多相关《文件加密与解密Java课程设计报告.docx(15页珍藏版)》请在冰点文库上搜索。

文件加密与解密Java课程设计报告.docx

文件加密与解密Java课程设计报告

JAVA课程设计

题目:

文件的加密与解密

 

姓名:

学号:

班级:

日期:

 

指导老师

分数

 

一、设计思路

自从Java技术出现以业,有关Java平台的安全性用由Java技术发展所引发的安全性问题,引起了越来越多的关注。

目前,Java已经大量应用于各个领域,研究Java的安全性对于更好地利用Java具有深远的意义。

使用Java的安全机制设计和实现安全系统更具有重要的应用价值。

本课程设计,主要实践Java安全中的JCE模块,包括密钥生成,Cipher对象初始化、加密模式、填充模式、底层算法参数传递,也涉及文件读写与对象输入输出流。

二、具体实现

本系统通过用户界面接收三个参数:

明文文件、密文文件、口令。

采用DES加密算法,密码分组链(CipherBlockChaining,CBC)加密模式,PKCS#5-Padding的分组填充算法。

因为CBC涉及到底层算法参数的解密密钥的传递,所以将明文文件中的字节块以密封对象(SealedObject)的方式加密后,用对象流输出到密文文件,这样就将密文、算法参数、解密密钥三都密封到一个对象中了。

口令的hash值作为产生密钥的参数。

设计流程图如下所示:

文件加密与解密设计流程图

 

本系统中,包含Default,Shares,SecretKey,EncAndDec四个包共6个类组成。

定义的几个参数:

MAX_BUF_SIZE为每次从文件中读取的字节数,也是内存缓冲区的大小;加密算法为DES;加密模式是密码分组链(CBC)模式;分组填充方式是PKCS#5Padding。

包和类结构图如下所示:

本课程设计,包和类结构图:

PackageDefault->ClassJCEDialog

1)

1)

Publicbyet[]HashDigest(StringStrPassword)

PublicSecretKeyGenerateKey(byte[]KeyData)

PublicboolenEncryptFile(SecretKeySecKey1,StringPlainFilePath1,StringCipherFilePath1)

PublicboolenDecryptFile(SecretKeySecKey2,StringPlainFilePath2,StringCipherFilePath2)

ava项目设计与开发范例.北京.电子工业出版社,

[2]朱福喜,尹为民等编著.Java语言与面向对象程序设计.武汉:

武汉大学出版社,

[3]冯军,程超等编著.JBuilder程序设计.北京:

中国水利水电出版社,

[4]丁振凡,黎章等编著.Java语言实用教程..北京:

北京邮电大学出版社,

[5]何桥,李肃义等编著.Java程序设计简明教程.北京:

中国水利水电出版社,

六、附录

程序的主要代码:

主界面模块主要代码

//加密按钮事件的处理方法

(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

HashDigesthashDigest=newHashDigest();

GenerateKeygenerateKey=newGenerateKey();

Encenc=newEnc();

//TODOAuto-generatedmethodstub

StringPlainFilePath1,CipherFilePath1,Strpasswrod1;

PlainFilePath1=();

CipherFilePath1=();

//Strpasswrod1=();

Strpasswrod1=newString());

//byte[]KeyData1=(Strpasswrod1);//生成密码的HASH值

SecKey1=(KeyData1);//由输入的密码的hash值,产生加密密钥

booleanEncOK=(SecKey1,PlainFilePath1,CipherFilePath1);//调用文件加密算法

("");

if(EncOK==true)

{

(null,"文件加密成功!

");

}

else{

(null,"文件加密失败!

");

}

}

});

//解密按钮事件的处理方法

(newActionListener(){

@Override

publicvoidactionPerformed(ActionEvente){

//TODOAuto-generatedmethodstub

HashDigesthashDigest=newHashDigest();

GenerateKeygenerateKey=newGenerateKey();

Decdec=newDec();

StringPlainFilePath2,CipherFilePath2,StrPasswrod2;

PlainFilePath2=();

CipherFilePath2=();

//StrPasswrod2=();

StrPasswrod2=newString());

byte[]KeyData2=(StrPasswrod2);

SecKey2=(KeyData2);

booleanDecOK=(SecKey2,PlainFilePath2,CipherFilePath2);

("");

if(DecOK==true)

{

(null,"文件解密成功!

");

}

else{

(null,"文件解密失败!

");

}

}

});

密码hash值生成

publicclassHashDigest{

publicbyte[]HashDigest(StringStrPassword)

{

byte[]BytePasswrod=();

StringDigestAlgorithm="SHA-1";

byte[]KeyMaterial={};

try

{

MesDigest=(DigestAlgorithm);

(BytePasswrod);

KeyMaterial=();

returnKeyMaterial;

}

catche1)

{

();

returnnull;

}

}

}

Hash产生密钥算法

publicclassGenerateKey{

publicSecretKeyGenerateKey(byte[]KeyData)

{

StringAlogorithm="DES";

try

{

DesKeySpec=newDESKeySpec(KeyData);

SecKeyFac=(Alogorithm);

secKey=(DesKeySpec);

returnsecKey;

}

catche1)

{

();

returnnull;

}

catche2)

{

();

returnnull;

}

}

}

文件加密算法

publicclassEnc{

finalintMAX_BUF_SIZE=1024;

StringAlgorithm="DES";

StringEncryptMode="CBC";

StringPaddingScheme="PKCS5Padding";

publicbooleanEncryptFile(SecretKeySecKey1,StringPlainFilePath1,StringCipherFilePath1)

{

try

{

//加密,将明文文件加密到另一个文件

Cipher1=(Algorithm+"/"+EncryptMode+"/"+PaddingScheme);

SecKey1);

PlainFile1=newFile(PlainFilePath1);

CipherFile1=newFile(CipherFilePath1);

longFileLen1=();

if(FileLen1<=0)

{

(null,"明文文件是空的!

");

returnfalse;

}

fis1=newFileInputStream(PlainFile1);

fos1=newFileOutputStream(CipherFile1);

ObjOS1=newObjectOutputStream(fos1);

byte[]buffer1=newbyte[MAX_BUF_SIZE];

inti=(buffer1);

while(i!

=-1)

{

SealedObj1=newSealedObject(buffer1,Cipher1);

(SealedObj1);

i=(buffer1);

}

();

();

();

();

returntrue;

}

catche1)

{

();

returnfalse;

}

catche2)

{

();

returnfalse;

}

catche3)

{

();

returnfalse;

}

}

}

文件解密算法

publicclassDec{

finalintMAX_BUF_SIZE=1024;

publicbooleanDecryptFile(SecretKeySecKey2,StringPlainFilePath2,StringCipherFilePath2)

{

try

{

PlainFile2=newFile(PlainFilePath2);

CipherFile2=newFile(CipherFilePath2);

longFileLen2=();

if(FileLen2<=0)

{

(null,"密文文件是空的!

");

returnfalse;

}

fis2=newFileInputStream(CipherFile2);

ObjIS2=newObjectInputStream(fis2);

fos2=newFileOutputStream(PlainFile2);

byte[]buffer2=newbyte[MAX_BUF_SIZE];

SealedObj2=null;

SealedObj2=(SealedObject)();

while(SealedObj2!

=null)

{

buffer2=(byte[])(SecKey2);

(buffer2);

SealedObj2=(SealedObject)();

}

();

();

();

();

returntrue;

}

catche1)

{

();

returnfalse;

}

catche2)

{

"解密完成!

");

returntrue;

}

catche3)

{

();

returnfalse;

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2