TripleDESUtil工具类

1,引入pom.xml
 <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15to18</artifactId>
            <version>1.69</version>
            <scope>compile</scope>
        </dependency>

 


2,TripleDESUtil.java
工具类
package com.bsoft.hihis.mq.otherSystem.reportUrl.secret;


import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;

public class TripleDesUtils {

        static {
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        }

        private static final String Key = "eWorldTomTaw#7=*"; //默认密钥,具体可根据现场配置
        private static final String Vector = "TomTaw#7";  //默认偏移向量,具体可根据现场配置
        private static final String ENCODE = "utf-8";

        public static void main(String[] args) throws Exception {
            String jsonStr = "{\"ScheduleOperatorID\":\"10999\",\"ScheduleOperatorName\":\"陈医生\",\"MedRecNO\":\"123\",\"Timestamp\":\"1490415566\"}";
            String res = Encrypt(jsonStr);
            System.out.println(res);
        }


        public static String Encrypt(String plaintext) throws Exception {

            byte[] byteArray = plaintext.getBytes(ENCODE);

            byte[] bKey = Key.getBytes(ENCODE);

            if (bKey.length < 24) {
                int i = 0;
                byte[] tmp = new byte[24];
                for (byte b : bKey) {
                    tmp[i++] = b;
                }
                bKey = tmp;
            }
            SecretKey key3 = new SecretKeySpec(bKey, "DESede");
            //初始向量默认 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
            IvParameterSpec iv3 = new IvParameterSpec(Vector.getBytes(ENCODE));
            //DESede/CBC/ZeroBytePadding,DESede/ECB/NoPadding,DESede/CBC/NoPadding,DES/ECB/PKCS5Padding 根据具体情况来定,有些不需要使用到向量的,去掉向量
            Cipher cipher3 = Cipher.getInstance("DESede/CBC/PKCS7Padding");
            cipher3.init(Cipher.ENCRYPT_MODE, key3, iv3);

            byte[] bOut = cipher3.doFinal(byteArray);

            return Base64.encodeBase64String(bOut).replaceAll("\r", "").replaceAll("\n", "");
        }
    }

 

posted on 2024-10-29 15:24  让代码飞  阅读(29)  评论(0)    收藏  举报

导航

一款免费在线思维导图工具推荐:https://www.processon.com/i/593e9a29e4b0898669edaf7f?full_name=python