一个加密解密的小工具

来源:互联网 发布:微信网络推广策划 编辑:程序博客网 时间:2024/05/20 15:12

公司某总要求做一个可逆的加密解密的小工具

对界面要求不高,能用就行。所以用java swing 做了一个简陋的~

现在估计用java做c/s模式的不多了。我也只是上学时在课堂上听老师那么一提。除了腾讯还是才有c/s模式,发个版本要求你更新。那是你用了几十年了。离不开了啊。。。扯远了。


由于java swing是边学边做。所以成果很粗糙。

package com.ucs;//这段代码主要是创建一个登录窗口界面,在这个界面中有文本组件、普通按钮组件、标签组件,它们是按照网格组布局管理方式布局,  import javax.swing.*;  import Constants.CryptConstants;import java.awt.*;  import java.awt.event.*;  ///这是一个登录类。设计成一个继承容器的类。  ///WIDTH是指整个顶层框架的宽度。  ///HEIGHT是指整个顶层框架的长度。  public class codeUtil extends JPanel  {    /**    *     */    private static final long serialVersionUID = 1L;    static final int WIDTH=300;    static final int HEIGHT=300;    private static String code="加密";  JFrame loginframe;  ///按照网格组布局方式排列组件的方法  ///x指控件位于第几列。  ///y指控件位于第几行。  ///w指控件需要占几列。  ///h指控件需要占几行。    public void add(Component c,GridBagConstraints constraints,int x,int y,int w,int h)    {        constraints.gridx=x;        constraints.gridy=y;        constraints.gridwidth=w;        constraints.gridheight=h;        add(c,constraints);  }                                         //此方法用来添加控件到容器中  ///这是一个构造器方法  codeUtil()    {        loginframe=new JFrame("解密/加密工具");         loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        GridBagLayout lay=new GridBagLayout();          setLayout(lay);                                loginframe.add(this, BorderLayout.WEST);        loginframe.setSize(WIDTH,HEIGHT);        Toolkit kit=Toolkit.getDefaultToolkit();        Dimension screenSize=kit.getScreenSize();        int width=screenSize.width;        int height=screenSize.height;        int x=(width-WIDTH)/2;        int y=(height-HEIGHT)/2;        loginframe.setLocation(x,y);        JButton ok=new JButton("开始");             // JButton cancel=new JButton("放弃");        JLabel title=new JLabel("解密/加密工具");        JLabel name=new JLabel("密文/明文");        JLabel result=new JLabel("结果");        JLabel label=new JLabel("您所要做的操作:");    //  contentPane.add(label);      JComboBox comboBox=new JComboBox();      comboBox.addItem("加密");      comboBox.addItem("解密");      comboBox.addItemListener(new ItemListener() {                @Override          public void itemStateChanged(ItemEvent ie) {              if(ie.getStateChange() == 1) {                  //label.setText(ie.getItem().toString());                  code=ie.getItem().toString();              }          }             });           // contentPane.add(comboBox);      final JTextField nameinput=new JTextField(30);        final JTextField resultinput=new JTextField(30);        GridBagConstraints constraints=new GridBagConstraints();        constraints.fill=GridBagConstraints.NONE;        constraints.anchor=GridBagConstraints.EAST;        constraints.weightx=3;        constraints.weighty=4;        ok.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {             String result= methodOK(nameinput.getText());             resultinput.setText(result);          }      });      add(label,constraints,0,0,2,1);       add(comboBox,constraints,2,0,2,1);       add(name,constraints,0,1,1,1);        add(result,constraints,0,2,2,1);        add(nameinput,constraints,2,1,1,1);        add(resultinput,constraints,2,2,1,1);        add(ok,constraints,0,3,1,1);        //add(cancel,constraints,2,3,1,1);        loginframe.setResizable(false);        loginframe.setVisible(true);              }  public static String methodOK(String re){if("加密".equals(code)){return Crypt.encoderByDES(re, CryptConstants.getCryptKey());}else{return Crypt.decoderByDES(re, CryptConstants.getCryptKey());}}  public static void main(String[] args){        try {            //windows            //String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";            //Mac            //String lookAndFeel = "com.sun.java.swing.plaf.mac.MacLookAndFeel";            //default cross platform            //String lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();            //current system            String lookAndFeel = UIManager.getSystemLookAndFeelClassName();            //motif            //String lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";            //String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";            UIManager.setLookAndFeel(lookAndFeel);        } catch (Exception e) {            e.printStackTrace();        }        codeUtil util=new codeUtil();}  }
效果图:  

另外打包用的myEclipse自带的export


0 0
原创粉丝点击