拖拽实现DES加密解密本地文件

  //加密文件部分
1
import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.OutputStream; 7 import java.security.InvalidKeyException; 8 import java.security.Key; 9 import java.security.NoSuchAlgorithmException; 10 import java.security.SecureRandom; 11 12 import javax.crypto.Cipher; 13 import javax.crypto.CipherInputStream; 14 import javax.crypto.CipherOutputStream; 15 import javax.crypto.IllegalBlockSizeException; 16 import javax.crypto.KeyGenerator; 17 import javax.crypto.NoSuchPaddingException; 18 19 public class encryption_File { 20 Key key; 21 static String path_en; 22 static String path_de; 23 public encryption_File(String str) throws NoSuchAlgorithmException{ 24 getKey(str); 25 } 26 27 public void getKey(String strKey) throws NoSuchAlgorithmException{ 28 KeyGenerator generator = KeyGenerator.getInstance("DES"); 29 generator.init(new SecureRandom(strKey.getBytes())); 30 this.key = generator.generateKey(); 31 //byte[] encoded = key.getEncoded(); 32 //String encodeHexString = encoded.toString(); 33 34 System.out.println(this.key.getFormat()); 35 36 generator = null; 37 } 38 39 public void encrypt(String file,String destFile) throws NoSuchAlgorithmException, NoSuchPaddingException, IOException, InvalidKeyException, IllegalBlockSizeException{ 40 Cipher cipher = Cipher.getInstance("DES"); 41 try { 42 cipher.init(Cipher.ENCRYPT_MODE, this.key); 43 //byte[] b = cipher.wrap(this.key); //包装密钥 44 InputStream is = new FileInputStream(file); 45 OutputStream os = new FileOutputStream(destFile); 46 CipherInputStream cis = new CipherInputStream(is, cipher); 47 //byte[] b = cipher.wrap(key); //包装密钥 48 //System.out.println(b); 49 byte[] buffer = new byte[1024]; 50 int r; 51 while((r=cis.read(buffer))>0){ 52 os.write(buffer, 0, r); 53 } 54 is.close(); 55 os.close(); 56 cis.close(); 57 //byte[] b = cipher.wrap(key); //包装密钥 58 } catch (InvalidKeyException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } catch (FileNotFoundException e) { 62 // TODO Auto-generated catch block 63 e.printStackTrace(); 64 } 65 66 } 67 public void decrypt(String file,String destFile) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException{ 68 Cipher cipher = Cipher.getInstance("DES"); 69 cipher.init(Cipher.DECRYPT_MODE, this.key); 70 InputStream is = new FileInputStream(file); 71 OutputStream os = new FileOutputStream(destFile); 72 CipherOutputStream cos = new CipherOutputStream(os, cipher); 73 byte[] buffer = new byte[1024]; 74 int r; 75 while((r=is.read(buffer))>=0){ 76 cos.write(buffer,0,r); 77 } 78 cos.close(); 79 is.close(); 80 os.close(); 81 } 82 }
   //拖拽窗口部分
1
import java.awt.BorderLayout; 2 import java.awt.Color; 3 import java.awt.datatransfer.DataFlavor; 4 import java.awt.datatransfer.UnsupportedFlavorException; 5 import java.awt.dnd.DnDConstants; 6 import java.awt.dnd.DropTarget; 7 import java.awt.dnd.DropTargetAdapter; 8 import java.awt.dnd.DropTargetDropEvent; 9 import java.io.File; 10 import java.io.IOException; 11 import java.security.InvalidKeyException; 12 import java.security.NoSuchAlgorithmException; 13 import java.util.List; 14 15 import javax.crypto.IllegalBlockSizeException; 16 import javax.crypto.NoSuchPaddingException; 17 import javax.swing.JFrame; 18 import javax.swing.JOptionPane; 19 import javax.swing.JPanel; 20 import javax.swing.JToggleButton; 21 22 public class DragFile extends JFrame{ 23 JPanel panel; 24 //JButton b1; 25 JToggleButton b; 26 static List<File> list; 27 static String path_De; 28 public DragFile(){ 29 panel = new JPanel(); 30 panel.setBackground(Color.PINK); 31 b = new JToggleButton("点击选择加密",false); 32 getContentPane().add(panel, BorderLayout.CENTER); 33 getContentPane().add(b,BorderLayout.EAST); 34 setSize(500,500); 35 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 36 setLocation(400, 400); 37 setTitle("请将要加密的文件拖拽至框内!"); 38 setVisible(true); 39 drag(); 40 } 41 42 public void drag(){ 43 new DropTarget(panel,DnDConstants.ACTION_COPY_OR_MOVE,new DropTargetAdapter() { 44 45 @Override 46 public void drop(DropTargetDropEvent dtde) { 47 if(dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)){ 48 dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 49 try { 50 encryption_File encry = new encryption_File("aaa");//投入种子 51 list = (List<File>) (dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor)); 52 String temp = ""; 53 for(File file:list){ 54 temp += file.getAbsolutePath()+"\n"; 55 if(b.isSelected()){ 56 //对拖拽进框内的内容加密 57 encry.encrypt(file.getAbsolutePath(), file.getParent()+File.separator+Caesar.caesar_En(file.getName())); 58 JOptionPane.showMessageDialog(null, "已成功加密:"+temp); 59 }else{ 60 encry.decrypt(file.getAbsolutePath(), file.getParent()+File.separator+Caesar.caesar_De(file.getName())); 61 JOptionPane.showMessageDialog(null, "已成功解密:"+temp); 62 } 63 64 if(file.isFile()&&file.exists()){ 65 file.delete(); 66 } 67 } 68 dtde.dropComplete(true); 69 } catch (UnsupportedFlavorException | IOException e) { 70 // TODO Auto-generated catch block 71 e.printStackTrace(); 72 } catch (NoSuchAlgorithmException e) { 73 // TODO Auto-generated catch block 74 e.printStackTrace(); 75 } catch (NoSuchPaddingException e) { 76 // TODO Auto-generated catch block 77 e.printStackTrace(); 78 } catch (InvalidKeyException e) { 79 // TODO Auto-generated catch block 80 e.printStackTrace(); 81 } catch (IllegalBlockSizeException e) { 82 // TODO Auto-generated catch block 83 e.printStackTrace(); 84 } 85 86 87 }else{ 88 dtde.rejectDrop(); 89 } 90 91 } 92 }); 93 } 94 95 public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, IOException { 96 new DragFile(); 97 } 98 }

以下为效果图:

猜你喜欢

转载自www.cnblogs.com/blzm742624643/p/9357498.html