java加密解密示例分享

 更新时间:2014年01月29日 11:13:09   作者:  
想要创造一个只有自己能看懂的文件吗?那就是对数据加密吧,下面分享一个java的数据加密与解密示例

(1)先定义要实现的类,我先定义了一个抽象类

复制代码 代码如下:

//图形类 
abstract class  Shape{ 
     int x,y; 
     int x1,y1; 
    Color color ; 
    Graphics g ; 
    byte type ; 
    public abstract void draw(Graphics g) ; 


//直线类 
 class LineShape extends Shape{ 

    public LineShape(int x, int y ,int x1,int y1,Color color){ 
        this.x = x ; 
        this.y = y ; 
        this.x1 = x1 ; 
        this.y1 = y1 ; 
        this.color = color ; 
        this.type = 0; 
    } 

    @Override 
    public void draw(Graphics g) { 
        g.setColor(color) ; 
        g.drawLine(x, y, x1, y1) ; 

    } 

 } 
//圆类 
class OvalShape extends Shape{ 

  public OvalShape(int x,int y, int x1,int y1,Color color){ 
        this.x = x ; 
        this.y = y ; 
        this.x1 = x1 ; 
        this.y1 = y1 ; 
        this.color = color ; 
        this.type = 1; 
     } 
    @Override 
    public void draw(Graphics g) { 

        g.setColor(color) ; 
        g.drawOval((x+x1)/2,(y+y1)/2,(x1-x)/2 ,(y1-y)/2 ); 
    } 

 } 
//图片类 
 class picture extends Shape{ 
     int a ; 
     int b ; 
     int cl[][] ; 
     public picture(int a,int b,int cl[][]){ 
         this.type =2 ; 
         this.a =a ; 
         this.b =b ; 
         this.cl =cl ; 

     } 
     public void draw(Graphics g){ 
         for(int k=0;k<a;k++){ 
              for(int j=0;j<b;j++){ 

                Color c   =  new Color(cl[k][j]) ; 
                g.setColor(c); 
                g.drawLine(k+100,j+100,k+100,j+100); 
              } 
          }  
     } 
 } 
 

(2)总方法类

复制代码 代码如下:

public class BmpSaveStart { 

    Graphics g ; 
    public static void main(String[] args) { 
         new BmpSaveStart() .UI(); 

    } 
    public void UI(){ 
        JFrame jf = new JFrame(); 
        jf.setSize(600,600); 

        jf.setTitle("图片的存储"); 
        jf.setBackground(Color.WHITE ); 
        JMenuBar bar = new JMenuBar() ; 
        JMenu menu1 = new JMenu("选项") ; 
        JMenuItem m1 = new JMenuItem("画圆"); 
        JMenuItem m2 = new JMenuItem("画线"); 
        JMenuItem m3 = new JMenuItem("存储"); 
        JMenuItem m4 = new JMenuItem("打开"); 
        JMenuItem m5 = new JMenuItem("图片"); 
        menu1.add(m1) ; 
        menu1.add(m2) ; 
        menu1.add(m3) ; 
        menu1.add(m4) ; 
        menu1.add(m5) ; 
        bar.add(menu1); 
        jf.setJMenuBar(bar); 

        jf.setDefaultCloseOperation(3); 
        jf.setVisible(true) ; 
        PaintDemo p =   new PaintDemo(g,jf) ; 

        m1.setActionCommand("画圆"); 
        m2.setActionCommand("画线"); 
        m3.setActionCommand("存储"); 
        m4.setActionCommand("打开"); 
        m5.setActionCommand("图片") ; 

        m1.addActionListener(p); 
        m2.addActionListener(p); 
        m3.addActionListener(p); 
        m4.addActionListener(p); 
        m5.addActionListener(p); 
    } 

 (3)监听类

复制代码 代码如下:

class PaintDemo implements MouseListener,ActionListener{ 
ImageIcon img = new ImageIcon("Images/100.gif"); 
BufferedImage bufImage = new BufferedImage(img.getIconWidth(),img.getIconHeight(),BufferedImage.TYPE_INT_RGB); 

 int x,y; 
 int x1,y1; 
 JFrame jf ; 
 Graphics g ; 
 String str ; 
 Color c ; 
 int cr ; 
 int cl[][] = new int[1000][1000] ; 

 
ArrayList<Shape> shapes = new ArrayList<Shape>() ; 
 Random random =new Random() ; 
 int R ; 
 int G ; 
 int B ; 
 public PaintDemo( Graphics g ,JFrame jf){ 
     this.jf = jf; 
     this.g = jf.getGraphics(); 
 } 
    @Override 
    public void mouseClicked(MouseEvent e) { 
        // TODO Auto-generated method stub 

    } 

    @Override 
    public void mousePressed(MouseEvent e) { 
           x=e.getX(); 
           y=e.getY(); 

         
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
          x1 = e.getX(); 
          y1 = e.getY(); 
          setColor() ; 
          if(str.equals("画圆")){ 
             // paintOval(); 

                Shape shape = new OvalShape(x, y, x1, y1,c) ; 
                shape.draw(g); 
                shapes.add(shape) ; 
            } 
            if(str.equals("画线")){ 
                //paintLine(); 

                Shape shape = new LineShape(x, y, x1, y1,c) ; 
                shape.draw(g); 
                shapes.add(shape) ; 
                /*File  f =new File("D:\\test.txt") ;
                if(f==null){
                    try {
                        f.createNewFile();
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }*/ 
            } 

                 

            } 
public void saveFile(String path,ArrayList<Shape> shapes){ 
    try{ 
        //文件输出流 
        FileOutputStream fos = new FileOutputStream(path) ; 
        //将文件输出流包装成可写基本类型的流 
        DataOutputStream dos = new DataOutputStream(fos) ; 
        dos.writeInt(shapes.size());//写入队列中图形的个数 
        //读取队列 
        for(int i=0;i<shapes.size();i++){ 
            //取出一种形状 
            Shape shape = shapes.get(i); 
            byte type = shape.type ; 

             
            if(type==0){//根据type判断类型,如果是直线 
                System.out.println("开始存储直线") ; 
                dos.writeByte(type); 
                LineShape line = (LineShape) shape ;//强制转换为直线类 
                //写直线形状的数据; 
                int x = line.x ; 
                int y = line.y ; 
                int x1 = line.x1 ; 
                int y1 = line.y1 ; 
                Color color = line.color ; 
                cr = color.getRGB(); 
                dos.writeInt(x); 
                dos.writeInt(y); 
                dos.writeInt(x1); 
                dos.writeInt(y1); 
                dos.writeInt(cr); 
            }else if(type == 1){ 
                dos.writeByte(type); 
                System.out.println("开始存储圆") ; 
                OvalShape oval = (OvalShape) shape ;//强制转换为圆类 
                //写直线形状的数据; 
                int x = oval.x ; 
                int y = oval.y ; 
                int x1 = oval.x1 ; 
                int y1 = oval.y1 ; 
                Color color = oval.color ; 
                cr = color.getRGB() ; 
                dos.writeInt(x); 
                dos.writeInt(y); 
                dos.writeInt(x1); 
                dos.writeInt(y1); 
                dos.writeInt(cr); 
            }else if(type ==2){ 

                dos.writeByte(type) ; 
                picture pl = (picture) shape ; 
                System.out.println("开始存储图片") ; 
                int width = pl.a ; 
                int height = pl.b ; 
                dos.writeInt(width) ; 
                dos.writeInt(height) ; 
                for(int k=0;k<width;k++){ 
                    for(int j=0;j<height;j++){ 
                        int t = pl.cl[k][j]; 
                        dos.writeInt(t) ; 
                    } 
                } 

            } 
            } 
        dos.flush() ; 
        fos.close(); 
    }catch(Exception e){ 
        e.printStackTrace(); 
    } 

public ArrayList<Shape> readFile(String path){ 

    try{ 
        //创建文件对象的输入流 
        FileInputStream fis = new FileInputStream(path); 
        //将文件输入流包装成可读基本类型的流 
        DataInputStream dis = new DataInputStream(fis); 
        //先读取文件长度,即总共的形状个数 
        int len = dis.readInt() ; 
        for(int i=0 ;i<len;i++){ 
            byte  type = dis.readByte(); 
            if(type==0){ 
            int a=  dis.readInt(); 
            int b=  dis.readInt(); 
            int c=  dis.readInt(); 
            int d=  dis.readInt(); 
            Color f= new Color(dis.readInt()); 
            LineShape line = new LineShape(a,b,c,d,f); 
            //读取直线的设置 

            //将直线存入队列 
            shapes.add(line) ; 
            }else if(type == 1){ 

                int a=  dis.readInt(); 
                int b=  dis.readInt(); 
                int c=  dis.readInt(); 
                int d=  dis.readInt(); 
                Color f= new Color(dis.readInt()); 
                System.out.println("开始读取圆") ; 
                OvalShape oval = new OvalShape(a,b,c,d,f); 
                shapes.add(oval) ; 
              }else if(type == 2){ 

                int   a=    dis.readInt(); 
                int    b=   dis.readInt(); 

                  for(int k=0;k<a;k++){ 
                      for(int j=0;j<b;j++){ 

                     cl[k] [j] = dis.readInt(); 

                      } 
                  } 
                  picture   pic = new picture(a,b,cl) ; 
                  shapes.add(pic) ; 
              } 

               
    }}catch(Exception e){ 
        e.printStackTrace(); 
    } 
    return shapes ; 

    @Override 
    public void mouseEntered(MouseEvent e) { 
        // TODO Auto-generated method stub 

    } 

    @Override 
    public void mouseExited(MouseEvent e) { 
        // TODO Auto-generated method stub 

    } 
    public void setColor(){ 
          R =random.nextInt(255); 
          G =random.nextInt(255); 
          B =random.nextInt(255); 
          c=new Color(R,G,B) ; 
    } 
   /* public void paintLine(){
        setColor();
          g.drawLine(x, y, x1, y1) ;
    }
    public void paintOval(){
        setColor();
        g.drawOval((x+x1)/2,(y+y1)/2,(x1-x)/2 ,(y1-y)/2 );
    }
    */ 
    @Override 
    public void actionPerformed(ActionEvent e) { 
        str = e.getActionCommand() ; 
        jf.addMouseListener(this); 
        if(str.equals("存储")){ 

            saveFile("D:\\test.txt",shapes) ; 

        } 
        if(str.equals("打开")){ 

            readFile("D:\\test.txt") ; 
            for(int i=0;i<shapes.size();i++){ 

                Shape shape = shapes.get(i); 
                if(shape.type ==0){ 
                    System.out.println("确定是圆类型"); 
                    LineShape line = (LineShape)shape ; 
                    line.draw(g); 
                }else if(shape.type==1){ 
                    System.out.println("确定是圆类型"); 
                    OvalShape  oval = (OvalShape)shape ; 
                    oval.draw(g); 
                }else if(shape.type ==2){ 
                    System.out.println("确定是图片类型"); 
                    picture pl = (picture)shape ; 
                    pl.draw(g) ; 

                } 

                 
            } 
        } 
        if(str.equals("图片")){ 

        Thread th = new Thread(new Runnable(){ 

            public void run(){ 
                while(true){ 
                    try{ 
                        Thread.sleep(30) ; 

               Graphics g1 = bufImage.getGraphics(); 

               g1.drawImage(img.getImage(), 0,0,null); 
             jf.getGraphics().drawImage(bufImage,100,100,null) ; 
              int width = bufImage.getWidth(); 
               int height = bufImage.getHeight() ; 
           for(int k=0;k<width;k++){ 
                for(int j=0;j<height;j++){ 
                    int p =bufImage.getRGB(k,j) ; 
                    cl[k][j] = p ; 
                         } 
                          } 
              picture pe =new picture(width,height,cl); 
              shapes.add(pe); 

                 
                       }catch(Exception e){ 
                        e.printStackTrace(); 
                         } 
            } 
        }}); 
        th.start(); 
        }         
    } 

相关文章

  • Java获取当前时间戳案例详解

    Java获取当前时间戳案例详解

    这篇文章主要介绍了Java获取当前时间戳案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • MyBatis自动生成Where语句

    MyBatis自动生成Where语句

    这篇文章主要介绍了MyBatis自动生成Where语句的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-08-08
  • spring初始化方法的执行顺序及其原理分析

    spring初始化方法的执行顺序及其原理分析

    这篇文章主要介绍了spring初始化方法的执行顺序及其原理分析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Swing常用组件之文本框和文本区

    Swing常用组件之文本框和文本区

    这篇文章主要为大家详细介绍了Swing常用组件之文本框(JTestField)和文本区(JTextArea),Swing是一个用于开发Java应用程序用户界面的开发工具包,本文开始带大家学习Swing
    2016-05-05
  • Mac下设置Java默认版本的方法

    Mac下设置Java默认版本的方法

    今天工作的时候发现了一个错误,提示java版本太低,无法启动!想起自己装过高版本的Java,但是却没有默认启动,从网上找了一些资料,整理下现在分享给大家,有需要的可以参考借鉴。
    2016-10-10
  • 网易Java程序员两轮面试 请问你能答对几个?

    网易Java程序员两轮面试 请问你能答对几个?

    为大家分享网易Java程序员两轮面试题,考考大家,这些问题你能答对几个?
    2017-11-11
  • Java设计模式编程之解释器模式的简单讲解

    Java设计模式编程之解释器模式的简单讲解

    这篇文章主要介绍了Java设计模式编程之解释器模式的讲解,解释器设计模式要注意其引发的性能问题,需要的朋友可以参考下
    2016-04-04
  • jvm运行原理以及类加载器实例详解

    jvm运行原理以及类加载器实例详解

    这篇文章主要给大家介绍了关于jvm运行原理以及类加载器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-05-05
  • elasticsearch bucket 之rare terms聚合使用详解

    elasticsearch bucket 之rare terms聚合使用详解

    这篇文章主要为大家介绍了elasticsearch bucket 之rare terms聚合使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • java9版本特性资源自动关闭的语法增强

    java9版本特性资源自动关闭的语法增强

    这篇文章主要为大家介绍了java9版本特性资源自动关闭的语法增强的详细使用说明,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03

最新评论