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

import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.DSAPrivateKey;
import java.security.interfaces.DSAPublicKey;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;




/**
 * <p>Title:RSA </p>
 * <p>Description: </p>
 * <p>Company: </p> 
 * @author hu
 * @date
 */
public class RSA_DSA_ECDSA {
	//需加密内容
    private static String src="security RSA";
		
	public static void jdkRSA(){
		//1、初始化密钥
		try {
			KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
			keyPairGenerator.initialize(512);
			KeyPair keyPair=keyPairGenerator.generateKeyPair();
			RSAPublicKey rsaPublicKey=(RSAPublicKey) keyPair.getPublic();
			RSAPrivateKey rsaPrivateKey=(RSAPrivateKey) keyPair.getPrivate();
		
		//2、执行签名
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
			KeyFactory keyFactory=KeyFactory.getInstance("RSA");
			PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
			//签名算法MD5withRSA
			Signature signature=Signature.getInstance("MD5withRSA");
			//初始化签署签名的私钥
			signature.initSign(privateKey);
			signature.update(src.getBytes());
			byte[] result=signature.sign();
			System.out.println("jdk RSA sign:"+result.hashCode());
			
	    //3、验证签名
			X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(rsaPublicKey.getEncoded());
			keyFactory=keyFactory.getInstance("RSA");
			PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
			signature=Signature.getInstance("MD5withRSA");
			//初始化验证签名的公钥
			signature.initVerify(publicKey);
			signature.update(src.getBytes());
			boolean bool=signature.verify(result);
			
			System.out.println("jdk rsa verify:"+bool);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void jdkDSA(){
		//1、初始化密钥
		try {
			KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("DSA");
			keyPairGenerator.initialize(512);
			KeyPair keyPair=keyPairGenerator.generateKeyPair();
			DSAPublicKey dsaPublicKey=(DSAPublicKey) keyPair.getPublic();
			DSAPrivateKey dsaPrivateKey=(DSAPrivateKey) keyPair.getPrivate();
		
		//2、执行签名
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(dsaPrivateKey.getEncoded());
			KeyFactory keyFactory=KeyFactory.getInstance("DSA");
			PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
			//签名算法SHA1withDSA
			Signature signature=Signature.getInstance("SHA1withDSA");
			//初始化签署签名的私钥
			signature.initSign(privateKey);
			signature.update(src.getBytes());
			byte[] result=signature.sign();
			System.out.println("jdk DSA sign:"+result.hashCode());
			
	    //3、验证签名
			X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(dsaPublicKey.getEncoded());
			keyFactory=keyFactory.getInstance("DSA");
			PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
			signature=Signature.getInstance("SHA1withDSA");
			//初始化验证签名的公钥
			signature.initVerify(publicKey);
			signature.update(src.getBytes());
			boolean bool=signature.verify(result);
			
			System.out.println("jdk DSA verify:"+bool);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void jdkECDSA(){
		//1、初始化密钥
		try {
			KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("EC");
			keyPairGenerator.initialize(256);
			KeyPair keyPair=keyPairGenerator.generateKeyPair();
			ECPublicKey ecPublicKey=(ECPublicKey) keyPair.getPublic();
			ECPrivateKey ecPrivateKey=(ECPrivateKey) keyPair.getPrivate();
		
		//2、执行签名
			PKCS8EncodedKeySpec pkcs8EncodedKeySpec=new PKCS8EncodedKeySpec(ecPrivateKey.getEncoded());
			KeyFactory keyFactory=KeyFactory.getInstance("EC");
			PrivateKey privateKey=keyFactory.generatePrivate(pkcs8EncodedKeySpec);
			//签名算法SHA1withDSA
			Signature signature=Signature.getInstance("SHA1withECDSA");
			//初始化签署签名的私钥
			signature.initSign(privateKey);
			signature.update(src.getBytes());
			byte[] result=signature.sign();
			System.out.println("jdk ECDSA sign:"+result.hashCode());
			
	    //3、验证签名
			X509EncodedKeySpec x509EncodedKeySpec=new X509EncodedKeySpec(ecPublicKey.getEncoded());
			keyFactory=keyFactory.getInstance("EC");
			PublicKey publicKey=keyFactory.generatePublic(x509EncodedKeySpec);
			signature=Signature.getInstance("SHA1withECDSA");
			//初始化验证签名的公钥
			signature.initVerify(publicKey);
			signature.update(src.getBytes());
			boolean bool=signature.verify(result);
			
			System.out.println("jdk ECDSA verify:"+bool);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		jdkRSA();
		jdkDSA();
		jdkECDSA();
	}

}
