zoukankan      html  css  js  c++  java
  • 课后作业1:字串加密

    1、古罗马皇帝凯撒在打仗时曾经使用过以下方法加密军事情报:

       

      请编写一个程序,使用上述算法加密或解密用户输入的英文字串要求设计思想、程序流程图、源代码、结果截图

      设计思想:用户从弹出的输入消息框内输入字符串,不限制大小写。

      使用toCharArray()函数将字符串转化成字符数组,再由加密和解密两个按钮分别对字符数组的每个字符进行单独操作,加密则+3,解密则-3。再强制转换回字符类型并拼合成字符串,从不可编辑的文本框输出。

      程序流程图:

       

      程序源代码:

     1 package tiaoshi;
     2 import java.awt.*;
     3 import java.awt.event.*;
     4 import javax.swing.*;
     5 public class Test 
     6 {
     7     JFrame frame;
     8     JButton button1;
     9     JButton button2;
    10     JTextArea text;
    11     JScrollPane s;
    12     public static void main(String[] args) 
    13     {
    14         Test g=new Test();
    15         g.go();
    16     }
    17     public void go()
    18     {
    19         frame=new JFrame("密码改写");
    20         button1=new JButton("加密");
    21         button2=new JButton("解密");
    22         text=new JTextArea();
    23         s=new JScrollPane(text);
    24         Container cp=frame.getContentPane();
    25         cp.setLayout(null);
    26         cp.add(button1);
    27         cp.add(button2);
    28         cp.add(s);
    29         button1.setBounds(40,35,70,30);
    30         button2.setBounds(190,35,70,30);
    31         s.setBounds(20,100,260,60);
    32         text.setEditable(false);
    33         frame.setResizable(false);
    34         frame.setSize(300,200);
    35         frame.setVisible(true);
    36         button1.addActionListener(new ActionListener()
    37         {
    38             public void actionPerformed(ActionEvent e)
    39             {
    40                     String s1=JOptionPane.showInputDialog(frame,"加密");
    41                     char c[];
    42                     c=s1.toCharArray();
    43                     s1="";
    44                     for(int i=0;i<c.length;i++)
    45                     {
    46                         if(c[i]==89||c[i]==90||c[i]==88||c[i]==120||c[i]==121||c[i]==122)
    47                         c[i]=(char)(c[i]-23);
    48                         else
    49                         c[i]=(char) (c[i]+3);
    50                         s1+=c[i];
    51                     }
    52                     text.setText(s1);
    53             }
    54          });
    55         button2.addActionListener(new ActionListener()
    56         {
    57             public void actionPerformed(ActionEvent e)
    58             {
    59                  String s1=JOptionPane.showInputDialog(frame,"解密");
    60                  char c[];
    61                  c=s1.toCharArray();
    62                  s1="";
    63                  for(int i=0;i<c.length;i++)
    64                  {
    65                      if(c[i]==65||c[i]==66||c[i]==67||c[i]==97||c[i]==98||c[i]==99)
    66                      c[i]=(char)(c[i]+23);
    67                      else
    68                      c[i]=(char) (c[i]-3);
    69                      s1+=c[i];
    70                  }
    71                  text.setText(s1);
    72              }
    73           });
    74     }
    75 }

      结果截图:

      初始界面:

       

      加密过程:

       

       

      解密过程:

      

       

  • 相关阅读:
    在asp.net项目中使用winform窗口
    href="#"是什么意思呢? (转)
    在asp.net webservice中如何使用session(转载)
    ASP.NET 2.0 新追加控件ImageMap介绍(转)
    SEO是什么?
    推荐60个jQuery插件(转)
    asp.net webform 与asp.net mvc 混合开发项目总结
    获取SqlDataReader的列名
    梯度下降法 Method of steepest descent.
    蒙特卡洛算法
  • 原文地址:https://www.cnblogs.com/guobin-/p/7737163.html
Copyright © 2011-2022 走看看