package cn.pluss.platform.util;

import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PublicKey;
import javax.security.cert.X509Certificate;
import javax.crypto.Cipher;
import java.util.Base64;


public class RSAEncryptUtil {

	private static final String ALGORITHOM = "RSA";// 固定值，无须修改
	private static final String CIPHER_PROVIDER = "SunJCE";
	private static final String TRANSFORMATION_PKCS1Paddiing = "RSA/ECB/PKCS1Padding";
	private static final String RSA_PKCS1_OAEP_PADDING = "RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING";
	
	private static final String CHAR_ENCODING = "UTF-8";// 固定值，无须修改
	private static final String CERT ="-----BEGIN CERTIFICATE-----\r\n" + 
			"MIID3DCCAsSgAwIBAgIUTz/ylkJ1ui0tgyC8gYf7PhjSLf4wDQYJKoZIhvcNAQEL\r\n" + 
			"BQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsT\r\n" + 
			"FFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3Qg\r\n" + 
			"Q0EwHhcNMTkwOTE5MDYzOTI4WhcNMjQwOTE3MDYzOTI4WjBuMRgwFgYDVQQDDA9U\r\n" + 
			"ZW5wYXkuY29tIHNpZ24xEzARBgNVBAoMClRlbnBheS5jb20xHTAbBgNVBAsMFFRl\r\n" + 
			"bnBheS5jb20gQ0EgQ2VudGVyMQswCQYDVQQGDAJDTjERMA8GA1UEBwwIU2hlblpo\r\n" + 
			"ZW4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCgVuNIZDy2F7UYQw7+\r\n" + 
			"QjkWSBLbAimkhZ6Qa6jhquZvUruD1/YpKbtghk/ecxrSDpC8Pqsjl/QjrmkTPWv7\r\n" + 
			"vNihoAMftuR/nAusmBrTkNlzxEBVvRpf4JRsIHg73wUi5zUJkZn7eAZ7kHVa0zow\r\n" + 
			"qflsl7w29guTvqUPP1P1+gH+xD3b3fCM1uRINDDqTyFff6p1hM7Fn9wnapKDmcO7\r\n" + 
			"57QkL8VGNAWknVkBpMYiAmK2R7DlPW5zUleO9M30koNYSIRVwgHiS2Kutd1/gvW7\r\n" + 
			"g5oC+nDLSgQkwgUoonrXzo1bUkE05icf92v89hDVccaXKzVjGgMP407s88++amNV\r\n" + 
			"5J65AgMBAAGjgYEwfzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIE8DBlBgNVHR8EXjBc\r\n" + 
			"MFqgWKBWhlRodHRwOi8vZXZjYS5pdHJ1cy5jb20uY24vcHVibGljL2l0cnVzY3Js\r\n" + 
			"P0NBPTFCRDQyMjBFNTBEQkMwNEIwNkFEMzk3NTQ5ODQ2QzAxQzNFOEVCRDIwDQYJ\r\n" + 
			"KoZIhvcNAQELBQADggEBAABTz0NLnc0bn7InEDVuYMEebEzZv3BxhHVdnzX6R0gQ\r\n" + 
			"Ug0QifHdAtbh/qL10E4FjVACyWOOWgPoX/9I1BZjfxddzQgbUib3XybloR6eE0z3\r\n" + 
			"WUh/B88cCM3GsJU4btaxY0qpTkEjEcqP7K+4ZrJyfCItAfxWqYh6sllGbFVvRSYa\r\n" + 
			"brlEI+lbdlNVnshrRNegtFZIJ40O8aSpNA/2R0+5lEKPeYiQUO4qYbCE3lkPpY9S\r\n" + 
			"p0SMyX0UmB9SIwOFAX+BX3KJ6GBKh+zqSmd0hm4NtbCNRmEG9gYQPl845FdYizKJ\r\n" + 
			"RIk+gkdXBduIpO3/ZkzED/PYMOj/hhJdLNByPe78Ipo=\r\n" + 
			"-----END CERTIFICATE-----";
	private static final String PUBLIC_KEY ="-----BEGIN PUBLIC KEY-----\r\n" + 
			"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArjSigZVVYy1LzeKmjrJY\r\n" + 
			"TVh1pS2mYa07hcXOH0vDGBFp8XY1HlJTedTDmYL9pBH/q6PSAf/mvQrXjEbOgFoa\r\n" + 
			"ro2qEr+SLIze47hxXdZKtgHOKdX6XmjJ9zc1VgB1xHGL339fyOsAz9+lYy1aBClw\r\n" + 
			"eInkruUBHSudF38dQY43UtHjLoXnrX+CiQCofTqcjbfjVgl/MKcsF6XVajkZ/yOa\r\n" + 
			"I3kOaQ+YAxB45lAn8vt7rK+VvdkwEf/A1U0+jxwChpdzNlTGMy9qpIWQiILA1Z4+\r\n" + 
			"9PtWq/3EYxGPKJyxG0PCSGT495v/rAbnYHDt37E4PqslBYBskYLlmQOIh4oltVMX\r\n" + 
			"wwIDAQAB\r\n" + 
			"-----END PUBLIC KEY-----";

	/**
	 * 数据加密方法
	 * @param publicKey
	 * @param data
	 * @return
	 * @throws Exception
	 */
	private static byte[] encryptPkcs1padding(PublicKey publicKey, byte[] data) throws Exception {
		Cipher ci = Cipher.getInstance(TRANSFORMATION_PKCS1Paddiing, CIPHER_PROVIDER);
		ci.init(Cipher.ENCRYPT_MODE, publicKey);
		return ci.doFinal(data);
	}

	/**
	 * 加密后的秘文，使用base64编码方法
	 * @param bytes
	 * @return
	 * @throws Exception
	 */
	private static String encodeBase64(byte[] bytes) throws Exception {
		return Base64.getEncoder().encodeToString(bytes);
	}

	/**
	 * 对敏感内容（入参Content）加密，其中PUBLIC_KEY_FILENAME为存放平台证书的路径，平台证书文件存放明文平台证书内容，且为pem格式的平台证书（平台证书的获取方式参照平台证书及序列号获取接口，通过此接口得到的参数certificates包含了加密的平台证书内容ciphertext，然后根据接口文档中平台证书解密指引，最终得到明文平台证书内容）
	 * @param Content
	 * @return
	 * @throws Exception
	 */
	public static String rsaEncrypt(String Content) throws Exception {
		X509Certificate certificate = X509Certificate.getInstance(CERT.getBytes());
		PublicKey publicKey = certificate.getPublicKey();
		System.out.println(publicKey);
		return encodeBase64(encryptPkcs1padding(publicKey, Content.getBytes(CHAR_ENCODING)));
	}
	
	public static String rsaEncrypt(String Content,String ras) throws Exception {
		X509Certificate certificate = X509Certificate.getInstance(PUBLIC_KEY.getBytes());
		PublicKey publicKey = certificate.getPublicKey();
		System.out.println(publicKey);
		Cipher ci = Cipher.getInstance(RSA_PKCS1_OAEP_PADDING);
		ci.init(Cipher.ENCRYPT_MODE, publicKey);
		return encodeBase64(ci.doFinal(Content.getBytes(CHAR_ENCODING)));
	}
}
