DES加密之3DES

3DES 算法 3DES算法顾名思义就是3次DES算法。

public class DES3 {

    private static final String CRYPT_ALGORITHM = "DESede";

    /**
     * 3DES加密模式
     */
    public static String encrypt(String value, String key) {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), CRYPT_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CRYPT_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            byte[] encryptedByte = cipher.doFinal(value.getBytes());
            String encodedByte = byte2hex(encryptedByte);
            return encodedByte;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    
    /**
     * 3DES加密
     *
     * @param value 加密数据
     * @param key 加密密钥
     * @param charset 编码格式,不传则为系统自带
     * @return
     */
    public static byte[] encrypt(String value, String key, String charset) {
        try {
            byte[] keybyte;
            byte[] valuebyte;
            if (charset != null && charset.length() > 0) {
                keybyte = key.getBytes(charset);
                valuebyte = value.getBytes(charset);
            } else {
                keybyte = key.getBytes();
                valuebyte = value.getBytes();
            }
            SecretKeySpec keySpec = new SecretKeySpec(keybyte, CRYPT_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CRYPT_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, keySpec);
            byte[] encryptedByte = cipher.doFinal(valuebyte);
            return encryptedByte;
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
            e.getMessage();
            return null;
        }
    }
    /**
     * 
     * @param valuebyte
     * @param key
     * @param charset
     * @return
     */
    public static byte[] decrypt(byte[] valuebyte, String key, String charset) {
        try {
            byte[] keybyte;
            if (charset != null && charset.length() > 0) {
                keybyte = key.getBytes(charset);
            } else {
                keybyte = key.getBytes();
            }
            SecretKeySpec keySpec = new SecretKeySpec(keybyte, CRYPT_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CRYPT_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            byte[] decryptedByte = cipher.doFinal(valuebyte);
            return decryptedByte;
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
          e.getMessage();
            return null;
        }
    }
    /**
     * 
     * @param src
     * @param key
     * @return
     * @throws Exception
     */
    public static String desEncrypt(String src, String key, String charset) throws Exception {
                SecretKey secretKey = new SecretKeySpec(build3DesKey(key, charset),
                                CRYPT_ALGORITHM);
                Cipher cipher = Cipher.getInstance(CRYPT_ALGORITHM);
                cipher.init(Cipher.ENCRYPT_MODE, secretKey);
                byte[] b = cipher.doFinal(src.getBytes(charset));

                return byte2HexStr(b);
        }
    
    /**
     * 
     * @param dest
     * @param key
     * @return
     * @throws Exception
     */
    public static String desDecrypt(String dest, String key, String charset) throws Exception {
                SecretKey secretKey = new SecretKeySpec(build3DesKey(key, charset),
                                CRYPT_ALGORITHM);
                Cipher cipher = Cipher.getInstance(CRYPT_ALGORITHM);
                cipher.init(Cipher.DECRYPT_MODE, secretKey);
                byte[] b = cipher.doFinal(str2ByteArray(dest));

                return new String(b, charset);
        }
    
    /**
     * 
     * @param keyStr
     * @param charset
     * @return
     * @throws Exception
     */
    private static byte[] build3DesKey(String keyStr,String charset) throws Exception {
                byte[] key = new byte[24];
                byte[] temp = keyStr.getBytes(charset);
                if (key.length > temp.length) {
                        System.arraycopy(temp, 0, key, 0, temp.length);
                } else {
                        System.arraycopy(temp, 0, key, 0, key.length);
                }

                return key;
        }
    
    /**
     * 二进制转十六进制字符串。每一个字节转为两位十六进制字符串。
     * @param b
     * @return
     */
    private static String byte2HexStr(byte[] b) {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < b.length; i++) {
                        String s = Integer.toHexString(b[i] & 0xFF);
                        if (s.length() == 1) {
                                sb.append("0");
                        }

                        sb.append(s.toUpperCase());
                }

                return sb.toString();
        }
    /**
     * 
     * @param s
     * @return
     */
    private static byte[] str2ByteArray(String s) {
                int byteArrayLength = s.length() / 2;
                byte[] b = new byte[byteArrayLength];
                for (int i = 0; i < byteArrayLength; i++) {
                        byte b0 = (byte) Integer.valueOf(s.substring(i * 2, i * 2 + 2), 16)
                                        .intValue();
                        b[i] = b0;
                }

                return b;
        }

    public static void main(String[] args) {
<span style="white-space:pre">	</span>//String password = "30BOgfz4vcEu7h6TjpYPa1EJ";
<span style="white-space:pre">	</span>//String msg = "3DES加密解密案例";
<span style="white-space:pre">	</span>//System.out.println("BEFOR:" + encrypt(msg, password));

        //加密
        //byte[] secretArr = ThreeDES.encryptMode(msg, password);
        //System.out.println("AFTER:" + new String(secretArr));
        //解密
        //byte[] myMsgArr = ThreeDES.decryptMode(secretArr, password);
        //System.out.println("DE AFTER:" + new String(myMsgArr));
        String key = "88888888";
                String username = " tianqiao";
                String pwd = "FE2E4F68DEE1CF47E04815E6549F9D61";
                String product = "ID100001";
                String param = "cxcx,110108196708179712";
                String charSet = "UTF-8";
                
                String str2 = "2C36EDC3C0CA60C426F22E3BDE8BB146FA8178563EF6BEBB8CD8F299E85FD9A67B14983D95B16BF5633E1DE7CB7B2460F7EDB2D3B387770EFC441383D23D0D8D135C94BBDEA78B2E52C5759CE9AA9CC2929EF02DE08F6593B053AD070299E239254EBC0B766BB317C1C007E900A493EF37511A96C959F587737A75EA5774E2AB978DCE8671D0D0E91B71171BF6680C39667F1B9428D66618A6A5841EC5AC8AE5";
                try {
                        String ename = ThreeDES.desEncrypt(username, key, charSet);
                        String dname = ThreeDES.desDecrypt(ename, key, charSet);
                        String epwd = ThreeDES.desEncrypt(pwd, key, charSet);
                        String dpwd = ThreeDES.desDecrypt(epwd, key, charSet);
                        String eproduct = ThreeDES.desEncrypt(product, key, charSet);
                        String dproduct = ThreeDES.desDecrypt(eproduct, key, charSet);
                        String eparam = ThreeDES.desEncrypt(param, key, charSet);
                        String dparam = ThreeDES.desDecrypt(eparam, key, charSet);
                        System.out.println("密文:"+ename+"---->明文:"+dname);
                        System.out.println("密文:"+epwd+"---->明文:"+dpwd);
                        System.out.println("密文:"+eproduct+"---->明文:"+dproduct);
                        System.out.println("密文:"+eparam+"---->明文:"+dparam);
                        /*System.out.println(ThreeDES.desEncrypt(product, key, charSet));
                        System.out.println(ThreeDES.desEncrypt(param, key, charSet));
                        System.out.println(ThreeDES.desDecrypt(str2, key, charSet));*/
                        
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值