/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.crypto.Cipher;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;

/**
 * @author 田平 create 2017年6月28日上午10:44:32
 */
public class RSAUtil {
	private static final Log log = LogFactory.getLog(RSAUtil.class);
	private static final String kpStr = "rO0ABXNyABVqYXZhLnNlY3VyaXR5LktleVBhaXKXAww60s0SkwIAAkwACnByaXZhdGVLZXl0ABpMamF2YS9zZWN1cml0eS9Qcml2YXRlS2V5O0wACXB1YmxpY0tleXQAGUxqYXZhL3NlY3VyaXR5L1B1YmxpY0tleTt4cHNyABRqYXZhLnNlY3VyaXR5LktleVJlcL35T7OImqVDAgAETAAJYWxnb3JpdGhtdAASTGphdmEvbGFuZy9TdHJpbmc7WwAHZW5jb2RlZHQAAltCTAAGZm9ybWF0cQB+AAVMAAR0eXBldAAbTGphdmEvc2VjdXJpdHkvS2V5UmVwJFR5cGU7eHB0AANSU0F1cgACW0Ks8xf4BghU4AIAAHhwAAACfDCCAngCAQAwDQYJKoZIhvcNAQEBBQAEggJiMIICXgIBAAKBgQDNITp//CLrnCVMQwwVJa69/bQ569g6El9HTtNseBE6nnS6J9mmpiLYIvgEOvRpGd1+hdGV+RQnvGSm6/EkziYDTdzeN9mwj8XK/mPeKoWA/uETyvRDoxxbUqi+Et/JH9Y10TFL3YYAaoDyIgWucbFLgMCJAvvY2f5DEYiB/1YPlwIDAQABAoGAOFIpR76or9wdeYTnKl9ATTX9Z/HTWo50zpVcA2osANZE4l/SqKjw5DslsbOmK71ITVbcprrOx+I4GISrnxli4gNfMfuQr7O+3jQo87FJwj/N3m852S4ibWxWbGjZLmyPfRr8fsAkyqVMfiuBw2g4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YiB/1YPlwIDAQAB";
	// 临时使用
	private static String RSAKeyStore = "d:/RSAKey.txt";

	private static KeyPair pair;
	static {
		pair = (KeyPair) byteToObject(Base64.decodeBase64(kpStr));
	}

	public static Object byteToObject(byte[] bytes) {
		Object obj = null;
		try {
			ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
			ObjectInputStream oi = new ObjectInputStream(bi);
			obj = oi.readObject();
			bi.close();
			oi.close();
		} catch (Exception e) {
			log.error("byteToObject error!", e);
			throw new RuntimeException("byteToObject error!", e);
		}
		return obj;
	}

	public static void saveKeyPair(KeyPair kp) {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream(RSAKeyStore);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(kp);
			oos.close();
			fos.close();
		} catch (Exception e) {
			log.error("saveKeyPair error!", e);
			throw new RuntimeException("saveKeyPair error!", e);
		}
	}

	public static String encrypt(String source) {
		byte[] data = source.getBytes();
		Cipher ci = null;
		try {
			ci = Cipher.getInstance("RSA");
			ci.init(Cipher.ENCRYPT_MODE, pair.getPublic());
			return Base64.encodeBase64String(ci.doFinal(data));
		} catch (Exception e) {
			log.error("RSAUtil encrypt error！", e);
			throw new RuntimeException("RSAUtil encrypt error！", e);
		}

	}

	public static String encryptHex(String source) {
		byte[] data = source.getBytes();
		Cipher ci = null;
		try {
			ci = Cipher.getInstance("RSA");
			ci.init(Cipher.ENCRYPT_MODE, pair.getPublic());
			byte[] secret = ci.doFinal(data);
			return HexUtil.bytes2HexStr(Base64.encodeBase64(secret));
		} catch (Exception e) {
			log.error("RSAUtil encrypt error！", e);
			throw new RuntimeException("RSAUtil encrypt error！", e);
		}

	}

	public static String decryptHex(String source) {
		byte[] data = HexUtil.hexStr2Byte(source);
		Cipher ci = null;
		try {
			ci = Cipher.getInstance("RSA");
			ci.init(Cipher.DECRYPT_MODE, pair.getPrivate());
			byte[] secret = Base64.decodeBase64(data);
			return new String(ci.doFinal(secret));
		} catch (Exception e) {
			log.error("RSAUtil decrypt error！", e);
			throw new RuntimeException("RSAUtil decrypt error！", e);
		}
	}

	public static String decrypt(String source) {
		byte[] data = Base64.decodeBase64(source);
		Cipher ci = null;
		try {
			ci = Cipher.getInstance("RSA");
			ci.init(Cipher.DECRYPT_MODE, pair.getPrivate());
			return new String(ci.doFinal(data));
		} catch (Exception e) {
			log.error("RSAUtil decrypt error！", e);
			throw new RuntimeException("RSAUtil decrypt error！", e);
		}
	}

	public static void main(String[] args) throws Exception {
		// System.out.println(encrypt("ADMIN"));
		// System.out.println(encrypt("abc123"));
		//System.out.println(decrypt("XFsOcSii8rE9RObVrvKSS2PjxyXlCpA/b2G/3kRB7JgXwneM1zelOnyZD8+R5dzJqP7USUO6iMOaVppl6kCE8tUIxqXCzRj0L7Y9hJvxaN6MnqAAi/NwE80Tsa0Qdeyc+T3aSAAQMIXrfcD23wJfW3EjfBizmiQk2DTQDZaBNCc="));
		//System.out.println(HexUtil.str2HexStr("XFsOcSii8rE9RObVrvKSS2PjxyXlCpA/b2G/3kRB7JgXwneM1zelOnyZD8+R5dzJqP7USUO6iMOaVppl6kCE8tUIxqXCzRj0L7Y9hJvxaN6MnqAAi/NwE80Tsa0Qdeyc+T3aSAAQMIXrfcD23wJfW3EjfBizmiQk2DTQDZaBNCc="));
		// System.out.println(encryptHex("ADMIN"));
		System.out.println(encryptHex("nacos"));
		System.out.println(encryptHex("@Crunii,.123"));
		System.out.println(decryptHex("4B566367786E6B42624E6D6F564F6E5370586F5653366968456D327A546A623052656B676165432B6B57476672627733546B736D52636B734E707850366D6C4D4F4749774A73636551586271353437684D31434A777736475456347567625A7957733766563661447357686D64477936543474795770794E583374754B4F6D4D70785666646976537633714A64553833653368434E6977784E54565366472B6A557A573050662B4D306B553D"));
		// 读取密钥对的二进制内容转换为base64的编码
		// FileInputStream fis = new FileInputStream(RSAKeyStore);
		// byte[] content = new byte[fis.available()];
		// fis.read(content);
		// fis.close();
		// System.out.println(Base64.encodeBase64String(content));

		// 生成密钥对
		// KeyPairGenerator keyGen;
		// try {
		// keyGen = KeyPairGenerator.getInstance("RSA");
		// } catch (NoSuchAlgorithmException e) {
		// log.error("RSAUtil getKeyGen error!", e);
		// throw new RuntimeException("RSAUtil getKeyGen error!", e);
		// }
		// keyGen.initialize(1024);
		// KeyPair keyPair = keyGen.generateKeyPair();
		// saveKeyPair(keyPair);
	}
}
