java 加密与加密.docx
《java 加密与加密.docx》由会员分享,可在线阅读,更多相关《java 加密与加密.docx(27页珍藏版)》请在冰点文库上搜索。
java加密与加密
学号
2015-2016学年第二学期
1408210136
《面向对象程序设计》
课程设计报告
题目:
文件的加密与解密
专业:
计算机科学与技术
班级:
14级
(2)班
姓名:
王欣
指导教师:
史学梅
成绩:
计算机学院
2016年5月10日
目录
1设计内容及要求...........................................................2
2总体设计.......................................................................2
3详细设计.......................................................................3
4总结..............................................................................14
致谢..................................................................................14
参考文献..........................................................................14
源代码..............................................................................14
1设计内容及要求
1.1设计要求
1.问题描述
通过编程,实现“加密与解密”功能。
给定任意一个文本文件,进行加密,生成另一个文件。
对加密后的文件还原。
2.基本要求
采用图形用户界面
1、给定任意一个文本文件,进行加密,生成另一个文件。
2、对加密后的文件还原
1.2流程图
图流程图
2总体设计
利用文件的输入输出流,按字符读取文件内容,把读取的内容,注意进行处理后,在重新写入新文件,实现文件加密。
用到的类:
FileRead,FileWrite.
添加窗体并布置面板,中间用到了,弹出对话框的事件。
设计的类有,Frame,Panel,JJOptionPane。
文件选择器的载入。
通过为按钮添加事件监听,来实现文件选择器的触发操作。
用到的类为:
JFileChooser。
自定义异常的种类,实现异常的灵活应用。
适当的调整程序的终止点或者循环点。
3详细设计
3.1代码分析
1加密函数
输入:
要加密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:
AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746
其中:
AD67EA2F3BE6E5ADDES密码一
D368DFE03120B5DFDES密码二
92A8FD8FEC2F0746DES密码三
输出:
对输入的文件加密后,保存到同一文件夹下增加了".tdes"扩展名的文件中。
代码:
publicstaticvoidmain(Stringargs[]){
FileEncrypterfe=newFileEncrypter();
fe.show();
}
FileEncrypterT(){
this.setSize(WIDTH,HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
Toolkittk=Toolkit.getDefaultToolkit();
DimensionscreenSize=tk.getScreenSize();
this.setLocation((screenSize.width-WIDTH)/2,
(screenSize.height-HEIGHT)/2);
this.setTitle("文件加密器(TriDES)");
Containerc=this.getContentPane();
c.setLayout(newFlowLayout());
finalFilePanelfp=newFilePanel("文件选择");
c.add(fp);
finalKeyPanelpp=newKeyPanel("密码");
c.add(pp);
JButtonjbE=newJButton("加密");
c.add(jbE);
jbE.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevent){
Filefile=newFile(fp.getFileName());
if(file.exists())
encrypt(file.getAbsoluteFile(),pp.getKey());
else
JOptionPane.showMessageDialog(
null,"请选择文件!
","提示",JOptionPane.OK_OPTION);
}
});
JButtonjbD=newJButton("解密");
c.add(jbD);
jbD.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevent){
Filefile=newFile(fp.getFileName());
if(file.exists())
decrypt(file.getAbsoluteFile(),pp.getKey());
else
JOptionPane.showMessageDialog(
null,"请选择文件!
","提示",JOptionPane.OK_OPTION);
}
});
}
privatevoidencrypt(FilefileIn,StringsKey){
try{
if(sKey.length()==48){
byte[]bytK1=getKeyByStr(sKey.substring(0,16));
byte[]bytK2=getKeyByStr(sKey.substring(16,32));
byte[]bytK3=getKeyByStr(sKey.substring(32,48));
FileInputStreamfis=newFileInputStream(fileIn);
byte[]bytIn=newbyte[(int)fileIn.length()];
for(inti=0;ibytIn[i]=(byte)fis.read();
}
//加密
byte[]bytOut=encryptByDES(encryptByDES(
encryptByDES(bytIn,bytK1),bytK2),bytK3);
StringfileOut=fileIn.getPath()+".tdes";
FileOutputStreamfos=newFileOutputStream(fileOut);
for(inti=0;ifos.write((int)bytOut[i]);
}
fos.close();
JOptionPane.showMessageDialog(
this,"加密成功!
","提示",JOptionPane.OK_OPTION);
}else
JOptionPane.showMessageDialog(
this,"密码长度必须等于48!
","错误信息",JOptionPane.ERROR_MESSAGE);
}catch(Exceptione){
e.printStackTrace();
}
}
2解密函数
输入:
要解密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:
AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746
其中:
AD67EA2F3BE6E5ADDES密码一
D368DFE03120B5DFDES密码二
92A8FD8FEC2F0746DES密码三
输出:
对输入的文件解密后,保存到用户指定的文件中。
代码:
privatevoiddecrypt(FilefileIn,StringsKey){
try{
if(sKey.length()==48){
StringstrPath=fileIn.getPath();
if(strPath.substring(strPath.length()-5).toLowerCase().equals(".tdes"))
strPath=strPath.substring(0,strPath.length()-5);
else{
JOptionPane.showMessageDialog(
this,"不是合法的加密文件!
","提示",JOptionPane.OK_OPTION);
return;
}
JFileChooserchooser=newJFileChooser();
chooser.setCurrentDirectory(newFile("."));
chooser.setSelectedFile(newFile(strPath));
//用户指定要保存的文件
intret=chooser.showSaveDialog(this);
if(ret==JFileChooser.APPROVE_OPTION){
byte[]bytK1=getKeyByStr(sKey.substring(0,16));
byte[]bytK2=getKeyByStr(sKey.substring(16,32));
byte[]bytK3=getKeyByStr(sKey.substring(32,48));
FileInputStreamfis=newFileInputStream(fileIn);
byte[]bytIn=newbyte[(int)fileIn.length()];
for(inti=0;ibytIn[i]=(byte)fis.read();
}
//解密
byte[]bytOut=decryptByDES(decryptByDES(
decryptByDES(bytIn,bytK3),bytK2),bytK1);
FilefileOut=chooser.getSelectedFile();
fileOut.createNewFile();
FileOutputStreamfos=newFileOutputStream(fileOut);
for(inti=0;ifos.write((int)bytOut[i]);
}
fos.close();
JOptionPane.showMessageDialog(
this,"解密成功!
","提示",JOptionPane.OK_OPTION);
}
}else
JOptionPane.showMessageDialog(
this,"密码长度必须等于48!
","错误信息",JOptionPane.ERROR_MESSAGE);
}catch(Exceptione){
JOptionPane.showMessageDialog(
this,"解密失败,请核对密码!
","提示",JOptionPane.OK_OPTION);
}
}
3用DES方法加密输入的字节
bytKey需为8字节长,是加密的密码
代码:
privatebyte[]encryptByDES(byte[]bytP,byte[]bytKey)throwsException{
DESKeySpecdesKS=newDESKeySpec(bytKey);
SecretKeyFactoryskf=SecretKeyFactory.getInstance("DES");
SecretKeysk=skf.generateSecret(desKS);
Ciphercip=Cipher.getInstance("DES");
cip.init(Cipher.ENCRYPT_MODE,sk);
returncip.doFinal(bytP);
}
4用DES方法解密输入的字节
bytKey需为8字节长,是解密的密码
代码:
privatebyte[]decryptByDES(byte[]bytE,byte[]bytKey)throwsException{
DESKeySpecdesKS=newDESKeySpec(bytKey);
SecretKeyFactoryskf=SecretKeyFactory.getInstance("DES");
SecretKeysk=skf.generateSecret(desKS);
Ciphercip=Cipher.getInstance("DES");
cip.init(Cipher.DECRYPT_MODE,sk);
returncip.doFinal(bytE);
}
5输入密码的字符形式,返回字节数组形式。
如输入字符串:
AD67EA2F3BE6E5AD
返回字节数组:
{173,103,234,47,59,230,229,173}
代码:
privatebyte[]getKeyByStr(Stringstr){
byte[]bRet=newbyte[str.length()/2];
for(inti=0;iIntegeritg=
newInteger(16*getChrInt(str.charAt(2*i))+getChrInt(str.charAt(2*i+1)));
bRet[i]=itg.byteValue();
}
returnbRet;
}
6计算一个16进制字符的10进制值
输入:
0-F
代码:
privateintgetChrInt(charchr){
intiRet=0;
if(chr=="0".charAt(0))iRet=0;
if(chr=="1".charAt(0))iRet=1;
if(chr=="2".charAt(0))iRet=2;
if(chr=="3".charAt(0))iRet=3;
if(chr=="4".charAt(0))iRet=4;
if(chr=="5".charAt(0))iRet=5;
if(chr=="6".charAt(0))iRet=6;
if(chr=="7".charAt(0))iRet=7;
if(chr=="8".charAt(0))iRet=8;
if(chr=="9".charAt(0))iRet=9;
if(chr=="A".charAt(0))iRet=10;
if(chr=="B".charAt(0))iRet=11;
if(chr=="C".charAt(0))iRet=12;
if(chr=="D".charAt(0))iRet=13;
if(chr=="E".charAt(0))iRet=14;
if(chr=="F".charAt(0))iRet=15;
returniRet;
}
7文件选择组件
代码:
classFilePanelextendsJPanel{
FilePanel(Stringstr){
JLabellabel=newJLabel(str);
JTextFieldfileText=newJTextField(35);
JButtonchooseButton=newJButton("浏览...");
this.add(label);
this.add(fileText);
this.add(chooseButton);
clickActionca=newclickAction(this);
chooseButton.addActionListener(ca);
}
publicStringgetFileName(){
JTextFieldjtf=(JTextField)this.getComponent
(1);
returnjtf.getText();
}
privateclassclickActionimplementsActionListener{
clickAction(Componentc){
cmpt=c;
}
publicvoidactionPerformed(ActionEventevent){
JFileChooserchooser=newJFileChooser();
chooser.setCurrentDirectory(newFile("."));
intret=chooser.showOpenDialog(cmpt);
if(ret==JFileChooser.APPROVE_OPTION){
JPaneljp=(JPanel)cmpt;
JTextFieldjtf=(JTextField)jp.getComponent
(1);
jtf.setText(chooser.getSelectedFile().getPath());
}
}
privateComponentcmpt;
}
}
8密码生成组件
代码:
classKeyPanelextendsJPanel{
KeyPanel(Stringstr){
JLabellabel=newJLabel(str);
JTextFieldfileText=newJTextField(35);
JButtonchooseButton=newJButton("随机产生");
this.add(label);
this.add(fileText);
this.add(chooseButton);
clickActionca=newclickAction(this);
chooseButton.addActionListener(ca);
}
//返回生成的密码(48个字符长度)
publicStringgetKey(){
JTextFieldjtf=(JTextField)this.getComponent
(1);
returnjtf.getText();
}
privateclassclickActionimplementsActionListener{
clickAction(Componentc){
cmpt=c;
}
publicvoidactionPerformed(ActionEventevent){
try{
KeyGeneratorkg=KeyGenerator.getInstance("DES");
kg.init(56);
Keyke=kg.generateKey();
byte[]bytK1=ke.getEncoded();
ke=kg.generateKey();
byte[]bytK2=ke.getEncoded();
ke=kg.generateKey();
byte[]bytK3=ke.getEncoded();
JPaneljp=(JPanel)cmpt;
JTextFieldjtf=(JTextField)jp.getComponent
(1);
jtf.setText(getByteStr(bytK1)+getByteStr(bytK2)+getByteStr(bytK3));
}catch(Exceptione){
e.printStackTrace();
}
}
privateStringgetByteStr(byte[]byt){
StringstrRet="";
for(inti=0;i//System.out.println(byt[i]);
strRet+=getHexValue((byt[i]&240)/16);
strRet+=getHexValue(byt[i]&15);
}
returnstrRet;
}
privateStringgetHexValue(ints){
StringsRet=null;
switch(s){
case0:
sRet="0";break;
case1:
sRet="1";break;
case2:
sRet="2";break;
case3:
sRet="3";break;
case4:
sRet="4";break;
case5:
sRet="5";break;
case6:
sRet="6";break;
case7:
sRet="7";break;
case8:
sRet="8";break;
case9:
sRet="9";break;
case10:
sRet="A";break;
case11:
sRet="B";break;
case12:
sRet="C";break;
case13:
sRet="D";break;
case14:
sRet="E";break;
case15:
sRet="F";
}
returnsRet;