1、文件加密与解密Java课程设计报告JAVA课程设计 题目: 文件的加密与解密 姓 名: 学 号: 班 级: 日 期: 指导老师分 数一、 设计思路自从Java技术出现以业,有关Java平台的安全性用由Java技术发展所引发的安全性问题,引起了越来越多的关注。目前,Java已经大量应用于各个领域,研究Java的安全性对于更好地利用Java具有深远的意义。使用Java的安全机制设计和实现安全系统更具有重要的应用价值。本课程设计,主要实践Java安全中的JCE模块,包括密钥生成,Cipher对象初始化、加密模式、填充模式、底层算法参数传递,也涉及文件读写与对象输入输出流。二、具体实现本系统通过用户界
2、面接收三个参数:明文文件、密文文件、口令。采用DES加密算法,密码分组链(Cipher Block Chaining,CBC)加密模式,PKCS#5-Padding的分组填充算法。因为CBC涉及到底层算法参数的解密密钥的传递,所以将明文文件中的字节块以密封对象(Sealed Object)的方式加密后,用对象流输出到密文文件,这样就将密文、算法参数、解密密钥三都密封到一个对象中了。口令的hash值作为产生密钥的参数。设计流程图如下所示:文件加密与解密设计流程图本系统中,包含Default,Shares,SecretKey,EncAndDec四个包共6个类组成。定义的几个 参数:MAX_BUF_
3、SIZE为每次从文件中读取的字节数,也是内存缓冲区的大小;加密算法为DES;加密模式是密码分组链(CBC)模式;分组填充方式是PKCS#5Padding。包和类结构图如下所示:本课程设计,包和类结构图:Package Default - Class JCEDialog1) 1) Public byet HashDigest(String StrPassword) Public SecretKey GenerateKey(byte KeyData) Public boolen EncryptFile(SecretKey SecKey1,String PlainFilePath1,String C
4、ipherFilePath1) Public boolen DecryptFile(SecretKey SecKey2,String PlainFilePath2,String CipherFilePath2)ava项目设计与开发范例.北京.电子工业出版社,2朱福喜,尹为民等编著.Java语言与面向对象程序设计.武汉:武汉大学出版社, 3冯军,程超等编著.JBuilder 程序设计.北京:中国水利水电出版社,4丁振凡,黎章等编著.Java语言实用教程.北京:北京邮电大学出版社,5何桥,李肃义等编著.Java程序设计简明教程.北京:中国水利水电出版社,六、附录程序的主要代码:主界面模块主要代码/
5、加密按钮事件的处理方法(new ActionListener() Override public void actionPerformed(ActionEvent e) HashDigest hashDigest=new HashDigest(); GenerateKey generateKey=new GenerateKey(); Enc enc=new Enc(); / TODO Auto-generated method stub String PlainFilePath1,CipherFilePath1,Strpasswrod1; PlainFilePath1=(); CipherFi
6、lePath1=(); /Strpasswrod1 = (); Strpasswrod1 = new String(); / byte KeyData1= (Strpasswrod1);/生成密码的HASH值 SecKey1=(KeyData1); /由输入的密码的hash值,产生加密密钥 boolean EncOK=(SecKey1, PlainFilePath1, CipherFilePath1);/调用文件加密算法 (); if(EncOK=true) (null, 文件加密成功!); else (null, 文件加密失败!); );/解密按钮事件的处理方法(new ActionList
7、ener() Override public void actionPerformed(ActionEvent e) / TODO Auto-generated method stub HashDigest hashDigest=new HashDigest(); GenerateKey generateKey=new GenerateKey(); Dec dec =new Dec(); String PlainFilePath2,CipherFilePath2,StrPasswrod2; PlainFilePath2=(); CipherFilePath2=(); /StrPasswrod2
8、=(); 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=(); S
9、tring 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(KeyDat
10、a); 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,Str
11、ing PlainFilePath1,String CipherFilePath1) try /加密,将明文文件加密到另一个文件 Cipher1 = (Algorithm + / + EncryptMode + / + PaddingScheme); ,SecKey1); PlainFile1=new File(PlainFilePath1); CipherFile1=new File(CipherFilePath1); long FileLen1=(); if(FileLen1=0) (null, 明文文件是空的!); return false; fis1=new FileInputStre
12、am(PlainFile1); fos1=new FileOutputStream(CipherFile1); ObjOS1=new ObjectOutputStream(fos1); byte buffer1 = new byteMAX_BUF_SIZE; int i=(buffer1); while(i!=-1) SealedObj1=new SealedObject(buffer1, Cipher1); (SealedObj1); i=(buffer1); (); (); (); (); return true; catch e1) (); return false; catch e2)
13、 (); 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
14、=0) (null, 密文文件是空的!); return false; fis2=new FileInputStream(CipherFile2); ObjIS2=new ObjectInputStream(fis2); fos2=new FileOutputStream(PlainFile2); byte buffer2=new byteMAX_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;
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2