9.29

实验3:工厂方法模式

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

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

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

 

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

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

实验要求:

1. 画出对应的类图;

 

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

 

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class EncryptionFactory {

    public static EncryptionAlgorithm createEncryptionAlgorithm(String algorithmType) {
        switch (algorithmType) {
            case "DES":
                return new DESAlgorithm();
            case "IDEA":
                return new IDEAAlgorithm();
            default:
                throw new IllegalArgumentException("Unknown algorithm type: " + algorithmType);
        }
    }

    interface EncryptionAlgorithm {
        byte[] encrypt(byte[] data);

        byte[] decrypt(byte[] encryptedData);
    }

    static class DESAlgorithm implements EncryptionAlgorithm {

        @Override
        public byte[] encrypt(byte[] data) {
            try {
                SecretKey key = KeyGenerator.getInstance("DES").generateKey();
                Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                return cipher.doFinal(data);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        public byte[] decrypt(byte[] encryptedData) {
            try {
                SecretKey key = KeyGenerator.getInstance("DES").generateKey();
                Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, key);
                return cipher.doFinal(encryptedData);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }

    static class IDEAAlgorithm implements EncryptionAlgorithm {

        @Override
        public byte[] encrypt(byte[] data) {
            try {
                KeyGenerator keyGen = KeyGenerator.getInstance("IDEA");
                SecretKey key = keyGen.generateKey();
                Cipher cipher = Cipher.getInstance("IDEA/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, key);
                return cipher.doFinal(data);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        public byte[] decrypt(byte[] encryptedData) {
            try {
                KeyGenerator keyGen = KeyGenerator.getInstance("IDEA");
                SecretKey key = keyGen.generateKey();
                Cipher cipher = Cipher.getInstance("IDEA/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, key);
                return cipher.doFinal(encryptedData);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
    }
}

 

public class Main {
    public static void main(String[] args) {
        try {
            // 使用 DES 加密
            EncryptionFactory.EncryptionAlgorithm desEncryptor = EncryptionFactory.createEncryptionAlgorithm("DES");
            byte[] encryptedDataDES = desEncryptor.encrypt("Hello, world!".getBytes());
            byte[] decryptedDataDES = desEncryptor.decrypt(encryptedDataDES);
            System.out.println("DES encrypted data: " + new String(encryptedDataDES));
            System.out.println("DES decrypted data: " + new String(decryptedDataDES));

            // 使用 IDEA 加密
            EncryptionFactory.EncryptionAlgorithm ideaEncryptor = EncryptionFactory.createEncryptionAlgorithm("IDEA");
            byte[] encryptedDataIDEA = ideaEncryptor.encrypt("Hello, world!".getBytes());
            byte[] decryptedDataIDEA = ideaEncryptor.decrypt(encryptedDataIDEA);
            System.out.println("IDEA encrypted data: " + new String(encryptedDataIDEA));
            System.out.println("IDEA decrypted data: " + new String(decryptedDataIDEA));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.注意编程规范。

 

posted @ 2024-11-27 08:15  The-rich  阅读(13)  评论(0)    收藏  举报