
using System.Security.Cryptography;

namespace Hotline.Application.Tools
{
    /// <summary>
    /// 公钥、私钥创建
    /// </summary>
    public class RSA_Create
    {
        /// <summary>
        /// 登陆公钥
        /// </summary>
        public const string RSA_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDGC7ZYMOLX8+Ze4AA0VrMcy/SbMhSGuTfOk5AWWekdSOZfb/OZ8G+oup5b/FCAxDmRolK0cK6nGgQ6QglMBB1qrHmfvyLR6Ock1MhUT8++T2lODRgM3xEtdJnFvcKIyte5eiSfJMby/7avHX0cho4OupRo1WK9veADyX45zmaMpQIDAQAB-----END PUBLIC KEY-----";

        /// <summary>
        /// 登陆私钥
        /// </summary>
        public const string RSA_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----MIICXAIBAAKBgQDGC7ZYMOLX8+Ze4AA0VrMcy/SbMhSGuTfOk5AWWekdSOZfb/OZ8G+oup5b/FCAxDmRolK0cK6nGgQ6QglMBB1qrHmfvyLR6Ock1MhUT8++T2lODRgM3xEtdJnFvcKIyte5eiSfJMby/7avHX0cho4OupRo1WK9veADyX45zmaMpQIDAQABAoGAELhaa7Ws4SiRUX895S1Qr+S0QQoIW5t1j7Y7mvd6S+Okt3f/LAb1llwzFHcH4OcwKRB4RQ2iYS88HiMHH+jVBs9uKtaVUGoTyly1TSBgNg7mtSh2JrGWPMAmVYYJKrIiwqrZh0783fQ0oYjSHoOGFaevVSiWdTgFCXkyJzXACKECQQDxaP8yO/YJ0k3oyYso0rzqrkt6NC17wl34VqGQ95QkJl40h2f0CXh458wPp1BgBQz9gcykri0V0MsdNK6a8TStAkEA0gPMbIHbNJ8FcbmToqWKBGlkDWSg/BZk9Xrov/wyc3QDtHZ7aV0m0x9/X0hXVzpAKiBnuehBVM/PxeQDaMs+2QJAT/hmQW/Xi11dbbYGIVneNyfR3QOX5Z13n6KjFvDM5ETJhEEu0Iqqm/K9iJn2aDiEGx56ejswuD9Yaa5gAPfSaQJBAIKuMmnic9BOk0isvR0b/MG6ArQCxdmW5cOKc4HjIFGPAy8r+qcZe6srkkpr7U0txfdwqpqX1u87UIJG24g90CECQBZTY2pGT6NVKFJ0cK7Qjwg1ekYMMPFGHjcf0jwI6nwZav7t46qSvSLSAXFtFsY7zTo3komvifKz8noLnA2qpBQ=-----END PRIVATE KEY-----";

        /// <summary>
        /// 取得私钥和公钥 XML 格式,返回数组第一个是私钥,第二个是公钥.
        /// </summary>
        /// <param name="size">密钥长度,默认1024,可以为2048</param>
        /// <returns></returns>
        public static string[] CreateXmlKey(int size = 1024)
        {
            //密钥格式要生成pkcs#1格式的  而不是pkcs#8格式的
            RSACryptoServiceProvider sp = new RSACryptoServiceProvider(size);
            string privateKey = sp.ToXmlString(true);//private key
            string publicKey = sp.ToXmlString(false);//public  key
            return new string[] { privateKey, publicKey };
        }

        /// <summary>
        /// 取得私钥和公钥 CspBlob 格式,返回数组第一个是私钥,第二个是公钥.
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static string[] CreateCspBlobKey(int size = 1024)
        {
            //密钥格式要生成pkcs#1格式的  而不是pkcs#8格式的
            RSACryptoServiceProvider sp = new RSACryptoServiceProvider(size);
            string privateKey = System.Convert.ToBase64String(sp.ExportCspBlob(true));//private key
            string publicKey = System.Convert.ToBase64String(sp.ExportCspBlob(false));//public  key 

            return new string[] { privateKey, publicKey };
        }
        /// <summary>
        /// 导出PEM PKCS#1格式密钥对，返回数组第一个是私钥,第二个是公钥.
        /// </summary>
        public static string[] CreateKey_PEM_PKCS1(int size = 1024)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(size);
            string privateKey = RSA_PEM.ToPEM(rsa, false, false);
            string publicKey = RSA_PEM.ToPEM(rsa, true, false);
            return new string[] { privateKey, publicKey };
        }

        /// <summary>
        /// 导出PEM PKCS#8格式密钥对，返回数组第一个是私钥,第二个是公钥.
        /// </summary>
        public static string[] CreateKey_PEM_PKCS8(int size = 1024, bool convertToPublic = false)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(size);
            string privateKey = RSA_PEM.ToPEM(rsa, false, true);
            string publicKey = RSA_PEM.ToPEM(rsa, true, true);
            return new string[] { privateKey, publicKey };
        }
    }
}
