3DES加解密工具说明文档Word文件下载.docx
《3DES加解密工具说明文档Word文件下载.docx》由会员分享,可在线阅读,更多相关《3DES加解密工具说明文档Word文件下载.docx(24页珍藏版)》请在冰豆网上搜索。
64位明文,64位子密钥。
各个类的源码及详细说明:
Myframe类:
:
/*
*Createdon2004-11-13
*
*TODOTochangethetemplateforthisgeneratedfilegoto
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
importjava.awt.BorderLayout;
importjava.awt.Button;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.Frame;
importjava.awt.Panel;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjava.io.UnsupportedEncodingException;
importjava.io.*;
importjava.util.Date;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
importmydes.DES;
importmydes.THREE;
/**
*@authorTINA
*TODOTochangethetemplateforthisgeneratedtypecommentgoto
publicclassmyframeextendsJFrameimplementsActionListener
{
JButtonEnc=newJButton("
加密"
);
//加密按钮
JButtonDec=newJButton("
解密"
//解密按钮
JTextAreataLog=newJTextArea();
//状态显示区
JLabelfindfile=newJLabel("
文件名:
"
JButtonkey1=newJButton("
密钥"
//密钥输入按钮
JTextFieldfilepath=newJTextField(20);
//文件路径输入部分
JTextFieldkeytext=newJTextField(16);
//密钥输入部分
byte[]cipherKey1=newbyte[8];
//3DES的密钥K1
byte[]cipherKey2=newbyte[8];
//3DES的密钥K2
int[][]subKeys1=newint[16][48];
//用于存放K1产生的子密钥
int[][]subKeys2=newint[16][48];
//用于存放K2产生的子密钥
byte[]bZ={(byte)0x00,(byte)0x03,(byte)0x45,(byte)0xf3,(byte)0x33,(byte)0x21,(byte)0x75,(byte)0xed};
//初始向量Z
bytekey[]=newbyte[16];
publicstaticvoidmain(Stringargs[])//生成界面
{
myframeframe=newmyframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
publicmyframe()//界面的构造函数:
生成按钮及输入框
setTitle("
3DES"
setSize(400,300);
JPanelinput=newJPanel();
input.setLayout(newFlowLayout());
input.add(findfile);
input.add(filepath);
JPanelkeyarea=newJPanel();
keyarea.setLayout(newFlowLayout());
keyarea.add(key1);
keyarea.add(keytext);
JPanelinputarea=newJPanel();
inputarea.setLayout(newBorderLayout());
inputarea.add("
South"
keyarea);
Center"
input);
JPanelpnButtons=newJPanel();
pnButtons.setLayout(newFlowLayout());
pnButtons.add(Enc);
Enc.addActionListener(this);
pnButtons.add(Dec);
Dec.addActionListener(this);
ContainercontentPane=getContentPane();
contentPane.setLayout(newBorderLayout());
contentPane.add("
North"
inputarea);
taLog);
pnButtons);
publicvoidactionPerformed(ActionEventae)//事件监听方法
{
if(ae.getSource()==key1)//获得密钥
{
for(inti=0;
i<
16;
i++)
try
{
byte[]temp=keytext.getText().getBytes("
ASCII"
//获得输入框内字符ASCII码值
intlen=temp.length/2;
for(intj=0;
j<
len;
j++)
{cipherKey1[j]=temp[j];
}
{cipherKey2[j]=temp[j+8];
}
catch(UnsupportedEncodingExceptionuee){}
hide();
}
if(ae.getSource()==Enc)//进行加密
Stringtemp=filepath.getText();
Filefpt=newFile(temp);
//取得文件路径
Filefct=newFile(temp.concat("
.des"
));
filepath.setText(temp.concat("
//给加密文件自动添上.des的扩展名
longbegintime=(newDate()).getTime();
//记录加密起始时间
longlen=encrypt(fpt,fct);
//加密文件,返回加密文件长度
if(len>
0)
{
longendtime=(newDate()).getTime();
//取得结束时间
longtime=endtime-begintime;
taLog.append("
\nPlaintextlength:
"
+len+"
bytes\n"
//输出各项信息
Encryptiontime:
+time+"
milliseconds\n"
Encryptionspeed:
+(len*1000)/(time*1024)+"
kB/s\n"
}
elseif(ae.getSource()==Dec)//进行解密,具体类似加密过程
Stringtemp=filepath.getText();
Filefct=newFile(temp);
Filefdt=newFile(temp.concat("
.dec"
filepath.setText("
longbegintime=(newDate()).getTime();
longlen=decrypt(fct,fdt);
if(len>
longendtime=(newDate()).getTime();
longtime=endtime-begintime;
taLog.append("
\nCiphertextlength:
Decryptiontime:
Decryptionspeed:
publicbyte[]arrayM2Add(byte[]array1,byte[]array2)//两个字节型整数串模2加
intk=array1.length;
byte[]array=newbyte[k];
for(inti=0;
k;
array[i]=(byte)(array1[i]^array2[i]);
returnarray;
publicbyte[]arrayM2Add(byte[]array1,byte[]array2,byte[]array3)//三个字节型整数串模2加
array[i]=(byte)(array[i]^array3[i]);
publicvoidarraycopy(byte[]source,byte[]destination)//字节型整数串完全复制
source.length;
destination[i]=source[i];
publicvoidarraycopy(byte[]src,intsrcPos,byte[]dest,intdestPos,intlength)//字节型整数串选择性复制
length;
dest[destPos+i]=src[srcPos+i];
publiclongencrypt(Filefpt,Filefct)//将文件fpt加密成fct
subKeys1=DES.makeSubKeys(cipherKey1);
subKeys2=DES.makeSubKeys(cipherKey2);
try
if(fct.exists()==true)//判断密文是否存在{
fct.delete();
fct.createNewFile();
RandomAccessFilept=newRandomAccessFile(fpt,"
r"
//建立随机访问流
RandomAccessFilect=newRandomAccessFile(fct,"
rw"
byte[]bMi=newbyte[8];
//M(i)
byte[]bMt=newbyte[8];
//M(i-1)
byte[]bCi=newbyte[8];
//C(i)
byte[]bCt=newbyte[8];
//C(i-1)
longi=0;
longt=pt.length()/8;
//预算标准块个数
intk=(int)(pt.length()%8);
//记录短块大小
ct.write(THREE.encrypt(bZ,subKeys1,subKeys2));
//把初始向量Z当作第一个标准块加密,防止文件本身小于8个字节的情况发生
while(i<
t)//标准块加密
i++;
pt.read(bMi);
if(i==1)//第一次使用初始向量
bCi=THREE.encrypt(arrayM2Add(bMi,bZ),subKeys1,subKeys2);
//使用THREE类进行加密}
else
bCi=THREE.encrypt(arrayM2Add(bMi,bCt),subKeys1,subKeys2);
ct.write(bCi);
arraycopy(bCi,bCt);
arraycopy(bMi,bMt);
if(k>
0)//进入短快处理
byte[]bMn=newbyte[k];
//短块M(n)
pt.read(bMn);
byte[]b=newbyte[8-k];
//挪用临时数组b
arraycopy(bCt,k,b,0,8-k);
//C(n-1)右k位->
b
byte[]temp=newbyte[8];
arraycopy(b,0,temp,0,8-k);
arraycopy(bMn,0,temp,8-k,k);
//b和M(n)拼接,存入temp
ct.seek(ct.getFilePointer()-(8-k));
//文件写入指针变换ct.write(THREE.encrypt(temp,subKeys1,subKeys2));
longlength=pt.length();
pt.close();
ct.close();
returnlength;
catch(FileNotFoundExceptionfnf)//异常处理
taLog.setText("
FileNotFoundException\n"
return-1;
catch(IOExceptionioe)
IOException\n"
publiclongdecrypt(Filefct,Filefdt)//解密部分,同加密
if(fdt.exists()==true){
fdt.delete();
fdt.createNewFile();
RandomAccessFiledt=newRandomAccessFile(fdt,"
byte[]bCn=newbyte[8];
byte[]bMn=newbyte[8];
longt=ct.length()/8;
intk=(int)(ct.length()%8);
while((i<
t-1)||((i<
t)&
&
(k==0)))
ct.read(bCi);
if(i==0){}
elseif(i==1)
bMi=arrayM2Add(THREE.decrypt(bCi,subKeys1,subKeys2),bZ);
dt.write(bMi);
bMi=arrayM2Add(THREE.decrypt(bCi,subKeys1,subKeys2),bCt);
dt.write(bMi);
0){
ct.seek(ct.getFilePointer()+k);
//文件指针移至C(n)
ct.read(bCn);
bMn=THREE.decrypt(bCn,subKeys1,subKeys2);
ct.seek(ct.getFilePointer()-(k+8));
ct.read(bCi);
arraycopy(bMn,0,b,0,8-k);
arraycopy(b,0,bCi,k,8-k);
byte[]temp=newbyte[k];
arraycopy(bMn,8-k,temp,0,k);
dt.write(temp);
longlength=ct.length();
dt.close();
//返回密文大小
catch(FileNotFoundExceptionfnf)
THREEl类:
packagemydes;
publicclassTHREE//返回密文数组
publicstaticbyte[]encrypt(byte[]oword,int[][]SubKeys1,int[][]SubKeys2)
byte[]bTemp1=DES.encrypt(oword,SubKeys1);
//用子密钥组SubKeys1加密
byte[]bTemp2=DES.decrypt(bTemp1,SubKeys2);
byte[]bCiphertext=DES.encrypt(bTemp2,SubKeys1);
returnbCiphertext;
publicstaticbyte[]decrypt(byte[]sword,int[][]SubKeys1,int[][]SubKeys2)//返回明文数组
byte[]bTemp1=DES.decrypt