⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applet3.java

📁 本程序是运用JAVA把流密码生成过程演示出来
💻 JAVA
字号:
/**
 * @(#)Applet3.java
 *
 *
 * @author 
 * @version 1.00 2009/5/20
 */


import java.applet.Applet;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class Applet3 extends Applet{
  private boolean isStandalone = false;
  static JTextField jTextField1 = new JTextField();
  static JLabel jLabel1 = new JLabel();
  static JButton jButton1 = new JButton();
  static JLabel jLabel2 = new JLabel();
  static JButton jButton2 = new JButton();
  static JLabel jLabel3 = new JLabel();
  static JButton jButton3 = new JButton();
  static JLabel jLabel4 = new JLabel();
  static JButton jButton4 = new JButton();
  static JTextArea jTextArea1 = new JTextArea();
  static JTextArea jTextArea2 = new JTextArea();
  static JTextArea jTextArea3 = new JTextArea();
  static JLabel jLabel5 = new JLabel();
  static JLabel jLabel6 = new JLabel();
  static JLabel jLabel7 = new JLabel();
  static JLabel jLabel8 = new JLabel();
  static int y;
  static String m;
  static String n;
  static String w;

  public static void main(String[] args)
  {
    try
    {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  public String getParameter(String key, String def)
  {
    return ((getParameter(key) != null) ? getParameter(key) : (isStandalone) ? System.getProperty(key, def) : def);
  }



  private static void jbInit() throws Exception {
      JFrame frame = new JFrame();
    jTextField1.setBorder(BorderFactory.createLineBorder(Color.black));
    jTextField1.setText("");
    jTextField1.setBounds(new Rectangle(149, 50, 80, 22));
    jTextField1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                jTextField1_actionPerformed(e);
            }
        });
    frame.setLayout(null);
    jLabel1.setForeground(SystemColor.windowBorder);
    jLabel1.setText("请输入明文");
    jLabel1.setBounds(new Rectangle(58, 46, 79, 30));
    jButton1.setBounds(new Rectangle(268, 49, 73, 25));
    jButton1.setForeground(Color.black);
    jButton1.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton1.setText("确定");
    jButton1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
            }
        });
    jLabel2.setText("密钥是");
    jLabel2.setBounds(new Rectangle(158, 119, 58, 28));
    jButton2.setBounds(new Rectangle(63, 174, 83, 25));
    jButton2.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton2.setText("加密");
    jButton2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                jButton2_actionPerformed(e);
            }
        });
    jLabel3.setText("密文是");
    jLabel3.setBounds(new Rectangle(157, 176, 54, 23));
    jButton3.setBounds(new Rectangle(63, 241, 80, 25));
    jButton3.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton3.setText("解密");
    jButton3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                jButton3_actionPerformed(e);
            }
        });
    jLabel4.setText("明文是");
    jLabel4.setBounds(new Rectangle(157, 244, 42, 16));
    jButton4.setBounds(new Rectangle(64, 120, 79, 25));
    jButton4.setBorder(BorderFactory.createLineBorder(Color.black));
    jButton4.setActionCommand("生成密钥");
    jButton4.setText("密钥");
    jButton4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                jButton4_actionPerformed(e);
            }
        });
    jTextArea1.setBorder(BorderFactory.createLineBorder(Color.black));
    jTextArea1.setText("");
    jTextArea1.setBounds(new Rectangle(223, 126, 128, 18));
    jTextArea2.setBorder(BorderFactory.createLineBorder(Color.black));
    jTextArea2.setText("");
    jTextArea2.setBounds(new Rectangle(225, 178, 127, 18));
    jTextArea3.setBorder(BorderFactory.createLineBorder(Color.black));
    jTextArea3.setText("");
    jTextArea3.setBounds(new Rectangle(223, 240, 130, 18));
    jLabel5.setText("1。获得密钥");
    jLabel5.setBounds(new Rectangle(60, 96, 153, 16));
    jLabel6.setForeground(Color.black);
    jLabel6.setText("加密过程如下所示:");
    jLabel6.setBounds(new Rectangle(59, 75, 137, 16));
    jLabel7.setText("2。运用模2加法进行加密");
    jLabel7.setBounds(new Rectangle(59, 154, 164, 16));
    jLabel8.setText("3。根据原密钥进行解密");
    jLabel8.setBounds(new Rectangle(61, 215, 167, 16));
    frame.setBackground(SystemColor.inactiveCaptionText);
    frame.setForeground(UIManager.getColor("inactiveCaptionText"));
    frame.add(jButton3, null);
    frame.add(jLabel6, null);
    frame.add(jTextArea3, null);
    frame.add(jButton2, null);
    frame.add(jTextArea2, null);
    frame.add(jTextArea1, null);
    frame.add(jLabel7, null);
    frame.add(jLabel5, null);
    frame.add(jLabel8, null);
    frame.add(jLabel2, null);
    frame.add(jLabel4, null);
    frame.add(jLabel3, null);
    frame.add(jButton4, null);
    frame.add(jLabel1, null);
    frame.add(jTextField1, null);
    frame.add(jButton1, null);
    frame.setSize(500,400);
    frame.setTitle("流密码");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);
  }

  public String getAppletInfo() {
    return "Applet Information";
  }

  public String[][] getParameterInfo() {
    return null;
  }

  public static String my(int x) throws NoSuchAlgorithmException {
    int bitlength = x;
    int certainty = 100;
    SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
    BigInteger p = new BigInteger(bitlength, certainty, sRandom);
    int m = p.intValue();
    String s = Integer.toBinaryString(m);
    return s;
  }

  public static String mtwo(String a, String b)
  {
    String s = "";
    char[] c = a.toCharArray();
    char[] d = b.toCharArray();

    for (int i = 0; i < y; ++i)
    {
      if (c[i] == d[i])
        c[i] = '0';
      else
        c[i] = '1';
    }
    for (int i = 0; i < y; ++i)
      s = s + c[i];
    return s;
  }

  static void jButton1_actionPerformed(ActionEvent e) {

    String s = jTextField1.getText();
    y = s.length();
    m = s;
    jTextArea1.setText("");
    jTextArea2.setText("");
    jTextArea3.setText("");
  }

  static void jTextField1_actionPerformed(ActionEvent e)
  {
  }

  static void jButton4_actionPerformed(ActionEvent e) {
    try {
      String w = my(y);
      n = w;
      jTextArea1.append(w);
    } catch (NoSuchAlgorithmException e1) {
    }
  }

  static void jButton2_actionPerformed(ActionEvent e) {
    w = mtwo(m, n);
    jTextArea2.append(w);
  }

  static void jButton3_actionPerformed(ActionEvent e)
  {
    String s = mtwo(n, w);
    jTextArea3.append(s);
    y = 0;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -