package com.philisense.www.utils.des;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import java.util.Base64;
import java.util.Base64.Decoder;

public class DES {
    public String encriptData(String inStr, String keyStr) {
        SecureRandom sr = new SecureRandom();
        byte[] rawKeyData = keyStr.getBytes();
        String retStr = inStr;
        try {
            DESKeySpec dks = new DESKeySpec(rawKeyData);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(1, key, sr);
//            StringBufferInputStream sbis = new StringBufferInputStream(inStr);
            InputStream sbis = new ByteArrayInputStream(inStr.getBytes("UTF-8"));
            int inLen = sbis.available();
            int lastLen = inLen % 8;
            inLen = (inLen / 8 + 1) * 8;
            byte[] inByte = new byte[inLen];
            sbis.read(inByte);
            inByte[inLen - 1] = (byte)lastLen;
            byte[] outData = cipher.doFinal(inByte);
            byte[] outByte = new byte[outData.length - 8];
            for (int i = 0; i < outData.length - 8; i++)
                outByte[i] = outData[i];
            retStr = encodeBASE64(outByte);
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return retStr;
    }

    public String decriptData(String inStr, String keyStr) {
        SecureRandom sr = new SecureRandom();
        byte[] rawKeyData = keyStr.getBytes();
        String retStr = inStr;
        try {
            DESKeySpec dks = new DESKeySpec(rawKeyData);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            byte[] inByte = decodeBASE64(inStr);
            int inLen = inByte.length;
            byte[] outData = new byte[inLen + 8];
            for (int i = 0; i < inLen; i++)
                outData[i] = inByte[i];
            cipher.init(1, key, sr);
            byte[] outByte_ = cipher.doFinal("\000\000\000\000\000\000\000\000".getBytes());
            for (int j = 0; j < 8; j++)
                outData[inLen + 7 - j] = outByte_[outByte_.length - 1 - j];
            cipher.init(2, key, sr);
            byte[] outByte = cipher.doFinal(outData);
            retStr = (new String(outByte)).trim();
        } catch (Exception ee) {
            ee.printStackTrace();
        }
        return retStr;
    }

    public static String encodeBASE64(byte[] s) {
        if (s == null)
            return null;
//        return (new BASE64Encoder()).encode(s);
        return Base64.getEncoder().encodeToString(s);
    }

    public static byte[] decodeBASE64(String s) {
        if (s == null)
            return null;
//        BASE64Decoder decoder = new BASE64Decoder();
        Decoder decoder = Base64.getDecoder();
        try {
            byte[] b = decoder.decode(s);
            return b;
        } catch (Exception e) {
            return null;
        }
    }
}
