10.22每日总结

实验3:工厂方法模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解工厂方法模式的动机,掌握该模式的结构;

2、能够利用工厂方法模式解决实际问题

 

[实验任务一]:加密算法

目前常用的加密算法有DES(Data Encryption Standard)和IDEA(International Data Encryption Algorithm)国际数据加密算法等,请用工厂方法实现加密算法系统。

 

1.画出对应的类图;

 

2.提交该系统的代码,该系统务必是一个可以能够直接使用的系统,查阅资料完成相应加密算法的实现;

 

3.注意编程规范。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//PWFactory.java
public interface PWFactory {
    public PW producePW();
}
//DESFactory.java
public class DESFactory implements PWFactory{
    public PW producePW()
    {
        return new DES();
    }
}
//IDEAFactory.java
public class IDEAFactory implements PWFactory{
    public PW producePW()
    {
        return new IDEA();
    }
}
//PW.java
public interface PW {
    public void show();
}
//DES.java
import java.security.SecureRandom;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DES implements PW{
    public static byte[] encrypt(byte[] datasource, String password) {
        try {
            SecureRandom random = new SecureRandom();
            DESKeySpec desKey = new DESKeySpec(password.getBytes());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(desKey);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            return cipher.doFinal(datasource);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
    public static byte[] decrypt(byte[] src, String password) throws Exception {
        SecureRandom random = new SecureRandom();
        DESKeySpec desKey = new DESKeySpec(password.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(desKey);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, securekey, random);
        return cipher.doFinal(src);
    }
    public void show() {
        System.out.println("DES");
        System.out.print("请输入加密内容:");
        Scanner out = new Scanner(System.in);
        String str = out.nextLine();
        String password = "12345678";
        byte[] result = DES.encrypt(str.getBytes(), password);
        System.out.println("加密后:" + result);
        try {
            System.out.println("解密内容:"+result);
            byte[] decryResult = DES.decrypt(result, password);
            System.out.println("解密后:" + new String(decryResult));
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }   
}
//IDEA.java
import java.security.SecureRandom;
import java.util.Scanner;
 
import javax.crypto.*;
public class IDEA implements PW{
private byte[] bytekey;
    public byte[] getKey(String key) {
        int len1 = key.length();
        if (len1 >= 16) {
            key = key.substring(0, 16);
        } else {
            for (int i = 0; i < 16 - len1; i++) {
                key = key.concat("0");
            }
        }
        bytekey = key.getBytes();
        return bytekey;
    }
    public String getEncString(String strMing) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = "";
        try {
            return byte2hex(IdeaEncrypt(bytekey, strMing.getBytes(), true));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            byteMing = null;
            byteMi = null;
        }
        return strMi;
    }
    public String getDesString(String strMi,int l) {
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = "";
        try {
            String tmp = new String(IdeaEncrypt(bytekey, hex2byte(strMi.getBytes()), false));
            return tmp.substring(0, l);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            byteMing = null;
            byteMi = null;
        }
        return strMing;
    }
    private byte[] Encrypt(byte[] bytekey, byte[] inputBytes, boolean flag) {
        byte[] encryptCode = new byte[8];
        int[] key = get_subkey(flag, bytekey);
        encrypt(key, inputBytes, encryptCode);
        return encryptCode;
    }
    private int bytesToInt(byte[] inBytes, int startPos) {
        return ((inBytes[startPos] << 8) & 0xff00) + (inBytes[startPos + 1] & 0xff);
    }
    private void intToBytes(int inputInt, byte[] outBytes, int startPos) {
        outBytes[startPos] = (byte) (inputInt >>> 8);
        outBytes[startPos + 1] = (byte) inputInt;
    }
    private int x_multiply_y(int x, int y) {
        if (x == 0) {
            x = 0x10001 - y;
        } else if (y == 0) {
            x = 0x10001 - x;
        } else {
            int tmp = x * y;
            y = tmp & 0xffff;
            x = tmp >>> 16;
            x = (y - x) + ((y < x) ? 1 : 0);
        }
        return x & 0xffff;
    }
    private void encrypt(int[] key, byte[] inbytes, byte[] outbytes) {
        int k = 0;
        int a = bytesToInt(inbytes, 0);
        int b = bytesToInt(inbytes, 2);
        int c = bytesToInt(inbytes, 4);
        int d = bytesToInt(inbytes, 6);
        for (int i = 0; i < 8; i++) {
            a = x_multiply_y(a, key[k++]);
            b += key[k++];
            b &= 0xffff;
            c += key[k++];
            c &= 0xffff;
            d = x_multiply_y(d, key[k++]);
            int tmp1 = b;
            int tmp2 = c;
            c ^= a;
            b ^= d;
            c = x_multiply_y(c, key[k++]);
            b += c;
            b &= 0xffff;
            b = x_multiply_y(b, key[k++]);
            c += b;
            c &= 0xffff;
            a ^= b;
            d ^= c;
            b ^= tmp2;
            c ^= tmp1;
        }
        intToBytes(x_multiply_y(a, key[k++]), outbytes, 0);
        intToBytes(c + key[k++], outbytes, 2);
        intToBytes(b + key[k++], outbytes, 4);
        intToBytes(x_multiply_y(d, key[k]), outbytes, 6);
    }
    private int[] encrypt_subkey(byte[] byteKey) {
        int[] key = new int[52];
        if (byteKey.length < 16) {
            byte[] tmpkey = new byte[16];
            System.arraycopy(byteKey, 0, tmpkey, tmpkey.length - byteKey.length, byteKey.length);
            byteKey = tmpkey;
        }
        for (int i = 0; i < 8; i++) {
            key[i] = bytesToInt(byteKey, i * 2);
        }
        for (int j = 8; j < 52; j++) {
            if ((j & 0x7) < 6) {
                key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 6] >> 7)) & 0xffff;
            } else if ((j & 0x7) == 6) {
                key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff;
            } else {
                key[j] = (((key[j - 15] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff;
            }
        }
        return key;
    }
    private int fun_a(int a) {
        if (a < 2) {
            return a;
        }
        int b = 1;
        int c = 0x10001 / a;
        for (int i = 0x10001 % a; i != 1;) {
            int d = a / i;
            a %= i;
            b = (b + (c * d)) & 0xffff;
            if (a == 1) {
                return b;
            }
            d = i / a;
            i %= a;
            c = (c + (b * d)) & 0xffff;
        }
        return (1 - c) & 0xffff;
    }
    private int fun_b(int b) {
        return (0 - b) & 0xffff;
    }
    private int[] uncrypt_subkey(int[] key) {
        int dec = 52;
        int asc = 0;
        int[] unkey = new int[52];
        int aa = fun_a(key[asc++]);
        int bb = fun_b(key[asc++]);
        int cc = fun_b(key[asc++]);
        int dd = fun_a(key[asc++]);
        unkey[--dec] = dd;
        unkey[--dec] = cc;
        unkey[--dec] = bb;
        unkey[--dec] = aa;
        for (int k1 = 1; k1 < 8; k1++) {
            aa = key[asc++];
            bb = key[asc++];
            unkey[--dec] = bb;
            unkey[--dec] = aa;
            aa = fun_a(key[asc++]);
            bb = fun_b(key[asc++]);
            cc = fun_b(key[asc++]);
            dd = fun_a(key[asc++]);
            unkey[--dec] = dd;
            unkey[--dec] = bb;
            unkey[--dec] = cc;
            unkey[--dec] = aa;
        }
        aa = key[asc++];
        bb = key[asc++];
        unkey[--dec] = bb;
        unkey[--dec] = aa;
        aa = fun_a(key[asc++]);
        bb = fun_b(key[asc++]);
        cc = fun_b(key[asc++]);
        dd = fun_a(key[asc]);
        unkey[--dec] = dd;
        unkey[--dec] = cc;
        unkey[--dec] = bb;
        unkey[--dec] = aa;
        return unkey;
    }
    private int[] get_subkey(boolean flag, byte[] bytekey) {
        if (flag) {
            return encrypt_subkey(bytekey);
        } else {
            return uncrypt_subkey(encrypt_subkey(bytekey));
        }
    }
    private byte[] ByteDataFormat(byte[] data, int unit) {
        int len = data.length;
        int padlen = unit - (len % unit);
        int newlen = len + padlen;
        byte[] newdata = new byte[newlen];
        System.arraycopy(data, 0, newdata, 0, len);
        for (int i = len; i < newlen; i++)
            newdata[i] = (byte) padlen;
        return newdata;
    }
    public byte[] IdeaEncrypt(byte[] idea_key, byte[] idea_data, boolean flag) {
        byte[] format_key = ByteDataFormat(idea_key, 16);
        byte[] format_data = ByteDataFormat(idea_data, 8);
        int datalen = format_data.length;
        int unitcount = datalen / 8;
        byte[] result_data = new byte[datalen];
        for (int i = 0; i < unitcount; i++) {
            byte[] tmpkey = new byte[16];
            byte[] tmpdata = new byte[8];
            System.arraycopy(format_key, 0, tmpkey, 0, 16);
            System.arraycopy(format_data, i * 8, tmpdata, 0, 8);
            byte[] tmpresult = Encrypt(tmpkey, tmpdata, flag);
            System.arraycopy(tmpresult, 0, result_data, i * 8, 8);
        }
        return result_data;
    }
    public static String byte2hex(byte[] b) { // 一个字节的数,
                                                // 转成16进制字符串
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase(); // 转成大写
    }
    public static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("长度不是偶数");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }
    public void show() {
        System.out.println("IDEA");
        System.out.print("请输入加密内容:");
        Scanner out = new Scanner(System.in);
        String str = out.nextLine();
        IDEA idea = new IDEA();
        idea.getKey("aabb");// 生成密匙
        String strEnc = idea.getEncString(str);// 加密字符串,返回String的密文
        System.out.println("加密后:"+strEnc);
        int ll=str.length();
        System.out.println("解密内容:"+strEnc);
        String strDes = idea.getDesString(strEnc,ll);// 把String 类型的密文解密
        System.out.println("解密后:"+strDes);
    }
}
//main.java
import java.util.Scanner;
 
public class main {
    public static void main(String[] args) {
        try{
            PW pw;
            PWFactory mif = null;
            System.out.println("请选择:1、DES算法  2、IDEA算法");
            Scanner input=new Scanner(System.in);
            int i=input.nextInt();
            if(i==1) {
                mif=new DESFactory();
            }else if(i==2) {
                mif=new IDEAFactory();
            }else {
                System.out.println("输入无效!");
            }
            pw=mif.producePW();
            pw.show();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

  

 

posted @   漏网鲨鱼  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示