当前位置:文档之家› 文件加密与解密—Java课程设计报告

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

JAVA课程设计题目:文件的加密与解密姓名:学号:班级: 日期:目录一、设计思路 (3)二、具体实现 (3)三、运行调试与分析讨论 (6)四、设计体会与小结 (8)五、参考文献 (8)六、附录 (8)一、设计思路自从Java技术出现以业,有关Java平台的安全性用由Java技术发展所引发的安全性问题,引起了越来越多的关注。

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

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

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

二、具体实现本系统通过用户界面接收三个参数:明文文件、密文文件、口令。

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

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

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

设计流程图如下所示:文件加密与解密设计流程图本系统中,包含Default,Shares,SecretKey,EncAndDec四个包共6个类组成。

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

包和类结构图如下所示:本课程设计,包和类结构图:Public boolen DecryptFile(SecretKey SecKey2,String PlainFilePath2,String CipherFilePath2)错误!未指定顺序。

错误!未指定顺序。

错误!未指定顺序。

错误!未指定顺序。

错误!未指定顺序。

ava项目设计与开发范例.北京.电子工业出版社,[2]朱福喜,尹为民等编著.Java语言与面向对象程序设计.武汉:武汉大学出版社,[3]冯军,程超等编著.JBuilder 程序设计.北京:中国水利水电出版社,[4]丁振凡,黎章等编著.Java语言实用教程..北京:北京邮电大学出版社,[5]何桥,李肃义等编著.Java程序设计简明教程.北京:中国水利水电出版社,六、附录程序的主要代码:主界面模块主要代码//加密按钮事件的处理方法(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {HashDigest hashDigest=new HashDigest();GenerateKey generateKey=new GenerateKey();Enc enc=new Enc();// TODO Auto-generated method stubString PlainFilePath1,CipherFilePath1,Strpasswrod1;PlainFilePath1=();CipherFilePath1=();//Strpasswrod1 = ();Strpasswrod1 = new String());//byte[] KeyData1= (Strpasswrod1);//生成密码的HASH值SecKey1=(KeyData1); //由输入的密码的hash值,产生加密密钥boolean EncOK=(SecKey1, PlainFilePath1, CipherFilePath1);//调用文件加密算法("");if(EncOK==true){(null, "文件加密成功!");}else {(null, "文件加密失败!");}}});//解密按钮事件的处理方法(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubHashDigest hashDigest=new HashDigest();GenerateKey generateKey=new GenerateKey();Dec dec =new Dec();String PlainFilePath2,CipherFilePath2,StrPasswrod2;PlainFilePath2=();CipherFilePath2=();//StrPasswrod2=();StrPasswrod2=new String());byte[] KeyData2=(StrPasswrod2);SecKey2=(KeyData2);boolean DecOK=(SecKey2, PlainFilePath2, CipherFilePath2);("");if(DecOK==true){(null, "文件解密成功!");}else {(null, "文件解密失败!");}}});密码hash值生成public class HashDigest {public byte[] HashDigest(String StrPassword) {byte[] BytePasswrod=();String DigestAlgorithm ="SHA-1";byte[] KeyMaterial ={};try{MesDigest=(DigestAlgorithm);(BytePasswrod);KeyMaterial=();return KeyMaterial;}catch e1){();return null;}}}Hash产生密钥算法public class GenerateKey {public SecretKey GenerateKey(byte[] KeyData) {String Alogorithm="DES";try{DesKeySpec=new DESKeySpec(KeyData);SecKeyFac=(Alogorithm);secKey=(DesKeySpec);return secKey;}catch e1){();return null;}catch e2){();return null;}}}文件加密算法public class Enc {final int MAX_BUF_SIZE=1024;String Algorithm="DES";String EncryptMode="CBC";String PaddingScheme="PKCS5Padding";public boolean EncryptFile(SecretKey SecKey1,String PlainFilePath1,StringCipherFilePath1){try{//加密,将明文文件加密到另一个文件Cipher1 = (Algorithm + "/" + EncryptMode + "/" + PaddingScheme);,SecKey1);PlainFile1=new File(PlainFilePath1);CipherFile1=new File(CipherFilePath1);long FileLen1=();if(FileLen1<=0){(null, "明文文件是空的!");return false;}fis1=new FileInputStream(PlainFile1);fos1=new FileOutputStream(CipherFile1);ObjOS1=new ObjectOutputStream(fos1);byte[] buffer1 = new byte[MAX_BUF_SIZE];int i=(buffer1);while(i!=-1){SealedObj1=new SealedObject(buffer1, Cipher1);(SealedObj1);i=(buffer1);}();();();();return true;}catch e1){();return false;}catch e2){();return false;}catch e3){();return false;}}}文件解密算法public class Dec {final int MAX_BUF_SIZE=1024;public boolean DecryptFile(SecretKey SecKey2,String PlainFilePath2,String CipherFilePath2){try{PlainFile2=new File(PlainFilePath2);CipherFile2=new File(CipherFilePath2);long FileLen2=();if(FileLen2<=0){(null, "密文文件是空的!");return false;}fis2=new FileInputStream(CipherFile2);ObjIS2=new ObjectInputStream(fis2);fos2=new FileOutputStream(PlainFile2);byte[] buffer2=new byte[MAX_BUF_SIZE];SealedObj2=null;SealedObj2=(SealedObject)();while(SealedObj2!=null){buffer2=(byte[])(SecKey2);(buffer2);SealedObj2=(SealedObject)();}();();();();return true;}catch e1){();return false;}catch e2){"解密完成!");return true;}catch e3){();return false;}}}。

相关主题