找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3098|回复: 0
收起左侧

基于RSA和LSB的图像隐藏技术 附Java源程序

[复制链接]
ID:338478 发表于 2019-7-23 18:50 | 显示全部楼层 |阅读模式
一、项目背景
随着Internet技术和多媒体信息技术的飞速发展,多媒体、计算机网络、个人移动通信技术等进入寻常百姓家,数字化已深入人心。数字多媒体信息在网上传播与传输越来越方便,通过网络传递各种信息越来越普遍。但与此同时也带来了信息安全的隐患问题。信息隐藏是近年来信息安全和多媒体信号处理领域中提出的一种解决媒体信息安全的新方法。它通过把秘密信息隐藏在可公开的媒体信息里,达到证实该媒体信息的数据完整性或传递秘密信息的目的,从而为数字信息的安全问题提供了一种新的解决方法。
二、软件原理及内容
1、原理
LSB(LeastSignificantBits)算法:将秘密信息嵌入到载体图像像素值的最低有效位,也称最不显著位,改变这一位置对载体图像的品质影响最小。
LSB算法基本原理:对空域的LSB做替换,用来替换LSB的序列就是需要加入的水印信息、水印的数字摘要或者由水印生成的伪随机序列。由于水印信息嵌入的位置是LSB,为了满足水印的不可见性,允许嵌入的水印强度不可能太高。然而针对空域的各种处理,如游程编码前的预处理,会对不显著分量进行一定的压缩,所以LSB算法对这些操作很敏感。因此LSB算法最初是用于脆弱性水印的。
2、内容
(1)编写程序,能够将至少20个汉字隐藏到一幅24位的bmp格式的图片中。
(2)用RSA算法将需要隐藏的信息进行加密,然后再隐藏。
(3)能使用密钥将隐藏信息解密提取出来。
三、实现步骤
1、找一幅bmp的24位图片。
2、通过网络学习RSA算法和LSB算法的相关原理和操作。
3、理清程序的运行流程设计好页面。
4、在NetBeansIDE编写代码实现相关功能。
5、调试程序并进行优化。
四、软件功能及分析
1、工程结构图。

1.png


图1 工程结构
分析说明:
图1中PrivateKey为私钥封装类,Publickey为公钥封装类,RSAGeneratorKey为生成密钥对的类,RSAUtil为含有加密和解密方法的封装类,RsaKeyPair为密钥对封装类,_LSB_BMP为包含信息加密解密等方法的主类。
2、程序运行初始界面。
2.png
图2 初始界面
分析说明:
图2显示的界面由标题栏、菜单栏、图片显示区和文本输入显示区构成。其中菜单栏中有文件、LSB和加密隐藏三个选项,“文件”选项主要用于打开已有的未加密图片和加密图片及保存隐藏信息后的图片,打开和保存的图片均为bmp格式;“LSB”主要用于图片的非加密隐藏和直接显示隐藏信息;“加密隐藏”选项主要用于将所需的隐藏信息进行RSA加密及将加密后的信息通过私钥进行解密。
3、点击菜单栏中“文件”的“打开”选项,可选择未加密信息的图片或者含隐藏信息的图片;若点击“保存”选项,则可以将含隐藏信息的图片进行保存。
3.png
图3 选择图片路径
4.png
图4 打开未隐藏信息的图片
5.png
图5 打开隐藏信息的图片
6.png
图6 保存隐藏信息的图片
分析说明:
图3中显示的界面为选择未隐藏信息的图片或者隐藏了信息的图片,图4为打开未隐藏信息的图片后所出现的界面,且在文本输入显示区中显示了可以嵌入多少字节的信息到图片中;图5为打开含隐藏信息的图片后的界面,可以看出含隐藏信息的图片和原图相比,凭肉眼是看不出区别的,但通过程序检测却能发现其中已经隐藏了一定的信息;图6为将含隐藏的图片进行保存,若保存成功会在文本输入显示区显示“保存成功!”。
4、点击菜单栏中的“加密隐藏”中的“隐藏”及“显示”。
7.png
图7 使用RSA对隐藏信息加密及保存私钥
8.png
图8 使用RSA私钥对隐藏信息解密
分析说明:
图7中表示使用RSA算法将需隐藏的信息加密后,把RSA的私钥通过文本保存的界面;图8显示的是通过选择图7保存的密钥解密后,显示的隐藏的信息。

源程序:
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package bmp;
  7. /**

  8. * @author 13673
  9. */
  10. import java.awt.BorderLayout;  
  11. import java.awt.Color;  
  12. import java.awt.Dimension;
  13. import java.awt.Graphics;  
  14. import java.awt.event.ActionEvent;  
  15. import java.awt.event.ActionListener;  
  16. import java.io.BufferedInputStream;  
  17. import java.io.BufferedOutputStream;  
  18. import java.io.File;  
  19. import java.io.FileInputStream;  
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.UnsupportedEncodingException;
  23. import java.math.BigInteger;
  24. import java.util.Enumeration;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javax.swing.JFileChooser;  
  28. import javax.swing.JFrame;
  29. import javax.swing.JMenu;  
  30. import javax.swing.JMenuBar;  
  31. import javax.swing.JMenuItem;  
  32. import javax.swing.JPanel;  
  33. import javax.swing.JScrollPane;
  34. import javax.swing.JTextArea;
  35. import javax.swing.UIManager;
  36. import javax.swing.filechooser.FileNameExtensionFilter;
  37. import javax.swing.plaf.FontUIResource;  
  38. /**
  39. *
  40. * @ClassName: 信息隐藏_LSB技术_于BMP位图
  41. * @Description: 基于24位BMP图片,运用LSB技术对文本信息进行隐藏
  42. * @author:
  43. * @date: 2019年5月3日
  44. */
  45. public class _LSB_BMP  extends JFrame{
  46.     private static final long serialVersionUID = 1L;
  47.     RsaKeyPair keyPair=RSAGeneratorKey.generatorKey(1024);//产生Rsa密钥对
  48.     boolean open_flag=false; //是否打开图片
  49.     int map[][];//保存像素颜色的数组 int 4个字节
  50.     int length_byte_sum=0;
  51.     MyPanel center;//绘图面板
  52.     File selectFile;//读取的文件
  53.     int width;//图像宽度
  54.     int height;//图像高度
  55.     byte temp1[];//图像前18个字节的信息
  56.     byte temp2[];//图像的28个字节的信息
  57.     JScrollPane scrollpane;//滑动面板
  58.     JTextArea infoJt;//文本信息区
  59.     JMenuItem open;     //打开文件
  60.     JMenuItem save;
  61.     JMenuItem hide;
  62.     JMenuItem show;
  63.     JMenuItem hide_rsa;
  64.     JMenuItem show_rsa;
  65.     int disWidth=1000;
  66.     int disHeight=800;
  67.     public _LSB_BMP() {  
  68.             setUIFont(new FontUIResource("微软雅黑", 0, 20));  //设置字体
  69.         this.setLayout(new BorderLayout());//设置布局
  70.         center=new MyPanel();   //初始化画图面板
  71.         center.setBackground(Color.WHITE);  
  72.         center.setBackground(Color.GRAY);
  73.       //  center.setPreferredSize(new Dimension(200, 300));
  74.         scrollpane=new JScrollPane(center);//用center初始化滚动面板  
  75.       //  scrollpane.setPreferredSize(new Dimension(200,100));
  76.         infoJt=new JTextArea();  //隐藏信息输入的控件
  77.         infoJt.setLineWrap(true);
  78.         infoJt.setWrapStyleWord(true);
  79.        JScrollPane infoJs=new JScrollPane(infoJt);
  80.         infoJs.setPreferredSize(new Dimension(disWidth, 300));
  81.         MyListener lis=new MyListener();  //设置经停事件  
  82.         JMenuBar menuBar=new JMenuBar();  
  83.         JMenu fileMenu=new JMenu("文件");  
  84.          open=new JMenuItem("打开");  
  85.          save=new JMenuItem("保存");
  86.         JMenu LSBMenu=new JMenu("LSB");  
  87.          hide=new JMenuItem("隐藏");  
  88.          show=new JMenuItem("显示");
  89.          JMenu RSAMenu=new JMenu("加密隐藏");  
  90.          hide_rsa=new JMenuItem("隐藏");  
  91.          show_rsa=new JMenuItem("显示");
  92.         open.addActionListener(lis);  
  93.         save.addActionListener(lis);
  94.         hide.addActionListener(lis);
  95.         show.addActionListener(lis);
  96.          hide_rsa.addActionListener(lis);
  97.         show_rsa.addActionListener(lis);
  98.         fileMenu.add(open);  
  99.         fileMenu.add(save);  
  100.         menuBar.add(fileMenu);
  101.         LSBMenu.add(hide);
  102.         LSBMenu.add(show);
  103.         menuBar.add(LSBMenu);
  104.         RSAMenu.add(hide_rsa);
  105.         RSAMenu.add(show_rsa);
  106.         menuBar.add(RSAMenu);//添加到菜单栏
  107.         this.setJMenuBar(menuBar);  
  108.         this.add(scrollpane,BorderLayout.CENTER);//加入滚动面板
  109.         this.add(infoJs,BorderLayout.SOUTH);
  110.         this.setTitle("LSB隐藏器");  
  111.         this.setSize(disWidth, disHeight);
  112.         this.setLocationRelativeTo(null);//设置窗体出现在屏幕中间
  113.         this.setDefaultCloseOperation(3);  
  114.         this.setVisible(true);
  115.     }  
  116.     /**

  117.      * 读取BMP文件

  118.      */  
  119.     public void readBMP()  
  120.     {  
  121.         try {  
  122.             FileInputStream fis=new FileInputStream(selectFile); //选择文件
  123.             //读取了一次就会依次往后移
  124.             try (BufferedInputStream bis = new BufferedInputStream(fis) //输入的缓冲区
  125.             ) {
  126.                 //读取了一次就会依次往后移
  127.                 byte[] wb=new byte[4];//存放宽度的字节数组
  128.                 byte[] hb=new byte[4];//存放高度的字节数组
  129.                 temp1=new byte[18];  //存放bmp前18个字节相关信息
  130.                 bis.read(temp1);//bis.skip(18);//跳过前18个byte
  131.                 String tp;
  132.                 //bis.skip(18);//跳过18个字节
  133.                 bis.read(wb);//读取宽度
  134.                 bis.read(hb);//读取高度
  135.                 width=byteToint(wb);
  136.                 System.out.println("wb:\""+width+"\"");
  137.                 height=byteToint(hb);
  138.                 System.out.println("hb:\""+height+"\"");
  139.                 map=new int[height][width];//保存图像的像素点的数组
  140.                 int skip=4-width*3%4;//得到每行要跳过的数字(与windows 系统机制有关)
  141.                 //使用调色板,然后前三个字节是颜色分量,最后个为空所以跳过
  142.                 temp2=new byte[28];  //bis.skip(28);
  143.                 bis.read(temp2);//bis.skip(28);
  144.                 if(temp1[10]==55)//偏移量为55,此位图已经隐入了信息数据
  145.                     bis.read();//跳过1字节
  146.                 for(int i=height-1;i>0;i--)
  147.                 {
  148.                     for(int j=0;j<width;j++)
  149.                     {
  150.                         int blue=bis.read();  //读取颜色的分量
  151.                         int green=bis.read();
  152.                         int red=bis.read();//
  153.                         Color c=new Color(red,green,blue);
  154.                         map[i][j]=c.getRGB(); //将RGB分量存入数组中
  155.                     }
  156.                     if(skip!=4)
  157.                         bis.skip(skip);  
  158.                     
  159.                 }
  160.             } //存放宽度的字节数组
  161.             center.setPreferredSize(new Dimension(width,height));  
  162.             javax.swing.SwingUtilities.updateComponentTreeUI(center);//这里必须要用updateComponentTreeUI(center)函数  
  163.         //不能用repaint()函数  
  164.         } catch (IOException e) {  
  165.         }  
  166.     }  

  167.      //保存bmp
  168.     public void writeBMP()  
  169.     {  
  170.         try {  
  171.             try (FileOutputStream fos = new FileOutputStream(selectFile)) {
  172.                 BufferedOutputStream bos=new BufferedOutputStream(fos);
  173.                 bos.write(temp1);//  心如头部文件
  174.                 bos.write(intTobyte(width,4));//宽度
  175.                 bos.write(intTobyte(height,4));//高度
  176.                 bos.write(temp2);
  177.                 int skip=4-width*3%4;//得到每行要跳过的数字(与windows 系统机制有关)
  178.                 if(temp1[10]==55)
  179.                     bos.write(new byte[1]);
  180.                 for(int i=height-1;i>=0;i--)
  181.                 {
  182.                     for(int j=0;j<width;j++)
  183.                     {
  184.                         Color c=new Color(map[i][j]);
  185.                         int blue=c.getBlue();
  186.                         int green=c.getGreen();
  187.                         int red=c.getRed();
  188.                         bos.write(blue);  //写入之前存放的像素点分量
  189.                         bos.write(green);
  190.                         bos.write(red);
  191.                     }
  192.                     if(skip!=4)
  193.                         bos.write(new byte[skip]);
  194.                 }
  195.                 bos.flush();
  196.             }  
  197.         } catch (IOException e) {  
  198.         }  
  199.     }  
  200.     //字节转int  
  201.     public static  int byteToint(byte b[])  
  202.     {  
  203.         int t1=(b[3]&0xff)<<24;  
  204.         int t2=(b[2]&0xff)<<16;  
  205.         int t3=(b[1]&0xff)<<8;  
  206.         int t4=b[0]&0xff;  
  207.        //System.out.println(b[1]&0xff);//输出的是一个整形数据  
  208.        //在java中,设计int和比int位数来的小的类型b,如byte,char等,都是先把小类型扩展成int再来运算,  ??????  
  209.               //return( t1<<24)+(t2<<16)+(t3<<8)+t4;//必须加括号
  210.         return t1+t2+t3+t4;  
  211.     }  
  212.     public static  int byteToint1(byte b[])  
  213.     {  
  214.       
  215.         int t3=(b[1]&0xff)<<8;  
  216.         int t4=b[0]&0xff;  
  217.        //System.out.println(b[1]&0xff);//输出的是一个整形数据  
  218.        //在java中,设计int和比int位数来的小的类型b,如byte,char等,都是先把小类型扩展成int再来运算,  ??????  
  219.               //return( t1<<24)+(t2<<16)+(t3<<8)+t4;//必须加括号
  220.         return t3+t4;  
  221.     }

  222.     //int 转字节

  223.     public static byte[] intTobyte(int a,int len)  

  224.     {  

  225.         byte []t=new byte[len];  
  226.             t[0]=(byte) ((a&0xff));  
  227.             if(len>1)  
  228.             t[1]=(byte)((a&0xff00)>>8);  
  229.             if(len>2)  
  230.             t[2]=(byte)((a&0xff0000)>>16);  
  231.             if(len>3)  
  232.             t[3]=(byte)((a&0xff000000)>>24);  
  233.         return t;  
  234.     }  

  235.    

  236.     private static void setUIFont(javax.swing.plaf.FontUIResource f) {
  237.                 Enumeration<Object> keys = UIManager.getDefaults().keys();
  238.                 while (keys.hasMoreElements()) {
  239.                         Object key = keys.nextElement();
  240.                         Object value = UIManager.get(key);
  241.                         if (value instanceof javax.swing.plaf.FontUIResource) {
  242.                                 UIManager.put(key, f);
  243.                         }
  244.                 }
  245.         }   
  246.     /**
  247.      * LSB处理
  248.      * @author
  249.      * @date 2019年5月3日
  250.      */
  251.     class LSB{
  252.         public  boolean hide(boolean flag) {
  253.             FileNameExtensionFilter filter=new FileNameExtensionFilter("文本文档(*.txt)", "txt");
  254.             JFileChooser fileChosser = new JFileChooser();  
  255.             fileChosser.setFileFilter(filter);  
  256.             fileChosser.setSelectedFile(new File("密钥.txt"));
  257.             fileChosser.setCurrentDirectory(new File("\\"));
  258.             String info="";
  259.             System.out.println("temp1【10】:"+temp1[10]);
  260.             temp1[10]+=1;//将位图数据偏移量增1,作为是否隐入数据的标识,此时temp1[10]=55;
  261.             System.out.println("temp1【10】:"+temp1[10]);
  262.             if(!flag)
  263.                     info=infoJt.getText(); //获取的隐藏信息
  264.             else{
  265.                 String mingwen = "0"+infoJt.getText();
  266.                 info=RSAUtil.encrypt(mingwen,keyPair.getPublicKey() ,"UTF-8");
  267.                 System.out.println("加密后:"+info);
  268.                 int choose=fileChosser.showSaveDialog(null);  
  269.                 if(choose==JFileChooser.APPROVE_OPTION)  
  270.                 {  
  271.                     selectFile=fileChosser.getSelectedFile();  
  272.                      try {  
  273.                             FileOutputStream fos=new FileOutputStream(selectFile);  
  274.                             BufferedOutputStream bos=new BufferedOutputStream(fos);
  275.                             byte[] N=keyPair.getPublicKey().getN().toString().getBytes();
  276.                             byte[] privateKey=keyPair.getPrivateKey().getA().toString().getBytes();
  277.                             String maohao=":";  
  278.                             byte[] mao=maohao.getBytes();
  279.                             length_byte_sum=N.length+privateKey.length+maohao.getBytes().length;
  280.                             System.out.println("length_byte_sum"+length_byte_sum);
  281.                             byte[] len=String.valueOf(length_byte_sum).getBytes();
  282.                            // bos.write(length_byte_sum);
  283.                             bos.write(len);//  
  284.                             bos.write(N);//   
  285.                             bos.write(mao);//   
  286.                             bos.write(privateKey);//   
  287.                             bos.flush();  
  288.                             fos.close();  
  289.                          } catch (IOException e) {  
  290.                  
  291.                         }  
  292.             
  293.                 }else{
  294.                     return false;
  295.                 }
  296.            
  297.                         
  298.                     }
  299.             
  300.              infoJt.setText("保存成功!");
  301.                         System.out.println("隐藏信息:"+info);
  302.                             char[] infoChs=info.toCharArray(); //将隐藏信息转成字符数组                       
  303.                         System.out.println("隐藏信息长度"+infoChs.length);
  304.                             int index=0,endIndex=0;
  305.                             String str;
  306.                             byte[] infoBins=new byte[infoChs.length*16];//隐藏信息的字符长度  
  307.                         //     二进制字符串                     一个字符两个字节 转成16位
  308.                             char[] wordBins=new char[16];
  309.                          //   字节二进制字符串                  
  310.                          //将每个字节隐藏的信
  311.                             for(int i=0;i<infoChs.length;i++) {
  312.                                     str=intToWordBinaryString((int)infoChs[i]);
  313.                                 //把int转成二进制字符串  7   111
  314.                                 //把每个字符都转成对应的二进制字符串
  315.                                //   System.out.print(str+"  ");
  316.                                     str=new StringBuilder(str).reverse().toString();
  317.                              //    System.out.print(str+"  ");
  318.                                 //reverse 逆序转换  123 321
  319.                                     wordBins=str.toCharArray();
  320.                                     for(int j=0;j<16;j++) {
  321.                                  //   System.out.print(wordBins[j]);
  322.                         
  323.                                             infoBins[index++]=(byte)(wordBins[j]-'0');     
  324.                                  //反向加密
  325.                                     }
  326.                              //  System.out.println();
  327.                             }
  328.                        
  329.                         //隐藏信息
  330.                             index=0;
  331.                             for(int i=height-1;i>0;i--) //hang
  332.                     for(int j=0;j<width;j++)  //lie
  333.                     {        //infoBins 已经将信息转换成字节反向的二进制字符串
  334.                             if(index<infoBins.length) {
  335.                               
  336.                                     map[i][j]=map[i][j]&0xfffffffe;  //int 4个字节                              
  337.                                 //数据末尾位清洗为0
  338.                                 //如果infoBins的位为1则将map对应像素点加1
  339.                                     if(infoBins[index]==1) {
  340.                                             map[i][j]+=1;
  341.                                     }
  342.                                //  System.out.print(map[i][j]+" ");
  343.                                     index++;
  344.                             }
  345.                             else
  346.                             {
  347.                                     map[i][j]=map[i][j]&0xfffffffe;//结束标记,一个字的最低位为0
  348.                                     endIndex++;
  349.                                     if(endIndex>=16)
  350.                                     return true;
  351.                             }
  352.                 }            
  353.             
  354.                    return false;            
  355.             }        
  356.             public String show(boolean flag) throws UnsupportedEncodingException {
  357.             byte temp[] = null;
  358.             FileNameExtensionFilter filter=new FileNameExtensionFilter("文本文档(*.txt)", "txt");
  359.             JFileChooser fileChosser = new JFileChooser();  
  360.             fileChosser.setFileFilter(filter);  
  361.             fileChosser.setCurrentDirectory(new File("\\"));
  362.                     String info="";
  363.                     byte[] wordBins=new byte[16];
  364.                     int index=0;
  365.                     String wordStr="";
  366.                     int wordInt;
  367.                     outer:for(int i=height-1;i>=0;i--) {
  368.                             for(int j=0;j<width;j++) {
  369.                                    wordBins[index++]=(byte)(map[i][j]&0x00000001);
  370.                                     if(index>=16) {
  371.                                             for(int k=15;k>=0;k--) {
  372.                                                     wordStr+=wordBins[k];
  373.                                             }
  374.                                             wordInt=Integer.parseInt(wordStr,2);
  375.                                             if(wordInt!=0) {
  376.                                                     info+=(char)wordInt;
  377.                                             }else {
  378.                                                     break outer;
  379.                                             }
  380.                                             index=0;wordStr="";
  381.                                     }
  382.                             }
  383.                     }
  384.                 String infor=info;
  385.                 String result=null;
  386.                 if(!flag)
  387.                     result = infor;
  388.                 else
  389.                 {
  390.                     int choose=fileChosser.showOpenDialog(null);  
  391.                     if(choose==JFileChooser.APPROVE_OPTION)//点击的是确定按钮   
  392.                     {  
  393.                            selectFile=fileChosser.getSelectedFile();  
  394.                      try {  
  395.                         FileInputStream fis=new FileInputStream(selectFile); //选择文件
  396.                         BufferedInputStream bis=new BufferedInputStream(fis); //输入的缓冲区
  397.                         byte[] wb=new byte[4];//存放宽度的字节数组
  398.                         bis.read(wb);
  399.                         System.out.println("wb:::"+ new String (wb));
  400.                         int len=Integer.parseInt( new String (wb) );
  401.                         temp =new byte[len];
  402.                         bis.read(temp);           
  403.                         bis.close();  
  404.                         String str= new String (temp);
  405.                         String[] sub=str.split(":");
  406.                         System.out.println("sub[0]"+sub[0]);
  407.                         System.out.println("sub[1]"+sub[1]);
  408.                         BigInteger n=new BigInteger(sub[0]);
  409.                         BigInteger a=new BigInteger(sub[1]);
  410.                         PrivateKey p =new PrivateKey( n,a);
  411.                         result=RSAUtil.decrypt(infor, p,"UTF-8");
  412.                          } catch (Exception e) {  
  413.                          }
  414.                      
  415.                     }
  416.                     
  417.                 }
  418.                     return result;
  419.             }
  420.             private String intToWordBinaryString(int i) {
  421.                         StringBuilder sb = new StringBuilder(Integer.toBinaryString(i));
  422.                         while (sb.length() < 16) {
  423.                                 sb.insert(0, "0");
  424.                         }
  425.                         return sb.toString();
  426.                 }
  427.     }
  428.     /**
  429.      * 显示面板
  430.      *
  431.      *
  432.      */  
  433.     class MyPanel extends JPanel{  
  434.         private static final long serialVersionUID = 1L;
  435.         public void paint(Graphics g) {  
  436.         super.paint(g);  
  437.         if(map!=null)  
  438.            {  
  439.                 for(int i=0;i<map.length;i++)  
  440.                 {  
  441.                     for(int j=0;j<map[i].length;j++)  
  442.                     {  
  443.                         g.setColor(new Color(map[i][j]));  
  444.                         g.drawLine(j+200, i, j+200, i);

  445.                     }
  446.                 }  
  447.             }  
  448.         }  
  449.     }      
  450.     class MyListener implements ActionListener{  
  451.         JFileChooser fileChosser;  
  452.         //文件选择控件
  453.         MyListener()  
  454.         {  
  455.             FileNameExtensionFilter filter=new FileNameExtensionFilter("24位位图(*.bmp)", "bmp");
  456.             fileChosser=new JFileChooser();  
  457.             fileChosser.setFileFilter(filter);  
  458.             fileChosser.setCurrentDirectory(new File("\\"));  
  459.         }  
  460.         public void actionPerformed(ActionEvent e) {
  461.                 JMenuItem jmi = (JMenuItem) e.getSource();
  462.             if(jmi==open)//选择的是打开  
  463.             {  
  464.                 open_flag=true;
  465.                int choose=fileChosser.showOpenDialog(null);  
  466.                 if(choose==JFileChooser.APPROVE_OPTION)//点击的是确定按钮   
  467.                 {  
  468.                     selectFile=fileChosser.getSelectedFile();  
  469.                     readBMP();
  470.                     infoJt.setText("图片打开成功!\n");
  471.                     if(temp1[10]==54) {
  472.                     infoJt.append("最多可嵌入"+height*width/8+"个字节的信息!\n");
  473.                     }else if(temp1[10]==55) {
  474.                       infoJt.append("此图片已经隐入有信息!");
  475.                     }
  476.                 }
  477.             }  
  478.             else if(jmi==save)//选择的是保存
  479.             {  
  480.                 if(open_flag){
  481.                     int choose=fileChosser.showSaveDialog(null);  
  482.                     if(choose==JFileChooser.APPROVE_OPTION)  
  483.                     {  
  484.                          selectFile=fileChosser.getSelectedFile();  
  485.                          writeBMP();  
  486.                          infoJt.setText("保存成功!");
  487.                     }  
  488.                 }else{
  489.                         infoJt.setText("请先打开图片!");
  490.                 }
  491.                
  492.             }
  493.             else if(jmi==hide) {
  494.                 if(open_flag){
  495.                     if(new LSB().hide(false)) {
  496.                            infoJt.setText("信息隐入成功!");
  497.                     }else{
  498.                             infoJt.setText("信息隐入出现不确定问题!");
  499.                     }
  500.                 }else{
  501.                      infoJt.setText("请先打开图片!");
  502.                 }
  503.             }
  504.              else if(jmi==hide_rsa) {
  505.                 if(open_flag){
  506.                     if(new LSB().hide(true)) {
  507.                            infoJt.setText("信息隐入成功(RSA加密)!");
  508.                     }else{
  509.                             infoJt.setText("信息隐入出现不确定问题!");
  510.                     }
  511.                 }else{
  512.                     infoJt.setText("请先打开图片!");
  513.                 }
  514.             }
  515.             else if(jmi==show) {
  516.                  if(open_flag){
  517.                     try {
  518.                         String info=new LSB().show(false);
  519.                         infoJt.setText("隐入的信息内容\n");
  520.                         infoJt.append(info);
  521.                     } catch (UnsupportedEncodingException ex) {
  522.                         Logger.getLogger(_LSB_BMP.class.getName()).log(Level.SEVERE, null, ex);
  523.                     }
  524.                  }else{
  525.                      infoJt.setText("请先打开图片!");
  526.                  }
  527.             }
  528.             else if(jmi==show_rsa) {
  529.                     if(open_flag){
  530.                 try {
  531.                         String info=new LSB().show(true);
  532.                         infoJt.setText("隐入的信息内容(RSA解密!)\n");
  533.                         infoJt.append(info);
  534.                     } catch (UnsupportedEncodingException ex) {
  535.                         Logger.getLogger(_LSB_BMP.class.getName()).log(Level.SEVERE, null, ex);
  536.                     }
  537.                     }else{
  538.                         infoJt.setText("请先打开图片!");
  539.                     }
  540.             }
  541.         }  
  542.     }  
  543.     public static void main(String[] args) {  
  544.        new _LSB_BMP();
  545.     }  
  546. }   
复制代码
0.png

全部资料51hei下载地址:
LSB,RSA的图片隐藏技术.zip (41.2 KB, 下载次数: 11)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表