3DES加解密/PKCS5Padding/PKCS7Padding

1 说明

在这里插入图片描述

2 完整代码

public static void main(String[] args) throws IOException {
        String str = des3EncodeECB("Key3Des","@##h586ellAAon你好");
        System.out.println(str);
        System.out.println(des3DecodeECB("Key3Des",str));
    }

    private static final String DES3 = "DESede";
    /**
     * 3DES ECB模式加密
     */
    public static String des3EncodeECB(String Key, String Data) {
        try {
            byte[] data=Data.getBytes("UTF-8");
            byte[] key = build3DesKey(Key);
            SecretKey DESKey = new SecretKeySpec(key, DES3);    //生成密钥
            Cipher cipher = Cipher.getInstance(DES3 + "/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, DESKey);
            return new BASE64Encoder().encode(cipher.doFinal(data));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 3DES ECB模式解密
     */
    public static String des3DecodeECB(String Key, String Data) {
        try {
            //--通过base64,将字符串转成byte数组
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] data = decoder.decodeBuffer(Data);
            byte[] key = build3DesKey(Key);

            SecretKey DESKey = new SecretKeySpec(key, DES3);    //生成密钥
            Cipher cipher = Cipher.getInstance(DES3 + "/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, DESKey);
            byte[] bout=cipher.doFinal(data);

            return new String(bout,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 根据字符串生成密钥字节数组
     *
     * @param keyStr 密钥字符串
     */
    private static byte[] build3DesKey(String keyStr) {
        try {
            byte[] key = new byte[24];    //声明一个24位的字节数组,默认里面都是0
            byte[] temp = keyStr.getBytes("UTF-8");    //将字符串转成字节数组
            if (key.length > temp.length) {
                //如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
                System.arraycopy(temp, 0, key, 0, temp.length);
            } else {
                //如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
                System.arraycopy(temp, 0, key, 0, key.length);
            }
            return key;
        } catch (Exception  e) {
            e.printStackTrace();
            return null;
        }
    }

3 验证

在这里插入图片描述

4 更换为PKCS7Padding

Maven

		<dependency>
			<groupId>org.bouncycastle</groupId>
			<artifactId>bcprov-jdk15on</artifactId>
			<version>1.56</version>
		</dependency>

添加 默认加密提供者

  static {    
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }
package com.example.demo.tools;


import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

public class Des3 {
    public static void main(String[] args) throws IOException {
        String str = des3EncodeECB("Key3Des","@##h586ellAAon你好");
        System.out.println(str);
        System.out.println(des3DecodeECB("Key3Des",str));
    }
    static {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }
    private static final String DES3 = "DESede";
    /**
     * 3DES ECB模式加密
     */
    public static String des3EncodeECB(String Key, String Data) {
        try {
            byte[] data=Data.getBytes("UTF-8");
            byte[] key = build3DesKey(Key);
            SecretKey DESKey = new SecretKeySpec(key, DES3);    //生成密钥
            Cipher cipher = Cipher.getInstance(DES3 + "/ECB/PKCS7Padding");
            cipher.init(Cipher.ENCRYPT_MODE, DESKey);
            return new BASE64Encoder().encode(cipher.doFinal(data));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 3DES ECB模式解密
     */
    public static String des3DecodeECB(String Key, String Data) {
        try {
            //--通过base64,将字符串转成byte数组
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] data = decoder.decodeBuffer(Data);
            byte[] key = build3DesKey(Key);

            SecretKey DESKey = new SecretKeySpec(key, DES3);    //生成密钥
            Cipher cipher = Cipher.getInstance(DES3 + "/ECB/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, DESKey);
            byte[] bout=cipher.doFinal(data);

            return new String(bout,"UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 根据字符串生成密钥字节数组
     *
     * @param keyStr 密钥字符串
     */
    private static byte[] build3DesKey(String keyStr) {
        try {
            byte[] key = new byte[24];    //声明一个24位的字节数组,默认里面都是0
            byte[] temp = keyStr.getBytes("UTF-8");    //将字符串转成字节数组
            if (key.length > temp.length) {
                //如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
                System.arraycopy(temp, 0, key, 0, temp.length);
            } else {
                //如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
                System.arraycopy(temp, 0, key, 0, key.length);
            }
            return key;
        } catch (Exception  e) {
            e.printStackTrace();
            return null;
        }
    }
}

5 参考文献

https://blog.csdn.net/weixin_43272781/article/details/107330649

https://www.jianshu.com/p/028fe67e6c58

http://tool.chacuo.net/cryptdes查阅网站
公众号地址

博客地址

https://blog.csdn.net/weixin_41563161

公众号

博客地址

https://blog.csdn.net/weixin_41563161
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haikuotiankongdong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值