yanjustino/ellis

View on GitHub
demos/dotnet/DecryptSecret.cs

Summary

Maintainability
A
0 mins
Test Coverage
using System;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;

namespace Ellis.Demo.Dotnet
{
    /// <summary>
    /// Service do decrypt Secret Keys in Settings File
    /// </summary>
    public class DecryptSecret
    {
        
        /// <summary>
        /// Private certificate Generated by Elli
        /// </summary>
        private const string KEY =
            @"MIIEowIBAAKCAQEAn4jdDVDlUCoCvOJNbZVR66GEfJOHuhlp/WrbyK6LSTID1SF8
        Q/9pJX5m8uYsWAF5MESooq2Ieu+/QUGnUDq4BWAoOerwLu2AyjVs7FyvDg3rAznN
            upLJQZ0ClQVH0DBW4lCQaNVpIr/xuLD0W8A2NBFOINsZBuBfS/KNktPmoT/xbGme
            fv1Vmu9Nyj0oBssoLYTtB3PImfntyLeM9fcJGpWKrQH0jDg8VTMs1Fn/LsqKwR/J
            im1+aqbWsD5MCIINN0T1+AQuzzubdQSwoT82vfscEy3Bbrt6UXb1bRVRUi3QQ/dk
            nghK3J1xKqt9NoPkIR8VRWgL7X6m2feELlCELwIDAQABAoIBABZYGfXSfbujlqyT
        2ZpejpG9FoxIMXtcMd8fh1btM272BVlljj0L2dM5h/u/RziwRCQBicj3GyV0zMm8
            A04WW03NcPTjte4sr5wirXXRaWdDhcX245ydEHkTH5h1m+BJ1PFvrToxob8POTDP
//mVy0GxpsqtRS0UYeSGPVPyP9wXmwFbxP1jGm3EZS0vVqe1uIF0G/FZbHr5YN3L
            exT+MSzylo0pCO0AyS7WeH0SNCKoGbD9AjChoumvbYgH8MdmnlQX7R0bQDqH4Eub
        9N5RA6NqO/MtF4gaksHogHs5X15LL+2tMcA3ZS4tOsWJl2UL/32CUd+4tM5jvt9T
            oZygvpECgYEAy0AeGhsO5F2L3j08GTwvyKMk5bklifMZdlJZgJF+GDNccEVinbgr
            BWqaJg+sYvuvXXEPP7W7uYX4XjkFbKgq/hbS7/UMXAG9vZh/nH9tg0Wgb15LQA+O
            hKMxwMlpXZNAKfvyDVMizNEmPL+J8kpRp4CSW+vXECQ09WAQbtEmnfcCgYEAyPBI
            U/gpr3ADYpDdz0HaXsE3k804vxnXoUrLPX48Iv1DNpSXnH7wrZkHp+2St/tR8Vjp
        /WNa9garbK716iHbH+WB2+zQPRGYc08cnkMttemXEtr0ABjrHalFVY6QL3BuWsJ7
            OBKOGkuLe2eF63WK09vuqYHg9OUUHBi/pikHHYkCgYABUKDhOluWTqShA3RoFWOZ
            wLR0aAY+Jjx773x0wHw4W9y1PT2uF4rvtgjZRjCvU0GHMTwQBXT5dkxtD6FhWpUs
            n6kttEAoODqqB1qMAKcMul6XWdzSxUb+xK+Ft8feux0nKGIgg/Etsgy1R+f0kVaF
        5ucLgTMa/6D4FW8FyISfcQKBgE0uZknCl5hoiRRSGrDiVZNx2fwupq5YqcetZ/Wd
            yFGUQD31w2p8I8w6bINnMKT0ptvyUCZhBG5dz3i2l4SwLm/JAyFLjAz49jrMV2y+
        1w75w2qdqX1kFONWwmgDk4R1Rh4cd6HpFqYpAFGNSRZRFtsbrkdzuMm4OAo2w156
            gd4pAoGBAJglKh6ImbnSjS4Q3T9XBa22ky8tOQCjl6AMZXLD5tm7N6vOjoI10qvN
        PMs1ZDMvmVg29PFp41GyFpY3qiPq1U9A5ifXrvMDLgilJIyUanvNZUFLTimg/Inp
        7PZJaP6nRNJ3KtcB6wCGsthiQ6amQPTIkWyDZVxeknKk7kd0oTg0";

        private IConfiguration Configuration { get; }

        public DecryptSecret(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// The method get secrets keys from settings file.
        /// These secret keys are generated by elli
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Decrypt(HttpContext context)
        {
            var cert = GetCertificate();
            var content = string.Empty;
            string key, value;
            
            
            key = Configuration.GetSection("Holder:Items:0:Key").Value;
            value = Configuration.GetSection("Holder:Items:0:Value").Value;
            content += WriteContent(key, value);
            
            key = Configuration.GetSection("Holder:Items:0:Key").Value;
            value = Configuration.GetSection("Holder:Items:0:Value").Value;

            var decodeValue = Convert.FromBase64String(value);
            var decriptedValue = Encoding.UTF8.GetString(cert.Decrypt(decodeValue, RSAEncryptionPadding.OaepSHA512));
            content += WriteContent(key, decriptedValue);
            
            
            await context.Response.WriteAsync(WriteHtmlBody(content));
        }

        /// <summary>
        /// Loading Private Certificate
        /// </summary>
        /// <returns></returns>
        private RSA GetCertificate()
        {
            var privateKeyBytes = Convert.FromBase64String(KEY);
            var rsa = RSA.Create();
            rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
            return rsa;
        }

        private static string WriteContent(string key, string value)
        {
            var sb = new StringBuilder();
            sb.AppendLine($"<h2>Encrypted {key}</h2>");
            sb.AppendLine($"<p>{value}</p>");
            sb.AppendLine("<hr />");

            return sb.ToString();
        }

        private static string WriteHtmlBody(string content) => $"<html><body>{content}</body></content>";
    }
}