2024/9/27

软件设计                  石家庄铁道大学信息学院

 

实验3:工厂方法模式

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

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

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

 
   

 


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

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

实验要求:

1.  画出对应的类图;

2. 

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

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.SecureRandom;
import java.util.Arrays;

// 抽象加密算法
interface EncryptionAlgorithm {
    byte[] encrypt(byte[] data) throws Exception;
    byte[] decrypt(byte[] data) throws Exception;
}

// DES 加密算法实现
class DESEncryption implements EncryptionAlgorithm {
    private final SecretKeySpec keySpec;
    private final IvParameterSpec ivSpec;

    public DESEncryption(byte[] key) throws Exception {
        this.keySpec = new SecretKeySpec(key, "DES");
        this.ivSpec = new IvParameterSpec(new byte[8]); // 8 字节 IV
    }

    @Override
    public byte[] encrypt(byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        return cipher.doFinal(data);
    }

    @Override
    public byte[] decrypt(byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        return cipher.doFinal(data);
    }
}

// IDEA 加密算法实现
class IDEAEncryption implements EncryptionAlgorithm {
    private final SecretKeySpec keySpec;
    private final IvParameterSpec ivSpec;

    public IDEAEncryption(byte[] key) throws Exception {
        this.keySpec = new SecretKeySpec(key, "IDEA");
        this.ivSpec = new IvParameterSpec(new byte[8]); // 8 字节 IV
    }

    @Override
    public byte[] encrypt(byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance("IDEA/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        return cipher.doFinal(data);
    }

    @Override
    public byte[] decrypt(byte[] data) throws Exception {
        Cipher cipher = Cipher.getInstance("IDEA/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        return cipher.doFinal(data);
    }
}

// 抽象工厂
interface EncryptionFactory {
    EncryptionAlgorithm createAlgorithm(byte[] key) throws Exception;
}

// DES 工厂
class DESEncryptionFactory implements EncryptionFactory {
    @Override
    public EncryptionAlgorithm createAlgorithm(byte[] key) throws Exception {
        return new DESEncryption(key);
    }
}

// IDEA 工厂
class IDEAEncryptionFactory implements EncryptionFactory {
    @Override
    public EncryptionAlgorithm createAlgorithm(byte[] key) throws Exception {
        return new IDEAEncryption(key);
    }
}

// 客户端示例
public class Client {
    public static void main(String[] args) {
        try {
            // 生成一个 8 字节的 DES 密钥
            byte[] desKey = new byte[8];
            new SecureRandom().nextBytes(desKey);
            EncryptionFactory desFactory = new DESEncryptionFactory();
            EncryptionAlgorithm desAlgorithm = desFactory.createAlgorithm(desKey);

            byte[] data = "Hello, World!".getBytes();
            byte[] encryptedData = desAlgorithm.encrypt(data);
            System.out.println("DES Encrypted: " + Arrays.toString(encryptedData));
            byte[] decryptedData = desAlgorithm.decrypt(encryptedData);
            System.out.println("DES Decrypted: " + new String(decryptedData));

            // 生成一个 16 字节的 IDEA 密钥
            byte[] ideaKey = new byte[16];
            new SecureRandom().nextBytes(ideaKey);
            EncryptionFactory ideaFactory = new IDEAEncryptionFactory();
            EncryptionAlgorithm ideaAlgorithm = ideaFactory.createAlgorithm(ideaKey);

            byte[] encryptedDataIdea = ideaAlgorithm.encrypt(data);
            System.out.println("IDEA Encrypted: " + Arrays.toString(encryptedDataIdea));
            byte[] decryptedDataIdea = ideaAlgorithm.decrypt(encryptedDataIdea);
            System.out.println("IDEA Decrypted: " + new String(decryptedDataIdea));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

3.注意编程规范。

posted @   Hbro  阅读(6)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示