首页 > 其他分享 >工厂方法模式

工厂方法模式

时间:2023-11-09 22:55:05浏览次数:36  
标签:String DES IDEA 模式 工厂 Cipher 方法 public cipher

[实验任务]:加密算法

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

 

public interface Encryption
{
    String encrypt(String text);

    String decrypt(String ciphertext);
}
public class DES implements Encryption
{
    private SecretKey secretKey;

    public DES()
    {
        try
        {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
            secretKey = keyGenerator.generateKey();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    //DES加密
    @Override
    public String encrypt(String text)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedBytes = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    //DES解密
    @Override
    public String decrypt(String ciphertext)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(ciphertext));
            return new String(decryptedBytes, StandardCharsets.UTF_8);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
public class IDEA implements Encryption
{
    private SecretKey secretKey;

    static
    {
        // 添加Bouncy Castle提供程序
        Security.addProvider(new BouncyCastleProvider());
    }

    public IDEA()
    {
        try
        {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("IDEA", "BC");
            secretKey = keyGenerator.generateKey();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public String encrypt(String text)
    {
        try
        {
            // 创建一个IDEA加密器
            Cipher cipher = Cipher.getInstance("IDEA/ECB/PKCS5Padding", "BC");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] encryptedBytes = cipher.doFinal(text.getBytes());
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String decrypt(String ciphertext)
    {
        try
        {
            Cipher cipher = Cipher.getInstance("IDEA/ECB/PKCS5Padding", "BC");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] encryptedBytes = Base64.getDecoder().decode(ciphertext);
            return new String(cipher.doFinal(encryptedBytes), StandardCharsets.UTF_8);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
}
public interface EncryptionFactory
{
    Encryption createEncryption();
}
public class DESFactory implements EncryptionFactory
{
    @Override
    public Encryption createEncryption()
    {
        return new DES();
    }
}
public class IDEAFactory implements EncryptionFactory
{
    @Override
    public Encryption createEncryption()
    {
        return new IDEA();
    }
}
import java.util.Scanner;

public class FactoryMethod
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要加密的文本:");
        String text = sc.next();

        EncryptionFactory factory = null;

        System.out.println("请输入加密算法编号:\n" +
                "1、DES加密  2、IDEA加密");
        int choose = sc.nextInt();
        if (choose == 1)
        {
            //DES加密
            factory = new DESFactory();
            System.out.println("DES加密");
        } else if (choose == 2)
        {
            //IDEA加密
            factory = new IDEAFactory();
            System.out.println("IDEA加密");
        }

        Encryption encryptionAlgorithm = factory.createEncryption();
        String encryptedText = encryptionAlgorithm.encrypt(text);
        String decryptedText = encryptionAlgorithm.decrypt(encryptedText);
        System.out.println("原文本:" + text);
        System.out.println("加密后:" + encryptedText);
        System.out.println("解密后:" + decryptedText);
    }
}

运行结果

 

 

标签:String,DES,IDEA,模式,工厂,Cipher,方法,public,cipher
From: https://www.cnblogs.com/mendianyu/p/17823080.html

相关文章

  • 一种在 ABAP 端扩展 SAP Fiori 应用的方法介绍
    有朋友在我的知识星球提问:HCMFAB_COMMON这个lib已经被很多app消费了,我想对HAMFAB_COMMON做一点扩展,希望原先消费它的app能使用最新的功能。有个群友给出了解答.StackOverflow的帖子:CansomeonepleaseadviseontheissueIamfacing.IamtryingtoextendaStand......
  • python升级到3.12版本, 开发模式安装包, 在vscode下Pylance无法解析的问题处理
    问题描述在python3.11版本下开发python库,为方便调测,使用开发模式安装包.使用如下命令:pipinstall-e.其中-e参数表示开发模式下安装python包,它并没有把包相关文件拷贝到site-packages目录,而是创建一个链接指向当前的开发库.这样,当自己开发的包修改后会立即生效,......
  • C#的DataTable排序各种方法
    在很多时候,我们做datatable排序的时候,在string类型 需要进行数字转换在排序的时候,就不能用自带的,dtTemplate.DefaultView.Sort办法,这时候的排序需要先转换类型,在进行排序。下面介绍几种转换类型后排序的方法:第一种:     publicDataTableLinqSortDataTable(DataT......
  • 泛型方法
     泛型方法   泛型方法是使用类型参数声明的方法。staticvoidSwap<T>(refTlhs,refTrhs){  Ttemp;  temp=lhs;  lhs=rhs;  rhs=temp;}类型推断   相同的类型推断规则也适用于静态方法以及实例方法。编译器能够根据传入的方法......
  • Go 接口:Go中最强大的魔法,接口应用模式或惯例介绍
    Go接口:Go中最强大的魔法,接口应用模式或惯例介绍目录Go接口:Go中最强大的魔法,接口应用模式或惯例介绍一、前置原则二、一切皆组合2.1一切皆组合2.2垂直组合2.2.1第一种:通过嵌入接口构建接口2.2.2第二种:通过嵌入接口构建结构体类型2.2.3第三种:通过嵌入结构体类型构建新结构......
  • WPF控件,按钮名称分行显示的方法
    1、利用XML规则下的特殊字符和空格下面的字符在[XML]中被定义为空白(whitespace)字符: 空格【】Tab 【】回车 【】换行【】这里,为了实现分行,我们选择最后一个换行。比如:<ButtonWidth="100" Height="50" Click="Button_Click_2" Content="第一行&#x000A......
  • final(最终)、final修饰类、final修饰方法、final修饰变量
          ......
  • importlib模块—— 另一种导入文件的方法
    正常我们想要从文件myfile导入模块b时,我们会使用:frommyfileimportb接下来介绍另一种导入方式,能够用字符串来导入模块:#首先导入importlib模块importimportlibres='myflie.b'importlib.import_module(res)#就等同于frommyfileimportb总......
  • 方法|基于T507开发板讲如何将占用引脚配置为普通GPIO
    方法|基于T507开发板讲如何将占用引脚配置为普通GPIO根据T5用户手册描述,只有在不使用多路复用功能的情况下,这些端口才可以配置为GPIO,所以我们需要把引脚的复用功能关闭。将被占用引脚配置为普通1.查看引脚默认功能配置2.关闭引脚默认功能配置以UART4-TX的PI13为例,这是PI13引脚复用......
  • flutter 运行的四种模式
    Debug(调试)模式1、这是开发Flutter应用程序时最常用的模式。可以在真机和模拟器上同时运行。重要用于开发和调试阶段。2、可以进行热重载,即在不重新启动应用程序的情况下实时更新代码。3、应用程序会包含用于调试和开发的额外信息和工具(debugging信息、debuggeraids(比如......