/**
 *
 */
package com.manage.encrypt;

import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * <p>Title:jdkDES </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author hu
 * @date
 */
public class DES {
	private static String src="security";
	/**
	 * DES加解密
	 */
	public static void jdkDES(){
		try {
			//生成Key
			KeyGenerator keyGeneratro=KeyGenerator.getInstance("DES");
			//Key Size
			keyGeneratro.init(56);
			SecretKey secrekKey=keyGeneratro.generateKey();
			byte[] bytesKey=secrekKey.getEncoded();

			//Key转换(传入生成的bytesKey)
			DESKeySpec desKeySpec=new DESKeySpec(bytesKey);
			//factory传入加密方式
			SecretKeyFactory factory=SecretKeyFactory.getInstance("DES");
			//Key转换生成密钥
			SecretKey convertSecretKey=factory.generateSecret(desKeySpec);

			//加密（DES算法/ECB工作方式/PKCS5Padding填充方式）
			Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
			//输入加密模式，密钥
			cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
			//加密，输入要加密的内容
			byte[] result=cipher.doFinal(src.getBytes());
			System.out.println("jdk des encrypt十六进制:"+result.hashCode());

			//解密
			cipher.init(cipher.DECRYPT_MODE, convertSecretKey);
			result=cipher.doFinal(result);
			System.out.println("jek des decrypt:"+new String(result));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * bcDES加解密
	 */
	public static void bcDES(){
		try {
			Security.addProvider(new BouncyCastleProvider());

			//生成Key
			KeyGenerator keyGeneratro=KeyGenerator.getInstance("DES","BC");
			keyGeneratro.getProvider();
			//Key Size
			keyGeneratro.init(56);
			SecretKey secrekKey=keyGeneratro.generateKey();
			byte[] bytesKey=secrekKey.getEncoded();

			//Key转换(传入生成的bytesKey)
			DESKeySpec desKeySpec=new DESKeySpec(bytesKey);
			//factory传入加密方式
			SecretKeyFactory factory=SecretKeyFactory.getInstance("DES");
			//Key转换生成密钥
			SecretKey convertSecretKey=factory.generateSecret(desKeySpec);

			//加密（DES算法/ECB工作方式/PKCS5Padding填充方式）
			Cipher cipher=Cipher.getInstance("DES/ECB/PKCS5Padding");
			//输入加密模式，密钥
			cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
			//加密，输入要加密的内容
			byte[] result=cipher.doFinal(src.getBytes());
			System.out.println("bc des encrypt十六进制:"+result.hashCode());

			//解密
			cipher.init(cipher.DECRYPT_MODE, convertSecretKey);
			result=cipher.doFinal(result);
			System.out.println("bc des decrypt:"+new String(result));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 3DES加解密
	 */
	public static void jdk3DES(){
		try {
			//生成Key
			KeyGenerator keyGeneratro=KeyGenerator.getInstance("DESede");
			//Key Size
			//keyGeneratro.init(168);
			//生成默认长度
			keyGeneratro.init(new SecureRandom());
			SecretKey secrekKey=keyGeneratro.generateKey();
			byte[] bytesKey=secrekKey.getEncoded();

			//Key转换(传入生成的bytesKey)
			DESedeKeySpec desKeySpec=new DESedeKeySpec(bytesKey);
			//factory传入加密方式
			SecretKeyFactory factory=SecretKeyFactory.getInstance("DESede");
			//Key转换生成密钥
			SecretKey convertSecretKey=factory.generateSecret(desKeySpec);

			//加密（DES算法/ECB工作方式/PKCS5Padding填充方式）
			Cipher cipher=Cipher.getInstance("DESede/ECB/PKCS5Padding");
			//输入加密模式，密钥
			cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
			//加密，输入要加密的内容
			byte[] result=cipher.doFinal(src.getBytes());
			System.out.println("jdk 3des encrypt十六进制:"+result.hashCode());

			//解密
			cipher.init(cipher.DECRYPT_MODE, convertSecretKey);
			result=cipher.doFinal(result);
			System.out.println("jek 3des decrypt:"+new String(result));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		jdkDES();
		bcDES();
		jdk3DES();
	}

}
