课后作业1:字串加密

题目

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

请编写一个程序,使用上述算法加密或解密用户输入的英文字串。

源程序

 1 package fbgtr;
 2 
 3 import javax.swing.JOptionPane;
 4 public class Entryption {
 5     
 6     public static  String jiami(String str)
 7     {
 8         String s;
 9         char c[] = new char[str.length()];
10         str.getChars(0, str.length(), c,0);
11         
12         for(int i = 0;i<str.length();i++)
13         {
14             if(c[i]=='x')
15                 c[i]='a';
16             else if(c[i]=='y')
17                 c[i]='b';
18             else if(c[i]=='z')
19                 c[i]='c';
20             else if(c[i] == ' ')
21                 c[i] = c[i];
22             else
23                 c[i] += 3;
24         }
25         s= new String(c);
26         return s;
27     }
28     
29     public static String jiemi(String str)
30     {
31         String s;
32         char c[] = new char[str.length()];
33         str.getChars(0, str.length(), c,0);
34         
35         for(int i = 0;i<str.length();i++)
36         {
37             if(c[i]=='c')
38                 c[i]='z';
39             else if(c[i]=='b')
40                 c[i]='y';
41             else if(c[i]=='a')
42                 c[i]='x';
43             else if(c[i] == ' ')
44                 c[i] = c[i];
45             else
46                 c[i]-=3 ;
47         }
48         s= new String(c);
49         return s;
50     }
51     
52     public static void main(String args[])
53     {
54         String str;
55         str = JOptionPane.showInputDialog("请输入:");
56         
57         JOptionPane.showMessageDialog(null,"加密后的字符串是:"+jiami(str)+"
解密后的字符串是:hj"+jiemi(str),null,
58                 JOptionPane.PLAIN_MESSAGE);
59         
60     }
61 
62 }

结果截图

原文地址:https://www.cnblogs.com/weipinggong/p/4907108.html