azure011328

导航

 

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

 

实验3:工厂方法模式

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

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

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

 
   

 


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

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

实验要求:

1.画出对应的类图;

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

3.注意编程规范。

 

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.security.SecureRandom;

import java.util.Base64;

 

// 加密算法接口

interface Encryption {

    String encrypt(String data);

    String decrypt(String data);

}

 

// DES加密算法实现

class DES implements Encryption {

    private static final String DES = "DES";

 

    @Override

    public String encrypt(String data) throws Exception {

        SecureRandom random = new SecureRandom();

        KeyGenerator keyGenerator = KeyGenerator.getInstance(DES);

        keyGenerator.init(random);

        SecretKey secretKey = keyGenerator.generateKey();

        byte[] key = secretKey.getEncoded();

        SecretKeySpec keySpec = new SecretKeySpec(key, DES);

        Cipher cipher = Cipher.getInstance(DES);

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] encrypted = cipher.doFinal(data.getBytes());

        return Base64.getEncoder().encodeToString(encrypted);

    }

 

    @Override

    public String decrypt(String data) throws Exception {

        SecureRandom random = new SecureRandom();

        KeyGenerator keyGenerator = KeyGenerator.getInstance(DES);

        keyGenerator.init(random);

        SecretKey secretKey = keyGenerator.generateKey();

        byte[] key = secretKey.getEncoded();

        SecretKeySpec keySpec = new SecretKeySpec(key, DES);

        Cipher cipher = Cipher.getInstance(DES);

        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(data));

        return new String(decrypted);

    }

}

 

// IDEA加密算法实现

class IDEA implements Encryption {

    private static final String IDEA = "IDEA";

 

    @Override

    public String encrypt(String data) throws Exception {

        SecureRandom random = new SecureRandom();

        KeyGenerator keyGenerator = KeyGenerator.getInstance(IDEA);

        keyGenerator.init(random);

        SecretKey secretKey = keyGenerator.generateKey();

        byte[] key = secretKey.getEncoded();

        SecretKeySpec keySpec = new SecretKeySpec(key, IDEA);

        Cipher cipher = Cipher.getInstance(IDEA);

        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] encrypted = cipher.doFinal(data.getBytes());

        return Base64.getEncoder().encodeToString(encrypted);

    }

 

    @Override

    public String decrypt(String data) throws Exception {

        SecureRandom random = new SecureRandom();

        KeyGenerator keyGenerator = KeyGenerator.getInstance(IDEA);

        keyGenerator.init(random);

        SecretKey secretKey = keyGenerator.generateKey();

        byte[] key = secretKey.getEncoded();

        SecretKeySpec keySpec = new SecretKeySpec(key, IDEA);

        Cipher cipher = Cipher.getInstance(IDEA);

        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(data));

        return new String(decrypted);

    }

}

 

// 加密算法工厂接口

interface EncryptionFactory {

    Encryption createEncryption();

}

 

// DES加密算法工厂

class DESFactory implements EncryptionFactory {

    @Override

    public Encryption createEncryption() {

        return new DES();

    }

}

 

// IDEA加密算法工厂

class IDEAFactory implements EncryptionFactory {

    @Override

    public Encryption createEncryption() {

        return new IDEA();

    }

}

 

// 主类

public class FactoryMethodPatternDemo {

    public static void main(String[] args) {

        try {

            EncryptionFactory factory;

 

            factory = new DESFactory();

            Encryption desEncryption = factory.createEncryption();

            System.out.println("Original: Hello World!");

            System.out.println("DES Encrypted: " + desEncryption.encrypt("Hello World!"));

            System.out.println("DES Decrypted: " + desEncryption.decrypt(desEncryption.encrypt("Hello World!")));

 

            factory = new IDEAFactory();

            Encryption ideaEncryption = factory.createEncryption();

            System.out.println("Original: Hello World!");

            System.out.println("IDEA Encrypted: " + ideaEncryption.encrypt("Hello World!"));

            System.out.println("IDEA Decrypted: " + ideaEncryption.decrypt(ideaEncryption.encrypt("Hello World!")));

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}

posted on 2024-11-04 16:55  淮竹i  阅读(2)  评论(0编辑  收藏  举报