DES加密 util

package com.joyveb.common.utils;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

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

import org.apache.commons.codec.binary.Base64;

import com.joyveb.common.exception.DesException;

public class DESCoder {

    private final static String CHARSET = "utf-8";

    /**
     * 对cipherText进行DES解密
     *
     * @param cipherText
     * @return
     * @throws DesException
     */
    public static String desDecrypt(String cipherText, String desKey)
            throws DesException {
        String decryptStr = null;
        try {
            // 解密
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

            byte[] input = Base64.decodeBase64(cipherText.trim().getBytes(
                    CHARSET));

            cipher.init(Cipher.DECRYPT_MODE, genSecretKey(desKey));
            byte[] output = cipher.doFinal(input);
            decryptStr = new String(output, CHARSET);

        } catch (Exception e) {
            throw new DesException("DES解密发生异常!", e);
        }

        return decryptStr;
    }

    /**
     * 对message进行DES加密
     *
     * @param message
     * @return
     * @throws DesException
     */
    public static String desEncrypt(String message, String desKey)
            throws DesException {
        String encryptStr = null;
        byte encryptByte[];

        try {
            // 加密
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, genSecretKey(desKey));
            byte[] cipherText = cipher
                    .doFinal(message.trim().getBytes(CHARSET));
            encryptByte = Base64.encodeBase64(cipherText);
            encryptStr = new String(encryptByte, CHARSET);
            encryptStr = encryptStr.replaceAll("\r\n", "").replaceAll("\n", "");

        } catch (Exception e) {
            throw new DesException("des加密发生异常!", e);
        }
        return encryptStr;
    }

    private static SecretKey genSecretKey(String key)
            throws InvalidKeyException, NoSuchAlgorithmException,
            InvalidKeySpecException {
        DESKeySpec desKeySpec = new DESKeySpec(hexStringToByte(key));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

        return secretKey;
    }

    private static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值