All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luues.util.encryption.DesECBUtil Maven / Gradle / Ivy

There is a newer version: 1.3.0.5.RELEASE
Show newest version
package com.luues.util.encryption;

import com.luues.util.TypeConvert;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

public class DesECBUtil {

    private static final String key = "wudayeshinidie_sb_rnm";

    /**
     * 加密数据
     *
     * @param encryptString 需要加密的内容
     * @param encryptKey 秘钥
     * @return 加密后的数据
     * @throws Exception
     */
    public static String encryptDES(String encryptString, String encryptKey) throws Exception {
        if(TypeConvert.isNull(encryptKey)){
            encryptKey = key;
        }
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(getKey(encryptKey), "DES"));
        byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF-8"));
        return Base64.encodeBase64String(encryptedData);
    }

    /**
     * key 不足8位补位
     *
     * @param keyRule
     */
    public static byte[] getKey(String keyRule) {
        Key key = null;
        byte[] keyByte = keyRule.getBytes();
        // 创建一个空的八位数组,默认情况下为0
        byte[] byteTemp = new byte[8];
        // 将用户指定的规则转换成八位数组
        for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
            byteTemp[i] = keyByte[i];
        }
        key = new SecretKeySpec(byteTemp, "DES");
        return key.getEncoded();
    }

    /***
     * 解密数据
     * @param decryptString
     * @param decryptKey
     * @return
     * @throws Exception
     */

    public static String decryptDES(String decryptString, String decryptKey) throws Exception {
        if(TypeConvert.isNull(decryptKey)){
            decryptKey = key;
        }
        byte[] sourceBytes = Base64.decodeBase64(decryptString);
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(getKey(decryptKey), "DES"));
        byte[] decoded = cipher.doFinal(sourceBytes);
        return new String(decoded, "UTF-8");
    }

    public static void main(String[] args) throws Exception {
        /*System.err.println(URLEncoder.encode("\\/", "UTF-8"));
        String clearText = "194-72e01724d3317a904a6fc4564c5595d0.ts";
        String key = "fskjdfsi534534gfsgfd";//密钥
        System.out.println("明文:" + clearText + "\n密钥:" + key);
        String encryptText = encryptDES(clearText, key);
        System.out.println("加密后:" + encryptText);
        String decryptText = decryptDES(encryptText, key);
        System.out.println("解密后:" + decryptText);*/
        String n = "2345435/";
        System.err.println(n.substring(n.length() - 1));
        if(n.substring(n.length() - 1).equals("/")){
            n = n.substring(0, n.length() - 1) + ".qty";
        }
        System.err.println(n);
        if(n.contains(".qty")){
            n = n.split(".qty")[0] + "/";
        }
        System.err.println(n);
        System.err.println(decryptDES("KVpBwCEZQyZo2eTdyKVmfyxJ9ytrMX828LqHH0JsqKsfkvOwmrRFmA98ysfwXKAUlZeJQ0zDTr+got81B+8y2eUDTHg0Idb/", "wudayeshinidie_sb_rnm"));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy