工具类:DESEncrypt加密

import java.io.UnsupportedEncodingException;
import java.util.Random;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESEncrypt {

    /**
     * 加密
     *
     * @param s
     * @return
     */
    public static String encrypt(String s) {
        return encrypt(s, null);
    }

    /**
     * 解密
     *
     * @param s
     * @return
     */
    public static String decrypt(String s) {
        return decrypt(s, null);
    }

    public static byte[] encrypt(byte[] s, byte[] key) {
        return encrypt(s, 1, key);
    }

    public static byte[] encrypt(byte[] s) {
        return encrypt(s, null);
    }

    public static byte[] decrypt(byte[] s, byte[] key) {
        return encrypt(s, 2, key);
    }

    public static byte[] decrypt(byte[] s) {
        return decrypt(s, null);
    }

    public static String encrypt(String s, String key) {
        if (s == null) {
            return null;
        }
        byte[] src = string2Bytes(s);
        byte[] b = encrypt(src, key == null ? null : string2Bytes(key));

        StringBuffer sb = new StringBuffer(1024);
        if ((b == null) || (b.length < 1)) {
            return null;
        }
        Random r = new Random(s.length());
        for (int i = 0; i < b.length; i++) {
            char c = (char) (r.nextInt(10) + 71);
            sb.append(c);
            if (b[i] < 0) {
                c = (char) (r.nextInt(10) + 81);
                b[i] = ((byte) -b[i]);
                sb.append(c);
            }
            sb.append(Integer.toString(b[i], 16).toUpperCase());
        }
        sb.deleteCharAt(0);
        return sb.toString();
    }

    public static String decrypt(String s, String key) {
        if (s.length() < 1) {
            return null;
        }
        String[] sByte = s.split("[G-Pg-p]");
        byte[] b = new byte[sByte.length];
        for (int i = 0; i < b.length; i++) {
            char c = sByte[i].charAt(0);
            if (((c >= 'Q') && (c <= 'Z')) || ((c >= 'q') && (c <= 'z'))) {
                b[i] = ((byte) -Byte.parseByte(sByte[i].substring(1), 16));
            } else {
                b[i] = Byte.parseByte(sByte[i], 16);
            }
        }
        byte[] ch = decrypt(b, key == null ? null : string2Bytes(key));
        if ((ch == null) || (ch.length < 1)) {
            return null;
        }
        return bytes2String(ch);
    }

    private static byte[] encrypt(byte[] s, int mode, byte[] salt) {
        if ((salt == null) || (salt.length < 8)) {
            byte[] nsalt = { -57, 115, 33, -116, 126, -56, -18, -103 };
            if (salt != null) {
                for (int i = 0; i < salt.length; i++) {
                    nsalt[i] = salt[i];
                }
            }
            salt = nsalt;
        }
        byte[] ciphertext;
        try {
            SecretKeyFactory keyFac = SecretKeyFactory.getInstance("DES");
            DESKeySpec desKeySpec = new DESKeySpec(salt);
            SecretKey desKey = keyFac.generateSecret(desKeySpec);

            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

            cipher.init(mode, desKey);

            ciphertext = cipher.doFinal(s);
        } catch (Exception e) {
            ciphertext = null;
        }
        return ciphertext;
    }

    private static byte[] string2Bytes(String str) {
        byte[] sb = null;
        try {
            sb = str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            sb = str.getBytes();
        }
        return sb;
    }

    private static String bytes2String(byte[] bytes) {
        String s = null;
        try {
            s = new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            s = new String(bytes);
        }
        return s;
    }

}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值