package com.duben.microtribe.utils;


import android.util.Log;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * 描述：3DES加密工具类
 * 作者：孟崔广
 * 时间：2018/3/29 17:51
 * 邮箱：mengcga@163.com
 */
public class Des3 {
    //	crediteasec@lx100$#365#$
    // 密钥
//    private final static String secretKey = "mail.asiainfo.com@2x222$#bbb#2";
    // 向量
    private final static String iv = "01234567";
    // 加解密统一使用的编码方式
    private final static String encoding = "utf-8";
    private static String TAG = "Des3";

    /**
     * 3DES加密
     *
     * @param plainText 普通文本
     * @return
     * @throws Exception
     */
    public static String encode(String plainText, String secretKey) {
        String ciphertext = "";
        if (plainText != null && plainText.length() != 0) {
            Key deskey = null;
            byte[] encryptData = null;
            try {
                byte[] bytes = secretKey.getBytes();
                DESedeKeySpec spec = new DESedeKeySpec(bytes);
                SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
                deskey = keyfactory.generateSecret(spec);
                Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
                IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
                cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
                encryptData = cipher.doFinal(plainText.getBytes(encoding));
                ciphertext = Base64.encode(encryptData);
                //这里需要对特殊字符串进行转码
                ciphertext = URLEncoder.encode(ciphertext, "utf-8");
            } catch (Exception e) {
                Log.e(TAG, e.getMessage().toString());
                e.printStackTrace();
            }
        }


        return ciphertext;
    }

    /**
     * 3DES解密
     *
     * @param encryptText 加密文本
     * @return
     * @throws Exception
     */
    public static String decode(String encryptText, String secretKey) {
        String clearText = "";
        if (encryptText != null && encryptText.length() != 0) {
            Key deskey = null;
            byte[] decryptData;
            try {
                //这里需要对特殊字符串进行转码
                encryptText = URLDecoder.decode(
                        encryptText, "utf-8");
                DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
                SecretKeyFactory keyfactory = SecretKeyFactory
                        .getInstance("desede");
                deskey = keyfactory.generateSecret(spec);
                Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
                IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
                cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
                decryptData = cipher.doFinal(Base64.decode(encryptText));
                clearText = new String(decryptData, encoding);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return clearText;
    }
}
