简单视频加密与授权

来源:互联网 发布:怎么一起网络看电影 编辑:程序博客网 时间:2024/04/30 23:28

应同学邀请参加信息安全大赛,负责作品的"离线空间"模块,简单来说,离线空间就是要求加密视频只能在指定机器播放,并且只能播放规定次数。

思维导图如下


运行环境:JDK1.8, WIN8


一、加密视频EncVideo部分由以下几个模块组成:

1、Mac.Java 

负责获取本地Mac

[java] view plain copy
  1. //获取本机MAC地址  
  2. //提供一个方法 String getMac()  
  3. //返回本机Mac地址  
  4. package org.tree.enc;  
  5.   
  6. import java.io.BufferedReader;  
  7. import java.io.IOException;  
  8. import java.io.InputStreamReader;  
  9.   
  10. public class Mac {  
  11.     public String getMac() throws IOException  
  12.     {  
  13.         String MacAddress = null;  
  14.         BufferedReader br = null;  
  15.         Process pro = null;  
  16.         try{  
  17.             pro = Runtime.getRuntime().exec("ipconfig /all");  
  18.             br = new BufferedReader(new InputStreamReader(pro.getInputStream()));  
  19.             int index = -1;  
  20.             String line = null;  
  21.             while((line = br.readLine()) != null){  
  22.                 String t = "物理地址";  
  23.                 String que = new String(t.getBytes("GBK"));  
  24.                 if(((index = line.indexOf(que)) != -1) || ((index = line.toLowerCase().indexOf("physical address")) != -1)){  
  25.                     index = line.indexOf(":");  
  26.                     MacAddress = line.substring(index + 1).trim();  
  27.                     break;  
  28.                 }  
  29.             }  
  30.         }  
  31.         catch(Exception e){  
  32.             e.printStackTrace();  
  33.         }  
  34.         finally{  
  35.             br.close();  
  36.         }  
  37.         return MacAddress;  
  38.     }  
  39. }  


2、MD5.java 

负责将Mac进行MD5加密

[java] view plain copy
  1. //对MAC地址进行MD5加密,生成32位字符串  
  2. //提供一个方法String getMD5()  
  3. //返回经过MD5加密后的MAC  
  4. package org.tree.enc;  
  5.   
  6. import java.security.MessageDigest;  
  7.   
  8. public class MD5 {  
  9.     public String getMD5(){  
  10.         String RawMessage = null;  
  11.         String MD5Message = null;  
  12.         try{  
  13.             RawMessage = new Mac().getMac();;  
  14.             MessageDigest md = MessageDigest.getInstance("MD5");  
  15.             md.update(RawMessage.getBytes());  
  16.             byte[] b = md.digest();  
  17.             StringBuffer temp = new StringBuffer("");  
  18.             for(int i = 0 ; i < b.length; i++){  
  19.                 int tep = b[i];  
  20.                 if(tep < 0){  
  21.                     tep += 256;  
  22.                 }  
  23.                 if(tep < 16){  
  24.                     temp.append("0");  
  25.                 }  
  26.                 temp.append(Integer.toHexString(tep));  
  27.             }  
  28.             MD5Message = temp.toString();  
  29.         }  
  30.         catch(Exception e){  
  31.             e.printStackTrace();  
  32.         }  
  33.         return MD5Message;  
  34.     }  
  35. }  


3、EncVideo.java 

负责对视频进行加密

[java] view plain copy
  1. //加密视频,在视频末尾添加MAC的MD5,生成Result.k文件  
  2. //提供一个方法 void encVideo(String InPath, String OutPath)  
  3. //InPath:需要加密的文件路径 OutPath:输出文件路径  
  4. package org.tree.enc;  
  5.   
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10.   
  11.   
  12. public class EncVideo {  
  13.     public void encVideo(String InPath, String OutPath) throws IOException  
  14.     {  
  15.         FileInputStream in  = null;  
  16.         FileOutputStream out = null;  
  17.         try{  
  18.             in = new FileInputStream(InPath);     
  19.             out = new FileOutputStream(new File(OutPath+"\\Result.k"));  
  20.             byte[] b = new byte[1024];  
  21.             int temp = 0;  
  22.             while((temp = in.read(b)) != -1){  
  23.                 out.write(b);  
  24.             }  
  25.             out.write(new MD5().getMD5().getBytes());  
  26.         }  
  27.         catch(Exception e){  
  28.             e.printStackTrace();  
  29.         }  
  30.         finally{  
  31.             if(in != null){  
  32.                 in.close();  
  33.             }  
  34.             if(out != null){  
  35.                 out.close();  
  36.             }  
  37.         }  
  38.     }  
  39. }  


4、UI.java

负责GUI部分

[java] view plain copy
  1. package org.tree.enc;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.io.File;  
  5.   
  6.   
  7.   
  8. import javax.swing.JButton;  
  9. import javax.swing.JDialog;  
  10. import javax.swing.JFileChooser;  
  11. import javax.swing.JFrame;  
  12. import javax.swing.JLabel;  
  13. import javax.swing.JPanel;  
  14.   
  15. public class UI {  
  16.     private String InPath = null;  
  17.     private String OutPath = null;  
  18.       
  19.     public UI(){  
  20.         JFrame f = new JFrame("加密视频");  
  21.         f.setSize(200150);  
  22.         f.setResizable(false);  
  23.         JPanel p1 = new JPanel();  
  24.         JPanel p2 = new JPanel();  
  25.         JPanel p3 = new JPanel();  
  26.         JFileChooser fc1 = new JFileChooser();  
  27.         JButton b1 = new JButton("选择加密文件");  
  28.         JFileChooser fc2 = new JFileChooser();  
  29.         JButton b2 = new JButton("选择保存位置");  
  30.         JButton b3 = new JButton("加密");  
  31.         p1.add(b1);  
  32.         p2.add(b2);  
  33.         p3.add(b3);  
  34.         f.add(p1, BorderLayout.NORTH);  
  35.         f.add(p2, BorderLayout.CENTER);  
  36.         f.add(p3, BorderLayout.SOUTH);  
  37.         f.setVisible(true);  
  38.           
  39.         b1.addActionListener(event -> {  
  40.             fc1.setDialogTitle("请选择需要加密的文件");  
  41.             fc1.showOpenDialog(b1);  
  42.         });  
  43.           
  44.         b2.addActionListener(event -> {  
  45.             fc2.setDialogTitle("请选择要保存的位置");  
  46.             fc2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  
  47.             fc2.showSaveDialog(b2);  
  48.         });  
  49.           
  50.         fc1.addPropertyChangeListener(event -> {  
  51.             if(event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY){  
  52.                 File file = (File)event.getNewValue();  
  53.                 InPath = file.getPath();  
  54.             }  
  55.         });  
  56.           
  57.         fc2.addPropertyChangeListener(event -> {  
  58.             if(event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY){  
  59.                 File file = (File)event.getNewValue();  
  60.                 OutPath = file.getPath();  
  61.             }  
  62.         });  
  63.           
  64.         b3.addActionListener(event -> {  
  65.             try{  
  66.                 if(InPath == null){  
  67.                     JDialog d = new JDialog(f, "您没有选择需要加密的文件!"true);  
  68.                     JLabel la = new JLabel("请选择需要加密的文件!", JLabel.CENTER);  
  69.                     d.add(la, BorderLayout.CENTER);  
  70.                     d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  71.                     d.setSize(300100);  
  72.                     d.setVisible(true);  
  73.                 }  
  74.                   
  75.                 else if(OutPath == null){  
  76.                     JDialog d = new JDialog(f, "您没有选择保存的位置!"true);  
  77.                     JLabel la = new JLabel("请选择需要保存的位置!", JLabel.CENTER);  
  78.                     d.add(la, BorderLayout.CENTER);  
  79.                     d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  80.                     d.setSize(300100);  
  81.                     d.setVisible(true);  
  82.                 }  
  83.                 else{  
  84.                     new EncVideo().encVideo(InPath, OutPath);  
  85.                     JDialog d = new JDialog(f, "加密完毕!"false);  
  86.                     JLabel la = new JLabel("加密完毕!", JLabel.CENTER);  
  87.                     d.add(la, BorderLayout.CENTER);  
  88.                     d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  89.                     d.setSize(300100);  
  90.                     d.setVisible(true);  
  91.                 }  
  92.             }  
  93.             catch(Exception e){  
  94.                 e.printStackTrace();  
  95.             }  
  96.         });  
  97.           
  98.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  99.     }  
  100.       
  101.     public static void main(String[] args){  
  102.         new UI();  
  103.     }  
  104. }  

运行效果(以test.mp4为例):

1、程序主界面


2、程序运行结果




二、解密视频DecVideo由以下几个模块组成:

1、Mac.java

获取MAC 同上

2、MD5.java

对MAC进行MD5加密 同上

3、DecVideo.java

负责对提取视频中的MD5

[java] view plain copy
  1. //提取视频中MAC  
  2. //提供一个方法 String getMacMD5(String path)  
  3. //返回视频中的MAC path:要打开的加密文件的路径  
  4. package org.tree.pla;  
  5.   
  6. import java.io.RandomAccessFile;  
  7.   
  8. public class DecVideo {  
  9.     public String getMacMD5(String path){  
  10.         String VideoMD5 = null;  
  11.         try(RandomAccessFile raf = new RandomAccessFile(path, "r")){  
  12.             long len = raf.length();  
  13.             raf.seek(len-32);  
  14.             byte[] b = new byte[32];  
  15.             raf.read(b);  
  16.             VideoMD5 = new String(b);  
  17.         }  
  18.         catch(Exception e){  
  19.             e.printStackTrace();  
  20.         }  
  21.         return VideoMD5;  
  22.     }  
  23. }  
4、PlayVideo.java

负责调用本地播放器播放视频

[java] view plain copy
  1. //播放视频  
  2. //提供两个方法 boolean CompareMD5(String path)  
  3. //比较视频中的MAC与本地MAC  
  4. //void Player(Sting path)  
  5. //播放视频  
  6. //提供一个类变量 int count 用来标识剩余授权次数  
  7. package org.tree.pla;  
  8.   
  9. import java.io.File;  
  10. import java.io.FileInputStream;  
  11. import java.io.FileOutputStream;  
  12. import java.io.RandomAccessFile;  
  13.   
  14. public class PlayVideo {  
  15.     public static int count = 10;  
  16.     public boolean CompareMD5(String path){  
  17.         String VideoMD5 = new DecVideo().getMacMD5(path);  
  18.         String LocalMD5 = new MD5().getMD5();  
  19.         if(VideoMD5.equals(LocalMD5)){  
  20.             return true;  
  21.         }  
  22.         else{  
  23.             return false;  
  24.         }  
  25.     }  
  26.       
  27.     public void Player(String path){  
  28.         if(new PlayVideo().CompareMD5(path)){  
  29.             try{  
  30.                 FileInputStream in = new FileInputStream(path);  
  31.                 FileOutputStream out = new FileOutputStream("C:\\tmp\\isctmp.avi");  
  32.                 byte[] tmpb = new byte[1024];  
  33.                 int index = 0;  
  34.                 while((index = in.read(tmpb)) != -1){  
  35.                     out.write(tmpb);  
  36.                 }  
  37.                 in.close();  
  38.                 out.close();  
  39.                   
  40.                 File f = new File("C:\\Users\\Public\\Documents\\IscInfo.ini");  
  41.                 if(f.exists()){  
  42.                     RandomAccessFile raf = new RandomAccessFile(f, "rw");  
  43.                     byte[] b = new byte[4];  
  44.                     raf.read(b);  
  45.                     String result = new String(b);  
  46.                     int num = Integer.parseInt(result.trim());  
  47.                     if(num != 0 && num <= 10){  
  48.                         raf.setLength(0);  
  49.                         num--;  
  50.                         PlayVideo.count = num;  
  51.                         String numtmp = num + "";  
  52.                         raf.write(numtmp.getBytes());  
  53.                           
  54.                         Process pro = Runtime.getRuntime().exec("C:\\Program Files\\Windows Media Player\\wmplayer.exe " + "C:\\tmp\\isctmp.avi");  
  55.                     }  
  56.                     else{  
  57.                         File inf = new File(path);  
  58.                         inf.delete();  
  59.                     }  
  60.                     raf.close();  
  61.                 }  
  62.                 else{  
  63.                     PlayVideo.count = 10;  
  64.                     FileOutputStream out1 = new FileOutputStream(f.getPath());  
  65.                     out1.write("10".getBytes());  
  66.                     Process pro = Runtime.getRuntime().exec("C:\\Program Files\\Windows Media Player\\wmplayer.exe " + "C:\\tmp\\isctmp.avi");  
  67.                     out1.close();  
  68.                 }  
  69.             }  
  70.             catch(Exception e){  
  71.                 e.printStackTrace();  
  72.             }  
  73.         }  
  74.         else{  
  75.             System.out.println("你不能播放这个视频");  
  76.         }  
  77.     }  
  78. }  
6、UI.java

负责GUI,验证授权信息

[java] view plain copy
  1. package org.tree.pla;  
  2.   
  3. import java.awt.BorderLayout;  
  4.   
  5.   
  6. import java.awt.event.WindowAdapter;  
  7. import java.awt.event.WindowEvent;  
  8. import java.io.File;  
  9.   
  10. import javax.swing.JButton;  
  11. import javax.swing.JDialog;  
  12. import javax.swing.JFileChooser;  
  13. import javax.swing.JFrame;  
  14. import javax.swing.JLabel;  
  15.   
  16. public class UI {  
  17.     private String path = null;  
  18.     public UI(){  
  19.         JFrame f = new JFrame("播放加密视频");  
  20.         f.setSize(250100);  
  21.         f.setResizable(false);  
  22.         JButton b1 = new JButton("选择加密文件");  
  23.         JButton b2 = new JButton("播放");  
  24.         JFileChooser fc = new JFileChooser();  
  25.         f.add(b1, BorderLayout.CENTER);  
  26.         f.add(b2, BorderLayout.SOUTH);  
  27.         f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
  28.         f.setVisible(true);  
  29.           
  30.         b1.addActionListener(event -> {  
  31.             fc.setDialogTitle("请选择需要打开的文件");  
  32.             fc.showOpenDialog(b1);  
  33.         });  
  34.           
  35.         b2.addActionListener(event -> {  
  36.             if(path == null){  
  37.                 JDialog d = new JDialog(f, "您没有选择打开的文件!"true);  
  38.                 JLabel la = new JLabel("请选择需要打开的文件!", JLabel.CENTER);  
  39.                 d.add(la, BorderLayout.CENTER);  
  40.                 d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  41.                 d.setSize(300100);  
  42.                 d.setVisible(true);  
  43.             }  
  44.             if(new PlayVideo().CompareMD5(path)){  
  45.                 if(PlayVideo.count <= 0){  
  46.                     JDialog d = new JDialog(f, "权限不足"true);  
  47.                     JLabel la = new JLabel("您的剩余授权次数不足!", JLabel.CENTER);  
  48.                     d.add(la, BorderLayout.CENTER);  
  49.                     d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  50.                     d.setSize(300100);  
  51.                     d.setVisible(true);  
  52.                     File file = new File(path);  
  53.                     file.delete();  
  54.                 }  
  55.                 else{  
  56.                     new PlayVideo().Player(path);  
  57.                     JDialog d = new JDialog(f, "剩余使用次数");  
  58.                     JLabel la = new JLabel("剩余使用次数为: " + PlayVideo.count, JLabel.CENTER);  
  59.                     d.add(la, BorderLayout.CENTER);  
  60.                     d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  61.                     d.setSize(300100);  
  62.                     d.setVisible(true);  
  63.                 }  
  64.             }  
  65.             else{  
  66.                 JDialog d = new JDialog(f, "权限不足"true);  
  67.                 JLabel la = new JLabel("您没有别授权打开此文件!", JLabel.CENTER);  
  68.                 d.add(la, BorderLayout.CENTER);  
  69.                 d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);  
  70.                 d.setSize(300100);  
  71.                 d.setVisible(true);  
  72.                 File file = new File(path);  
  73.                 file.delete();  
  74.             }  
  75.         });  
  76.           
  77.         fc.addPropertyChangeListener(event -> {  
  78.             if(event.getPropertyName() == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY){  
  79.                 File file = (File)event.getNewValue();  
  80.                 path = file.getPath();  
  81.             }  
  82.         });  
  83.       
  84.         f.addWindowListener(new WindowAdapter() {  
  85.             public void windowClosing(WindowEvent e) {  
  86.                 try{  
  87.                     File file = new File("C:\\tmp\\isctmp.avi");  
  88.                     file.delete();  
  89.                 }  
  90.                 catch(Exception ex){  
  91.                     ex.printStackTrace();  
  92.                 }  
  93.                 System.exit(0);  
  94.             }  
  95.         });  
  96.     }  
  97.   
  98.   
  99.     public static void main(String[] args){  
  100.         new UI();  
  101.     }  
  102. }  

程序运行结果:

1、程序主界面


2、程序运行界面



小结

简单的视频加密,不过现在的加密非常的脆弱,比如将.k改名为.avi等视频格式就可以正常播放,修改授权信息内容就可以无限次播放,将隐式转换成视频格式的文件复制一份也可以正常播放。要对抗这种方式的攻击就需要用到其他加密方法,这就不在本文讨论范围了QAQ(其实我会说我也不懂吗。。。我只负责这个模块,听说还有左右手识别,吧唧吧唧的什么的加密方法Orz)

PS.忽略我写的渣的JAVA代码。。新手...IO方面各种流乱用 还有GUI那写的更不能看了QAQ


==================================================================

IscInfo.ini文件非常容易被修改,所以想出了一个解决办法


在将.k文件转化为.avi格式时,文件流是打开的,此时我们可以修改.k文件尾部的授权次数信息,每次打开视频前检测剩余几次授权,这样就不需要IscInfo.ini这个文件了


加密部分

1、MAC.Java不变,获取本地MAC地址

2、MD5.java 略微修改,使之可以加密所有传入的字符串,比如授权次数

[java] view plain copy
  1. //对MAC地址进行MD5加密,生成32位字符串  
  2. //提供一个方法String getMD5()  
  3. //返回经过MD5加密后的MAC  
  4. package org.tree.enc;  
  5.   
  6. import java.security.MessageDigest;  
  7.   
  8. public class MD5 {  
  9.     //能够加密所有传入的字符串  
  10.     public String getMD5(String mac){  
  11.         String RawMessage = null;  
  12.         String MD5Message = null;  
  13.         try{  
  14.             RawMessage = mac;  
  15.             MessageDigest md = MessageDigest.getInstance("MD5");  
  16.             md.update(RawMessage.getBytes());  
  17.             byte[] b = md.digest();  
  18.             StringBuffer temp = new StringBuffer("");  
  19.             for(int i = 0 ; i < b.length; i++){  
  20.                 int tep = b[i];  
  21.                 if(tep < 0){  
  22.                     tep += 256;  
  23.                 }  
  24.                 if(tep < 16){  
  25.                     temp.append("0");  
  26.                 }  
  27.                 temp.append(Integer.toHexString(tep));  
  28.             }  
  29.             MD5Message = temp.toString();  
  30.         }  
  31.         catch(Exception e){  
  32.             e.printStackTrace();  
  33.         }  
  34.         return MD5Message;  
  35.     }  
  36. }  
3、EncVideo.java

将MAC添加进视频尾部后,再加入授权次数,使后64位都是加密信息

[java] view plain copy
  1. //加密视频,在视频末尾添加MAC的MD5,生成Result.k文件  
  2. //提供一个方法 void encVideo(String InPath, String OutPath)  
  3. //InPath:需要加密的文件路径 OutPath:输出文件路径  
  4. package org.tree.enc;  
  5.   
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10.   
  11.   
  12. public class EncVideo {  
  13.     public void encVideo(String InPath, String OutPath) throws IOException  
  14.     {  
  15.         FileInputStream in  = null;  
  16.         FileOutputStream out = null;  
  17.         try{  
  18.             in = new FileInputStream(InPath);     
  19.             out = new FileOutputStream(new File(OutPath+"\\Result.k"));  
  20.             byte[] b = new byte[1024];  
  21.             int temp = 0;  
  22.             while((temp = in.read(b)) != -1){  
  23.                 out.write(b);  
  24.             }  
  25.             out.write(new MD5().getMD5(new Mac().getMac()).getBytes());  
  26.             //在尾部添加进授权信息  
  27.             out.write(new MD5().getMD5("10").getBytes());  
  28.         }  
  29.         catch(Exception e){  
  30.             e.printStackTrace();  
  31.         }  
  32.         finally{  
  33.             if(in != null){  
  34.                 in.close();  
  35.             }  
  36.             if(out != null){  
  37.                 out.close();  
  38.             }  
  39.         }  
  40.     }  
  41. }  

4、UI.java GUI部分不变


解密部分:

1、MAC.java

同加密部分

2、MD5.java

同加密部分 使之加密所有字符串

3、DecVideo.java 

不仅提取出视频尾部MAC,也提取出授权次数

[java] view plain copy
  1. //提取视频中MAC   
  2. //提供一个方法 String getMacMD5(String path)  
  3. //返回视频中的MAC path:要打开的加密文件的路径  
  4. //返回视频尾部的授权次数  
  5. package org.tree.pla;  
  6.   
  7. import java.io.RandomAccessFile;  
  8.   
  9. public class DecVideo {  
  10.     //提取视频内Mac信息  
  11.     public String getMacMD5(String path){  
  12.         String VideoMD5 = null;  
  13.         try(RandomAccessFile raf = new RandomAccessFile(path, "r")){  
  14.             long len = raf.length();  
  15.             raf.seek(len-64);  
  16.             byte[] b = new byte[32];  
  17.             raf.read(b);  
  18.             VideoMD5 = new String(b);  
  19.         }  
  20.         catch(Exception e){  
  21.             e.printStackTrace();  
  22.         }  
  23.         return VideoMD5;  
  24.     }  
  25.       
  26.     //提取视频内授权次数信息  
  27.     public String getVideoCount(String path){  
  28.         String VideoCount = null;  
  29.         try(RandomAccessFile raf = new RandomAccessFile(path, "r")){  
  30.             long len = raf.length();  
  31.             raf.seek(len-32);  
  32.             byte[] b = new byte[32];  
  33.             raf.read(b);  
  34.             VideoCount = new String (b);  
  35.         }  
  36.         catch(Exception e){  
  37.             e.printStackTrace();  
  38.         }  
  39.         return VideoCount;  
  40.     }  
  41. }  
4、PlayVideo.java

不仅比较MAC, 还要比较剩余授权次数,且如果满足播放条件 ,剩余授权次数-1重新写入视频尾部

[java] view plain copy
  1. //播放视频  
  2. //提供两个方法 boolean CompareMD5(String path)  
  3. //比较视频中的MAC与本地MAC  
  4. //void Player(Sting path)  
  5. //播放视频  
  6. //提供一个类变量 int count 用来标识剩余授权次数  
  7. package org.tree.pla;  
  8.   
  9. import java.io.FileInputStream;  
  10. import java.io.FileOutputStream;  
  11. import java.io.RandomAccessFile;  
  12. import java.util.ArrayList;  
  13.   
  14. public class PlayVideo {  
  15.     //存储MD5加密后的0-10数字用于比较剩余授权次数  
  16.     public static ArrayList<String> ComList = new ArrayList<>();  
  17.       
  18.     public static void init(){  
  19.         for(int i = 0 ; i <= 10 ; i ++){  
  20.             ComList.add(new MD5().getMD5(i+""));  
  21.         }  
  22.     }  
  23.     //比较视频中MAC与本地MAC  
  24.     public boolean CompareMacMD5(String path){  
  25.         String VideoMD5 = new DecVideo().getMacMD5(path);  
  26.         String localMD5 = null;  
  27.         try{  
  28.             localMD5 = new MD5().getMD5(new Mac().getMac());  
  29.         }  
  30.         catch(Exception e){  
  31.             e.printStackTrace();  
  32.         }  
  33.           
  34.         if(VideoMD5.equals(localMD5)){  
  35.             return true;  
  36.         }  
  37.         else{  
  38.             return false;  
  39.         }  
  40.     }  
  41.       
  42.     //获取剩余授权次数  
  43.     public int CompareCountMD5(String path){  
  44.         String VideoCount = new DecVideo().getVideoCount(path);  
  45.         return ComList.indexOf(VideoCount);  
  46.     }  
  47.       
  48.       
  49.     public void Player(String path){  
  50.         if(new PlayVideo().CompareMacMD5(path)){  
  51.             try{  
  52.                 FileInputStream in = new FileInputStream(path);  
  53.                 FileOutputStream out = new FileOutputStream("C:\\tmp\\isctmp.avi");  
  54.                 byte[] tmpb = new byte[1024];  
  55.                 int index = 0;  
  56.                 while((index = in.read(tmpb)) != -1){  
  57.                     out.write(tmpb);  
  58.                 }  
  59.                 in.close();  
  60.                 out.close();  
  61.                 int count = new PlayVideo().CompareCountMD5(path);  
  62.                 //如果剩余授权次数>0 播放且次数-1重新写入视频尾部  
  63.                 if(count > 0){  
  64.                     RandomAccessFile raf = new RandomAccessFile(path, "rw");  
  65.                     long len = raf.length();  
  66.                     raf.seek(len-32);  
  67.                     String result = count - 1 + "";  
  68.                     result = new MD5().getMD5(result);  
  69.                     raf.write(result.getBytes());  
  70.                     Process pro = Runtime.getRuntime().exec("C:\\Program Files\\Windows Media Player\\wmplayer.exe " + "C:\\tmp\\isctmp.avi");  
  71.                     raf.close();  
  72.                 }  
  73.             }  
  74.             catch(Exception e){  
  75.                 e.printStackTrace();  
  76.             }  
  77.         }  
  78.     }  
  79. }  


5、UI.java

基本不变 负责GUI

小结

通过上面的方法,一定程度上提高了安全性



原文链接:http://blog.csdn.net/acm_yuuji/article/details/43822229