首页 > 其他分享 >10.25

10.25

时间:2024-10-27 22:30:58浏览次数:2  
标签:String 10.25 System println data public out

软件设计实验三

 

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

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

实验要求:

 

 

代码:

 

MethodFactory.java

package jiami;

public interface MethodFactory {

    public Method produceMethod();

}

 

MethodFactory.java

package jiami;

 

public class DESFactory implements MethodFactory {

    public DES produceMethod() {

        System.out.println("使用DES算法");

        return new DES();

    }

}

 

MethodFactory.java

package jiami;

 

public class IDEAFactory implements MethodFactory {

    public IDEA produceMethod() {

        System.out.println("使用IDEA算法");

        return new IDEA();

    }

}

 

Method.java

package jiami;

public interface Method {

    public abstract void work(String str, String password);

}

 

DES.java

package jiami;

 

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

 

public class DES implements Method {

 

    public void work(String str, String password) {

        String codeStringBegin = "信2205-3何不语"; // 要加密的明文

        String codeStringEnd = null; // 加密后的密文

        String decodeString = null; // 密文解密后得到的明文

        System.out.println("要加密的明文:" + codeStringBegin);

        String cipherType = "DESede"; // 加密算法类型,可设置为DES、DESede、AES等字符串

        try {

            // 获取密钥生成器

            KeyGenerator keyGen = KeyGenerator.getInstance(cipherType);

            // 初始化密钥生成器,不同的加密算法其密钥长度可能不同

            keyGen.init(112);

            // 生成密钥

            SecretKey key = keyGen.generateKey();

 

            // 得到密钥字节码

            byte[] keyByte = key.getEncoded();

            // 输出密钥的字节码

            System.out.println("密钥是:");

            for (int i = 0; i < keyByte.length; i++) {

                System.out.print(keyByte[i] + ",");

            }

            System.out.println("");

            // 创建密码器

            Cipher cp = Cipher.getInstance(cipherType);

            // 初始化密码器

            cp.init(Cipher.ENCRYPT_MODE, key);

            System.out.println("要加密的字符串是:" + codeStringBegin);

            byte[] codeStringByte = codeStringBegin.getBytes("UTF8");

            System.out.println("要加密的字符串对应的字节码是:");

            for (int i = 0; i < codeStringByte.length; i++) {

                System.out.print(codeStringByte[i] + ",");

            }

            System.out.println("");

            // 开始加密

            byte[] codeStringByteEnd = cp.doFinal(codeStringByte);

            System.out.println("加密后的字符串对应的字节码是:");

            for (int i = 0; i < codeStringByteEnd.length; i++) {

                System.out.print(codeStringByteEnd[i] + ",");

            }

            System.out.println("");

            codeStringEnd = new String(codeStringByteEnd);

            System.out.println("加密后的字符串是:" + codeStringEnd);

            System.out.println("");

            // 重新初始化密码器

            cp.init(Cipher.DECRYPT_MODE, key);

            // 开始解密

            byte[] decodeStringByteEnd = cp.doFinal(codeStringByteEnd);

            System.out.println("解密后的字符串对应的字节码是:");

            for (int i = 0; i < decodeStringByteEnd.length; i++) {

                System.out.print(decodeStringByteEnd[i] + ",");

            }

            System.out.println("");

            decodeString = new String(decodeStringByteEnd);

            System.out.println("解密后的字符串是:" + decodeString);

            System.out.println("");

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        System.out.println("DES加密算法");

        DES des = new DES();

        try {

            des.work("8787878787878787", "0E329232EA6D0D73");

 

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }

 

}

 

IDEA.java

package jiami;

 

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import org.apache.commons.codec.binary.Base64;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

 

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.spec.SecretKeySpec;

import java.security.Key;

import java.security.Security;

 

public class IDEA implements Method {

 

    public static final String KEY_ALGORITHM = "IDEA";

 

    public static final String CIPHER_ALGORITHM = "IDEA/ECB/ISO10126Padding";

 

    public static byte[] initkey() throws Exception {

        // 加入bouncyCastle支持

        Security.addProvider(new BouncyCastleProvider());

 

        // 实例化密钥生成器

        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);

        // 初始化密钥生成器,IDEA要求密钥长度为128位

        kg.init(128);

        // 生成密钥

        SecretKey secretKey = kg.generateKey();

        // 获取二进制密钥编码形式

        return secretKey.getEncoded();

    }

 

    /**

     * 转换密钥

     *

     * @param key

     *            二进制密钥

     * @return Key 密钥

     */

    private static Key toKey(byte[] key) throws Exception {

        // 实例化DES密钥

        // 生成密钥

        SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);

        return secretKey;

    }

 

    /**

     * 加密数据

     *

     * @param data

     *            待加密数据

     * @param key

     *            密钥

     * @return byte[] 加密后的数据

     */

    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {

        // 加入bouncyCastle支持

        Security.addProvider(new BouncyCastleProvider());

        // 还原密钥

        Key k = toKey(key);

        // 实例化

        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

        // 初始化,设置为加密模式

        cipher.init(Cipher.ENCRYPT_MODE, k);

        // 执行操作

        return cipher.doFinal(data);

    }

 

    /**

     * 解密数据

     *

     * @param data

     *            待解密数据

     * @param key

     *            密钥

     * @return byte[] 解密后的数据

     */

    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {

        // 加入bouncyCastle支持

        Security.addProvider(new BouncyCastleProvider());

        // 还原密钥

        Key k = toKey(key);

        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

        // 初始化,设置为解密模式

        cipher.init(Cipher.DECRYPT_MODE, k);

        // 执行操作

        return cipher.doFinal(data);

    }

 

    public static String getKey() {

        String result = null;

        try {

            result = Base64.encodeBase64String(initkey());

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result;

    }

 

    public static String ideaEncrypt(String data, String key) {

        String result = null;

        try {

            byte[] data_en = encrypt(data.getBytes(), Base64.decodeBase64(key));

            result = Base64.encodeBase64String(data_en);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result;

    }

 

    public static String ideaDecrypt(String data, String key) {

        String result = null;

        try {

            byte[] data_de = decrypt(Base64.decodeBase64(data), Base64.decodeBase64(key));

            ;

            result = new String(data_de);

        } catch (Exception e) {

            e.printStackTrace();

        }

        return result;

    }

 

    public void work(String str, String password) {

        String data = "20223735何不语";

        String key = getKey();

        System.out.println("要加密的原文:" + data);

        System.out.println("密钥:" + key);

        String data_en = ideaEncrypt(data, key);

        System.out.println("密文:" + data_en);

        String data_de = ideaDecrypt(data_en, key);

        System.out.println("原文:" + data_de);

    }

 

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        System.out.println("IDEA加密算法");

        IDEA idea = new IDEA();

        try {

            idea.work("8787878787878787", "0E329232EA6D0D73");

        } catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }

 

}

zhuhanshu.java

package jiami;

 

import java.util.Scanner;

 

public class zhuhanshu {

 

    public static void main(String[] args) {

 

        DES des = new DES();

        IDEA idea = new IDEA();

        try {

            int n = 0;

 

            Scanner in = new Scanner(System.in);

            while (n != 3) {

                System.out.println("请选择要使用的加密算法 1.DES加密算法 2.IDEA加密算法");

                System.out.println("3.退出");

                System.out.println("请选择:");

                if (in.hasNextInt()) {

                    n = in.nextInt();

                } else {

                    System.out.println("输入的不是整数,请重新输入:");

                    continue;

                }

                switch (n) {

                case 1: {

 

                    des.work("1787878787878787", "0E329232EA6D0D73");

                    break;

                }

                case 2: {

                    idea.work("8787878787878787", "0E329232EA6D0D73");

                    break;

                }

                }

            }

        }

 

        catch (Exception e) {

            System.out.println(e.getMessage());

        }

    }

}

 

 

标签:String,10.25,System,println,data,public,out
From: https://www.cnblogs.com/zzqq1314/p/18509169

相关文章

  • java学习10.25企业ERP生产计划管理系统(20分)
    今天成功的把这个项目写好了,就是文档中的一些具体的需求比较难写(由于之前没写过)所以项目能完成基本的增删改查和浏览操作。使用技术栈mybatis+thymeleft+mysql比较复杂,写的东西比较多,以后学了springboot会更简便一些整体架构具体页面源代码通过百度网盘分享的文件:企......
  • 10.25Python_pandas_函数(1)
    二、函数1、常用的统计学函数函数名称描述说明count()统计某个非空值的数量sum()求和mean()求均值median()求中位数std()求标准差min()求最小值max()求最大值abs()求绝对值prod()求所有数值的乘积案例:#创建一个示例DataFramedata={'A':[1,2,3,4,5],......
  • 杂题随笔 2024.10.25 两道图论
    最近在写abc375这场,最后的F和G是两道很典的图论题。https://atcoder.jp/contests/abc375/tasks/abc375_f题意:大致就是说给你一张图,有两种操作:第一种操作是把第i条边删掉,第二种操作是询问u,v两点的最短路。解法:这种题目印象里好像是考过几次了,把在线询问的顺序转变,倒着做,把减边......
  • 10.25
    实验2:简单工厂模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解简单工厂模式的动机,掌握该模式的结构;2、能够利用简单工厂模式解决实际问题。[实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则......
  • 活动报名丨实时互动AI 开发者年度聚会!RTE Open Day@RTE2024,10.25/26北京
         2024年10月25日~26日,由声网和RTE开发者社区联合主办的RTE2024第十届实时互联网大会将在北京·悠唐皇冠假日酒店正式开启! 大会以「AI爱」为主题,推出覆盖实时互联网全生态的论坛及周边活动共计20余场。 RTEOpenDay携手18家展区伙伴齐聚大......
  • 10.25随笔
    特殊条件1.空值判断:isnullSelect*fromempwherecommisnull;查询emp表中comm列中的空值。2.betweenand(在之间的值)Select*fromempwheresalbetween1500and3000;查询emp表中SAL列中大于1500的小于3000的值。注意:大于等于1500且小于等于30......
  • 10.25
    今日学习内容<%--CreatedbyIntelliJIDEA.TochangethistemplateuseFile|Settings|FileTemplates.--%><%@pagecontentType="text/html;charset=UTF-8"language="java"%><html><head><title>Title</ti......
  • 10.25日
    早八英语课,第一节课是同学的演讲展示,不过今天的两位同学占用时间比较长,熬过了一节课,第二节课讲了英语作文中对比的写法,课下要求使用对比写法写一篇作文。 ......
  • 大二快乐日记10.25
    匹配优先级Servlet虚拟路径的匹配优先级顺序为:全路径匹配(精确匹配)>目录匹配>扩展名匹配>缺省匹配(默认匹配)。Servlet容器会从优先级高的虚拟路径开始匹配,匹配成功后就会立刻将请求交给相应的Servlet进行处理,不会再关注其他虚拟路径是否匹配成功。......
  • linux10.25课堂随笔
    一.文件权限4.3.1SUID权限1.使用ll命令查看/usr/bin/passwd文件,第一行的第四个字符为“s”。“s”表示特殊权限SUID。2.普通用户可通过SUID提权,使用chmod命令给user增加SUID权限。4.3.2SGID权限1.在一个程序上添加SGID,用户在执行过程中会获得该程序用户组的权限(相当于临时......