工厂方法模式

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

实验要求:

1.画出对应的类图;

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

3.注意编程规范。

1.类图

 

2. 系统代码

package org.example;

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

package org.example;

import java.util.Arrays;

public class DESAlgorithm implements EncryptionAlgorithm {
    @Override
    public byte[] encrypt(byte[] data) {
        // 实现DES加密逻辑
        return Arrays.copyOfRange(data, 0, data.length);
    }
}

package org.example;

import java.util.Arrays;

public class IDEAAlgorithm implements EncryptionAlgorithm {
    @Override
    public byte[] encrypt(byte[] data) {
        // 实现IDEA加密逻辑
        return Arrays.copyOfRange(data, 0, data.length);
    }
}

package org.example;

public class EncryptionAlgorithmFactory {
    public static EncryptionAlgorithm getEncryptionAlgorithm(String algorithmType) {
        if ("DES".equalsIgnoreCase(algorithmType)) {
            return new DESAlgorithm();
        } else if ("IDEA".equalsIgnoreCase(algorithmType)) {
            return new IDEAAlgorithm();
        } else {
            throw new IllegalArgumentException("Unsupported algorithm type: " + algorithmType);
        }
    }
}

package org.example;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String algorithmType = "DES"; // 或者 "IDEA"
        EncryptionAlgorithm encryptionAlgorithm = EncryptionAlgorithmFactory.getEncryptionAlgorithm(algorithmType);

        byte[] data = "Hello, World!".getBytes();
        byte[] encryptedData = encryptionAlgorithm.encrypt(data);

        System.out.println("Encrypted Data: " + Arrays.toString(encryptedData));

    }
}

posted @   平安喜乐×  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示