1 package makepeople;
 2 
 3 public class Man implements Person{
 4     
 5     public Man() {
 6         
 7     }
 8     public void make(){
 9         
10         System.out.print("生产男人");
11         
12     }
13 }
Man
 1 package makepeople;
 2 
 3 public class Woman implements Person{
 4     
 5     public Woman() {
 6     }
 7     
 8     public void make() {
 9         
10         System.out.print("生产女人");
11         
12     }
13 }
Woman
 1 package makepeople;
 2 
 3 public class Nvwa {
 4 
 5     public Nvwa() {
 6     
 7     }
 8 
 9     public static  Person Personjudge(String arg ) {
10         //判断参数,再选择调用哪个类
11         if(arg.equalsIgnoreCase("M"))
12         {
13             return new Man();
14         }else if(arg.equalsIgnoreCase("W"))
15         {
16             return new Woman();
17         }else if(arg.equalsIgnoreCase("R"))
18         {
19             return new Robot();
20         }else
21         {
22             return null;
23         }
24     }
25     }
Nvwa
1 package makepeople;
2 
3 
4 public interface Person {
5     
6     public void make();
7     
8 }
Person
 1 package makepeople;
 2 
 3 
 4 public class Robot implements Person{
 5     
 6     public Robot() {
 7     }
 8     
 9     public void make(){
10         
11         System.out.print("生产机器人");
12         
13     }
14 }
Robot

 实验三:

 1 package desidea;
 2 
 3 
 4 import javax.crypto.Cipher;
 5 import javax.crypto.KeyGenerator;
 6 import javax.crypto.SecretKey;
 7 
 8 /**
 9  * 具体方法类DES
10  * @author dell
11  *
12  */
13 public class DES implements Method{
14      public void work(String str, String password) {
15             String begincode = "信2105一班 20213727 余志强"; // 要加密的明文
16             String endcode = null; // 加密后的密文
17             String opencode = null; // 密文解密后得到的明文
18             System.out.println("要加密的明文:" + begincode);
19             String cipherType = "DESede"; // 加密算法类型,可设置为DES(56)、DESede(112)、AES等字符串(128)
20             try {
21                 // 获取密钥生成器
22                 KeyGenerator keyGen = KeyGenerator.getInstance(cipherType);
23                 // 初始化密钥生成器,不同的加密算法其密钥长度可能不同
24                 keyGen.init(112);
25                 // 生成密钥
26                 SecretKey key = keyGen.generateKey();
27                 // 得到密钥字节码
28                 byte[] keyByte = key.getEncoded();
29                 // 输出密钥的字节码
30                 System.out.println("密钥是:");
31                 for (int i = 0; i < keyByte.length; i++) {
32                     System.out.print(keyByte[i] + ",");
33                 }
34                 System.out.println("");
35                 // 创建密码器
36                 Cipher cp = Cipher.getInstance(cipherType);
37                 // 初始化密码器
38                 cp.init(Cipher.ENCRYPT_MODE, key);
39                 System.out.println("要加密的字符串是:" + begincode);
40                 byte[] codeStringByte = begincode.getBytes("UTF8");
41                 System.out.println("要加密的字符串对应的字节码是:");
42                 for (int i = 0; i < codeStringByte.length; i++) {
43                     System.out.print(codeStringByte[i] + ",");
44                 }
45                 System.out.println("");
46                 // 开始加密
47                 byte[] codeStringByteEnd = cp.doFinal(codeStringByte);
48                 System.out.println("加密后的字符串对应的字节码是:");
49                 for (int i = 0; i < codeStringByteEnd.length; i++) {
50                     System.out.print(codeStringByteEnd[i] + ",");
51                 }
52                 System.out.println("");
53                 endcode = new String(codeStringByteEnd);
54                 System.out.println("加密后的字符串是:" + endcode);
55                 System.out.println("");
56                 // 重新初始化密码器
57                 cp.init(Cipher.DECRYPT_MODE, key);
58                 // 开始解密
59                 byte[] decodeStringByteEnd = cp.doFinal(codeStringByteEnd);
60                 System.out.println("解密后的字符串对应的字节码是:");
61                 for (int i = 0; i < decodeStringByteEnd.length; i++) {
62                     System.out.print(decodeStringByteEnd[i] + ",");
63                 }
64                 System.out.println("");
65                 opencode = new String(decodeStringByteEnd);
66                 System.out.println("解密后的字符串是:" + opencode);
67                 System.out.println("");
68             } catch (Exception e) {
69                 e.printStackTrace();
70             }
71         }
72 
73         public static void main(String[] args) {
74             // TODO Auto-generated method stub
75             System.out.println("DES加密算法");
76             DES des = new DES();
77             try {
78                 des.work("8787878787878787", "0E329232EA6D0D73");
79 
80             } catch (Exception e) {
81                 System.out.println(e.getMessage());
82             }
83         }
84     
85 }
DES
1 package desidea;
2 
3 public class DESFactory implements MethodFactory {
4     public DES produceMethod() {
5         System.out.println("使用DES算法");
6         return new DES();
7     }
8 }
DESFactory
  1 package desidea;
  2 
  3 
  4 import javax.crypto.Cipher;
  5 import javax.crypto.KeyGenerator;
  6 import javax.crypto.SecretKey;
  7 import org.apache.commons.codec.binary.Base64;
  8 import org.bouncycastle.jce.provider.BouncyCastleProvider;
  9 
 10 
 11 import javax.crypto.spec.SecretKeySpec;
 12 import java.security.Key;
 13 import java.security.Security;
 14 
 15 public class IDEA implements Method {
 16 
 17     public static final String KEY_ALGORITHM = "IDEA";
 18 
 19     public static final String CIPHER_ALGORITHM = "IDEA/ECB/ISO10126Padding";
 20 
 21     public static byte[] initkey() throws Exception {
 22         // 加入bouncyCastle支持
 23         Security.addProvider(new BouncyCastleProvider());
 24 
 25         // 实例化密钥生成器
 26         KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
 27         // 初始化密钥生成器,IDEA要求密钥长度为128位
 28         kg.init(128);
 29         // 生成密钥
 30         SecretKey secretKey = kg.generateKey();
 31         // 获取二进制密钥编码形式
 32         return secretKey.getEncoded();
 33     }
 34 
 35     /**
 36      * 转换密钥
 37      * 
 38      * @param key   二进制密钥       
 39      * @return Key 密钥
 40      * 
 41      */
 42     private static Key toKey(byte[] key) throws Exception {
 43         // 实例化DES密钥
 44         // 生成密钥
 45         SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
 46         return secretKey;
 47     }
 48 
 49     /**
 50      * 加密数据
 51      * 
 52      * @param data   待加密数据
 53      * @param key  密钥
 54      * @return byte[] 加密后的数据
 55      * 
 56      */
 57     private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
 58         // 加入bouncyCastle支持
 59         Security.addProvider(new BouncyCastleProvider());
 60         // 还原密钥
 61         Key k = toKey(key);
 62         // 实例化
 63         Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
 64         // 初始化,设置为加密模式
 65         cipher.init(Cipher.ENCRYPT_MODE, k);
 66         // 执行操作
 67         return cipher.doFinal(data);
 68     }
 69 
 70     /**
 71      * 解密数据
 72      * 
 73      * @param data  待解密数据
 74      * @param key   密钥
 75      * @return byte[] 解密后的数据
 76      * 
 77      */
 78     private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
 79         // 加入bouncyCastle支持
 80         Security.addProvider(new BouncyCastleProvider());
 81         // 还原密钥
 82         Key k = toKey(key);
 83         Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
 84         // 初始化,设置为解密模式
 85         cipher.init(Cipher.DECRYPT_MODE, k);
 86         // 执行操作
 87         return cipher.doFinal(data);
 88     }
 89 
 90     public static String getKey() {
 91         String result = null;
 92         try {
 93             result = Base64.encodeBase64String(initkey());
 94         } catch (Exception e) {
 95             e.printStackTrace();
 96         }
 97         return result;
 98     }
 99 
100     public static String ideaEncrypt(String data, String key) {
101         String result = null;
102         try {
103             byte[] data_en = encrypt(data.getBytes(), Base64.decodeBase64(key));
104             result = Base64.encodeBase64String(data_en);
105         } catch (Exception e) {
106             e.printStackTrace();
107         }
108         return result;
109     }
110 
111     public static String ideaDecrypt(String data, String key) {
112         String result = null;
113         try {
114             byte[] data_de = decrypt(Base64.decodeBase64(data), Base64.decodeBase64(key));
115             ;
116             result = new String(data_de);
117         } catch (Exception e) {
118             e.printStackTrace();
119         }
120         return result;
121     }
122 
123     public void work(String str, String password) {
124         String data = "软工一班20213727余志强"; // 要加密的明文
125         String key = getKey();
126         System.out.println("要加密的原文:" + data);
127         System.out.println("密钥:" + key);
128         String data_en = ideaEncrypt(data, key);
129         System.out.println("密文:" + data_en);
130         String data_de = ideaDecrypt(data_en, key);
131         System.out.println("原文:" + data_de);
132     }
133 
134     public static void main(String[] args) {
135         // TODO Auto-generated method stub
136         System.out.println("IDEA加密算法");
137         IDEA idea = new IDEA();
138         try {
139             idea.work("8787878787878787", "0E329232EA6D0D73");
140         } catch (Exception e) {
141             System.out.println(e.getMessage());
142         }
143     }
144 
145 }
IDEA
1 package desidea;
2 
3 public class IDEAFactory implements MethodFactory {
4     public IDEA produceMethod() {
5         System.out.println("使用IDEA算法");
6         return new IDEA();
7     }
8 }
IDEAFactory
1 package desidea;
2 
3 public interface Method {
4      public void work(String str, String password);
5 }
Method
1 package desidea;
2 
3 public interface MethodFactory {
4     public Method produceMethod();
5 }
MethodFactory
 1 package desidea;
 2 
 3 
 4 
 5 import java.util.Scanner;
 6 
 7 public class zhuhanshu {
 8 
 9     public static void main(String[] args) {
10 
11         DES des = new DES();
12         IDEA idea = new IDEA();
13         try {
14             int n = 0;
15 
16             Scanner in = new Scanner(System.in);
17             while (n != 3) {
18                 System.out.println("请选择要使用的加密算法 1.DES加密算法 2.IDEA加密算法");
19                 System.out.println("3.退出");
20                 System.out.println("请选择:");
21                 if (in.hasNextInt()) {
22                     n = in.nextInt();
23                 } else {
24                     System.out.println("输入的不是整数,请重新输入:");
25                     continue;
26                 }
27                 switch (n) {
28                 case 1: {
29 
30                     des.work("1787878787878787", "0E329232EA6D0D73");
31                     break;
32                 }
33                 case 2: {
34                     idea.work("8787878787878787", "0E329232EA6D0D73");
35                     break;
36                 }
37                 }
38             }
39         }
40 
41         catch (Exception e) {
42             System.out.println(e.getMessage());
43         }
44     }
45 }
zhuhanshu

 

posted on 2023-10-26 07:57  夜的第七章i  阅读(3)  评论(0编辑  收藏  举报