12.13

 

package com.itheima.test3;



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 begincode = "人生苦短及时行乐";
        String endcode = null;
        String opencode = null;
        System.out.println("要加密的明文" + begincode);
        String cipherType = "DESede";
        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("要加密的字符串是:" + begincode);
            byte[] codeStringByte = begincode.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("");
            endcode = new String(codeStringByteEnd);
            System.out.println("加密后的字符串是:" + endcode);
            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("");
            opencode = new String(decodeStringByteEnd);
            System.out.println("解密后的字符串是:" + opencode);
            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());
        }
    }

}

 

posted @   秋渡晚枫  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示