📄 filedes.java
字号:
package DES;
import DES.mmsSecDilog;
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import com.jgoodies.forms.layout.FormLayout;
public class fileDES extends JDialog {
private JButton button_4;
/**
* Launch the application
* @param args
*/
private static final boolean enc=true; //加密
private static final boolean dec=false; //解密
private final String passWord="1234ABCD";
private static final fileDES dialog = new fileDES();
String srcFileName;
private String destFileName;
private String dirName;
private String desdirName;
private String filerealname;
private String inKey;
private boolean actionType;
private File srcFile;
private File destFile;
private Des des;
protected mmsSecDilog frame1;
public static fileDES getfileDES()
{
return dialog;
}
private void analyzePath(){
File dir=new File(dirName);
if (!dir.exists()){
System.err.println(dirName+" is not exist");
System.exit(1);
}else if(!dir.isDirectory()){
System.err.println(dirName+" is not a directory");
System.exit(1);
}
dir=new File(desdirName);
if (!dir.exists()){
if(!dir.mkdirs()){
System.out.println ("can not creat directory:"+desdirName);
System.exit(1);
}
}else if(!dir.isDirectory()){
System.err.println(desdirName+" is not a directory");
System.exit(1);
}
}
private static int replenish(FileChannel channel,ByteBuffer buf) throws IOException{
long byteLeft=channel.size()-channel.position();
if(byteLeft==0L)
return -1;
buf.position(0);
buf.limit(buf.position()+(byteLeft<8 ? (int)byteLeft :8));
return channel.read(buf);
}
private void file_operate(boolean flag){
des=new Des(inKey);
FileOutputStream outputFile=null;
try {
outputFile=new FileOutputStream(srcFile,true);
}catch (java.io.FileNotFoundException e) {
e.printStackTrace(System.err);
}
FileChannel outChannel=outputFile.getChannel();
try{
if(outChannel.size()%2!=0){
ByteBuffer bufTemp=ByteBuffer.allocate(1);
bufTemp.put((byte)32);
bufTemp.flip();
outChannel.position(outChannel.size());
outChannel.write(bufTemp);
bufTemp.clear();
}
}catch(Exception ex){
ex.printStackTrace(System.err);
System.exit(1);
}
FileInputStream inFile=null;
try{
inFile=new FileInputStream(srcFile);
}catch(java.io.FileNotFoundException e){
e.printStackTrace(System.err);
//System.exit(1);
}
outputFile=null;
try {
outputFile=new FileOutputStream(destFile,true);
}catch (java.io.FileNotFoundException e) {
e.printStackTrace(System.err);
}
FileChannel inChannel=inFile.getChannel();
outChannel=outputFile.getChannel();
ByteBuffer inBuf=ByteBuffer.allocate(8);
ByteBuffer outBuf=ByteBuffer.allocate(8);
try{
String srcStr;
String destStr;
while(true){
if (replenish(inChannel,inBuf)==-1) break;
srcStr=((ByteBuffer)(inBuf.flip())).asCharBuffer().toString();
inBuf.clear();
if (flag)
destStr=des.enc(srcStr,srcStr.length());
else
destStr=des.dec(srcStr,srcStr.length());
outBuf.clear();
if (destStr.length()==4){
for (int i = 0; i<4; i++) {
outBuf.putChar(destStr.charAt(i));
}
outBuf.flip();
}else{
outBuf.position(0);
outBuf.limit(2*destStr.length());
for (int i = 0; i<destStr.length(); i++) {
outBuf.putChar(destStr.charAt(i));
}
outBuf.flip();
}
try {
outChannel.write(outBuf);
outBuf.clear();
}catch (java.io.IOException ex) {
ex.printStackTrace(System.err);
}
}
System.out.println (inChannel.size());
System.out.println (outChannel.size());
System.out.println ("EoF reached.");
inFile.close();
outputFile.close();
}catch(java.io.IOException e){
e.printStackTrace(System.err);
System.exit(1);
}
}
public void FileDES(String srcFileName,String destFileName,String inKey,boolean actionType){
this.srcFileName=srcFileName;
this.destFileName=destFileName;
this.actionType=actionType;
analyzePath();
srcFile=new File(srcFileName);
destFile=new File(destFileName);
this.inKey=inKey;
if (actionType==enc)
file_operate(enc);
else
file_operate(dec);
}
public static void main(String args[]) {
try {
dialog.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog
*/
public fileDES() {
super();
setSize(500, 300);
getContentPane().setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
final JLabel label = new JLabel();
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("@楷体_GB2312", Font.PLAIN, 20));
label.setLayout(new FormLayout("492px", "341px"));
label.setBounds(new Rectangle(100, 100, 500, 375));
label.setText("DES加密");
getContentPane().add(label, BorderLayout.BEFORE_FIRST_LINE);
final JButton button_1 = new JButton();
button_1.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent arg0) {
mmsSecDilog frame1=new mmsSecDilog(dialog);
frame1.setTitle("请选择要加密的文件");
frame1.setSize(550,350);
frame1.setVisible(true);
//System.out.println(dialog.srcFileName);
}
});
button_1.setFont(new Font("楷体_GB2312", Font.PLAIN, 18));
button_1.setBounds(new Rectangle(100, 100, 500, 375));
button_1.setText("打开加密文件");
getContentPane().add(button_1, BorderLayout.WEST);
final JButton button_2 = new JButton();
button_2.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent arg0) {
System.out.println(dialog.srcFileName);
int pos=srcFileName.lastIndexOf("\\");
dirName=srcFileName.substring(0,pos);
System.out.println(dirName);
filerealname = srcFileName.substring(srcFileName.lastIndexOf("\\") + 1);
System.out.println(filerealname);
StringBuffer dest=new StringBuffer();
dest.append(dirName);
dest.append("\\");
dest.append("DES");
dest.append(filerealname);
destFileName=dest.toString();
System.out.println(destFileName);
pos=destFileName.lastIndexOf("\\");
desdirName=destFileName.substring(0,pos);
FileDES(srcFileName,destFileName,passWord,true);
}
});
button_2.setFont(new Font("楷体_GB2312", Font.PLAIN, 18));
button_2.setText("加密");
getContentPane().add(button_2, BorderLayout.CENTER);
final JButton button_3 = new JButton();
button_3.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent arg0) {
mmsSecDilog frame1=new mmsSecDilog(dialog);
frame1.setTitle("请选择要解密的文件");
frame1.setSize(550,350);
frame1.setVisible(true);
}
});
button_3.setFont(new Font("楷体_GB2312", Font.PLAIN, 18));
button_3.setText("打开解密文件");
getContentPane().add(button_3, BorderLayout.EAST);
final JButton button_4 = new JButton();
button_4.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent arg0) {
System.out.println(dialog.srcFileName);
int pos=srcFileName.lastIndexOf("\\");
dirName=srcFileName.substring(0,pos);
System.out.println(dirName);
System.out.println(desdirName);
filerealname = srcFileName.substring(srcFileName.lastIndexOf("\\") + 1);
System.out.println(filerealname);
StringBuffer dest=new StringBuffer();
dest.append(dirName);
dest.append("\\");
dest.append("ENY");
dest.append(filerealname);
destFileName=dest.toString();
pos=destFileName.lastIndexOf("\\");
desdirName=destFileName.substring(0,pos);
FileDES(srcFileName,destFileName,passWord,false);
}
});
button_4.setFont(new Font("楷体_GB2312", Font.PLAIN, 18));
button_4.setText("解密");
getContentPane().add(button_4, BorderLayout.SOUTH);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -