package com.zztrapp.zztrapp.utilcode.mUtil;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class EncryptUtil {

	private static String key = "wrdkdfsdfds!@#ff";

	/**
	 * 加密密码
	 * @param input  源数据
	 * @return
	 */
	public static String encrypt(String input) {
		return getEncryptStr(input);

	}

	private static String getEncryptStr(String input) {
		String prikey = key;
		byte[] crypted = null;
		byte rawKeyData[] = prikey.getBytes(); /* 用某种方法获得密匙数据 */
		String str="";
		SecretKeySpec skey = new SecretKeySpec(rawKeyData, "AES");
		try {
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, skey);
			crypted = cipher.doFinal(input.getBytes());
			str = new String(Base64.encode(crypted, Base64.NO_WRAP));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}

	/**
	 * 加密密码
	 * @param input  源数据
	 * @return
	 */
	/*public static String encryptPassword(String input) {
		input = MD5Util.MD5(input);
		return getEncryptStr(input);

	}*/

	/**
	 * 解密函数
	 * @param input  加密后的源数据
	 * @return
	 */
	public static String decrypt(String input) {
		String prikey = key;
		byte[] output = null;
		byte rawKeyData[] = prikey.getBytes(); /* 用某种方法获得密匙数据 */
		
		String str="";
		SecretKeySpec skey = new SecretKeySpec(rawKeyData, "AES");
		try {
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, skey);
			
//			output = cipher.doFinal(Base64.decodeBase64(input.getBytes()));
			output = cipher.doFinal(Base64.decode(input.getBytes(), Base64.NO_WRAP));
			str=new String(output,"UTF-8");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BadPaddingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;

	}
	
	public static void main(String[] args) {
		String username = "admin";
		String password = "123456";
		username = EncryptUtil.encrypt(username);
		password = EncryptUtil.encrypt(password);
		System.out.println("用户名加密："+username);
		System.out.println("用户名解密："+EncryptUtil.decrypt(username));
		System.out.println("密码加密："+password);
		System.out.println("密码解密："+EncryptUtil.decrypt(password));
	}
}
