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