// ***********************************************************************
// 
//     AgileWebs
// 
// ***********************************************************************
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using System.Security.Cryptography;
using System.Text;
namespace Core.Cerberos.Adapters.Helpers
{
    /// 
    /// Handles all methods related to RSA encryption"/>.
    /// 
    public class RsaHelper
    {
        private readonly RSACryptoServiceProvider _privateKey;
        private readonly RSACryptoServiceProvider _publicKey;
        private readonly string keysFolder = "Keys\\";
        private readonly string exeDirectory = AppContext.BaseDirectory;
        /// 
        /// Initializes a new instance of .
        /// 
        public RsaHelper()
        {
            exeDirectory = exeDirectory + keysFolder;
            _publicKey = GetPublicKeyFromPemFile();
            _privateKey = GetPrivateKeyFromPemFile();
        }
        /// 
        /// Encrypts a text using RSA algorithm.
        /// 
        /// The text to be encrypted.
        /// The encrypted text.
        public string Encrypt(string text)
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(text);
            var encryptedBytes = _publicKey.Encrypt(Encoding.UTF8.GetBytes(text), true);
            return Convert.ToBase64String(encryptedBytes);
        }
        /// 
        /// Decrypts a text using RSA algorithm.
        /// 
        /// The encrypted text to be decrypted.
        /// The decrypted text.
        public string Decrypt(string encrypted)
        {
            var decryptedBytes = _privateKey.Decrypt(Convert.FromBase64String(encrypted), true);
            return Encoding.UTF8.GetString(decryptedBytes, 0, decryptedBytes.Length);
        }
        /// 
        ///Obtains the private key from a file.
        /// 
        /// The private key.
        private RSACryptoServiceProvider GetPrivateKeyFromPemFile()
        {
            using (TextReader privateKeyTextReader = new StringReader(File.ReadAllText(Path.Combine(exeDirectory, "HeathPrivateKey.pem"))))
            {
                AsymmetricCipherKeyPair readKeyPair = (AsymmetricCipherKeyPair)new PemReader(privateKeyTextReader).ReadObject();
                RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaPrivateCrtKeyParameters)readKeyPair.Private);
                RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
                csp.ImportParameters(rsaParams);
                return csp;
            }
        }
        /// 
        ///Obtains the public key from a file.
        /// 
        /// The public key.
        public RSACryptoServiceProvider GetPublicKeyFromPemFile()
        {
            using (TextReader publicKeyTextReader = new StringReader(File.ReadAllText(Path.Combine(exeDirectory, "HeathPublicKey.pem"))))
            {
                RsaKeyParameters publicKeyParam = (RsaKeyParameters)new PemReader(publicKeyTextReader).ReadObject();
                RSAParameters rsaParams = DotNetUtilities.ToRSAParameters((RsaKeyParameters)publicKeyParam);
                RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
                csp.ImportParameters(rsaParams);
                return csp;
            }
        }
    }
}