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

com.loocme.security.encrypt.DesUtil Maven / Gradle / Ivy

package com.loocme.security.encrypt;

import lombok.extern.slf4j.Slf4j;

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * @author liuchi
 */
@Slf4j
public class DesUtil
{

    /**
     * 解密
     *
     * @param message
     * @param key
     * @return str
     */
    public static String decryptAngelpay(String message, String key)
    {
        try
        {
            byte[] desiv = new byte[8];
            byte[] bytesrc = Base64.decode(message.getBytes());
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("utf-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(desiv);
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte, "utf-8");
        }
        catch (Exception e)
        {
            log.error("", e);
            return null;
        }
    }

    /**
     * 加密
     *
     * @param message
     * @param key
     * @return str
     */
    public static String encryptAngelpay(String message, String key)
    {
        try
        {
            byte[] desiv = new byte[8];
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("utf-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            AlgorithmParameterSpec iv = new IvParameterSpec(desiv);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            byte[] encryptbyte = cipher.doFinal(message.getBytes("utf-8"));
            return new String(Base64.encode(encryptbyte));
        }
        catch (Exception e)
        {
            log.error("", e);
            return null;
        }
    }

    public static void main(String[] args)
    {
        String str = "";
        System.out.println(decryptAngelpay(str, "11111111"));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy