//[Obsolete] public void GenerateRsaKey(string version) { var rsa = RSA.Create(); rsa.KeySize = Convert.ToInt32(_configuration.GetSection("RSA:KeySize").Value); var directory = Path.Combine(_env.ContentRootPath, _configuration.GetSection("RSA:KeyLocation").Value, version); bool exists = System.IO.Directory.Exists(directory); if (!exists) { System.IO.Directory.CreateDirectory(directory); } var genpublicKey = Path.Combine(directory, @"publickey.pem"); var genprivatekey = Path.Combine(directory, @"privatekey.pem"); using (var fs = File.Create(genprivatekey)) { using (var pem = new PemWriter(fs)) { pem.WritePrivateKey(rsa); } } using (var fs = File.Create(genpublicKey)) { using (var pem = new PemWriter(fs)) { pem.WritePublicKey(rsa); } } }
public void WritePrivateKey_PrivateRsaParameters_ShouldWriteCorrectKey() { var expectedPem = "-----BEGIN RSA PRIVATE KEY-----\n" + "MIIEpAIBAAKCAQEAzw2rOycv+DqPhxJ/XZDsALH0WIn/Yyk97TGKhYME6CuybqlJ\n" + "BVTbpd2th2Fw/bDTwIHXr2wYJubX9b4G0gfVIwemq1rZyyJu2SxoOEK4BQg5s8po\n" + "Qh0Uq5KadP5DVvxk0AkIUySBCvNU+AHxRFJtJ6UZTjtUVYv2Yie+3YWhN3uDZ5ki\n" + "mjB7VSZqgdgnMpfx8kDTtGFHpA1oQNt71z3nyXPRTxZXGlYZlgGNEmv+cd3wYuuQ\n" + "0LnqZf3thXZfXYd0ASWhfdq0BxGT8WbThk8y6y9/aHsEtaf4QSpc8idVXPu0BwR8\n" + "P4p5lGzY11a5YERPOCl5BiaHYPgUJxzPnkFipwIDAQABAoIBAA26MUUNtw91CnkB\n" + "D/KrHgp5weJw276+SD3GkBGD+zpNU1ok3RN+acWYad3U5wHazF8x/JPDzeIeYekH\n" + "/TnFjSryYelwb4oZMVIysIIyYjLrNbAm1jyz4t/xK05gYSSOPTzRrHyeqfOI6HQ8\n" + "5LsL3/LF7mSSaGf3jJE7Y1sadfLQneSUhLMjDksl+o914P/WrV/LGcZ7O6noZ3Vg\n" + "e8zPGlPciPVO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q==\n" + "-----END RSA PRIVATE KEY-----\n"; // Expected data has been derived from https://superdry.apphb.com/tools/online-rsa-key-converter var rsa = new RSAParameters { Modulus = Convert.FromBase64String("zw2rOycv+DqPhxJ/XZDsALH0WIn/Yyk97TGKhYME6CuybqlJBVTbpd2th2Fw/bDTwIHXr2wYJubX9b4G0gfVIwemq1rZyyJu2SxoOEK4BQg5s8poQh0Uq5KadP5DVvxk0AkIUySBCvNU+AHxRFJtJ6UZTjtUVYv2Yie+3YWhN3uDZ5kimjB7VSZqgdgnMpfx8kDTtGFHpA1oQNt71z3nyXPRTxZXGlYZlgGNEmv+cd3wYuuQ0LnqZf3thXZfXYd0ASWhfdq0BxGT8WbThk8y6y9/aHsEtaf4QSpc8idVXPu0BwR8P4p5lGzY11a5YERPOCl5BiaHYPgUJxzPnkFipw=="), Exponent = Convert.FromBase64String("AQAB"), D = Convert.FromBase64String("DboxRQ23D3UKeQEP8qseCnnB4nDbvr5IPcaQEYP7Ok1TWiTdE35pxZhp3dTnAdrMXzH8k8PN4h5h6Qf9OcWNKvJh6XBvihkxUjKwgjJiMus1sCbWPLPi3/ErTmBhJI49PNGsfJ6p84jodDzkuwvf8sXuZJJoZ/eMkTtjWxp18tCd5JSEsyMOSyX6j3Xg/9atX8sZxns7qehndWB7zM8aU9yI9U4Goeq1bPHJPFu3JwhQ9bsYptmNpYzBHAucjlT6FygZCAexFRQkxzCIa5txJx2U4cKeivEzv2yCOeSjhEP4NRkZT+J/+g1FLp2Ei9pYIiu37iUeGkeFkYh3Xq0sIQ=="), P = Convert.FromBase64String("+uOWmE91jbACXylQ+GSAchUfAmeqtf2e23TlXzScleEay/elSbf6/HgUhUiUXCa2r+3d6kbEhoKy4Hed1Jm19rYBBIMSLZR0R3Il987x6WAnx+xrkfSWI3Z8fHJoCtTfS/hXA+W/Snr0WlFUMgwaEZzd3FEIU62ZizKgjO1c67E="), Q = Convert.FromBase64String("00V5L2a7FGa684RWXxNmFX0ROmAn3Dt9urT8lRsfsRARF52dPqSthS/iLchBIGTBUtZD5EnX5ttu88o5XkjxOLRrPsMBBW4fk1stUgQVifeDrlUG4yMf7lNLrkbmJy+0cl0yme9l+HMhOPKdLfyebadSy1HlkR8nKSaQW3y3wdc="), DP = Convert.FromBase64String("8vIL1DWtmaSEx22U0NNRZid5vbRxJIYRnGVX75dcwe4XKsgGMJqN2ojVJjOgJpP+d+IY8FHS4IYTfTWXilXGVL7twVbC9Yw6BS1OAudMbjcEjp4rlEyKTpDf/woyIbr89+3lJQsG77KciBEVPNlnLQL/++Yj8BO9CYPe4FjBkCE="), DQ = Convert.FromBase64String("mkKj1YSBHMhVwPDjz8/uPcpwBdunvxqBFw6HTqfbYAGHOWMQKWk8eX8Y+qy5QNnQfpeMQufYCOw3+zGw6bMAzpKNq+nemQRrccClOrlYsMBVGbljqf0/l1iibcG+0uX2L3r1M4ilP99wZpBfS/CkDRSbU3Gc2XWRtm4+AU7zLUk="), InverseQ = Convert.FromBase64String("RVY0Fcr5IzXZ6SphZ9zQj+RCw49d3aLyv5mu+T2sPlPSdfuGyH/tDobpResFVDiSpXn+7+0u6uBDO1q2z16SPwqsp+weEPwtKFi9FQm3HlILbxuqk8vKXQFMZPVtD52Ayu0sPo7/IjdMzTa9UqmXN+2C8tkdXg9Eft/kUB5Jet0=") }; using (var stream = new MemoryStream()) { using (var writer = new PemWriter(stream)) writer.WritePrivateKey(rsa); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream, Encoding.UTF8)) { var pem = reader.ReadToEnd(); Assert.That(pem, Is.EqualTo(expectedPem)); } } }
private RSA ReadRSA(string filePath = _rsaPrivateKeyFileName) { _validationHelpers.ThrowIfNull(filePath, nameof(filePath)); RSA rsa = null; string fileDir = Path.Combine(_commonHelpers.DefaultFileProvider.BaseDirectory, filePath); if ((SingletonDictionary <string, RSA> .Instance).ContainsKey(filePath)) { rsa = (SingletonDictionary <string, RSA> .Instance)[filePath]; } else { if (_commonHelpers.DefaultFileProvider.FileExists(fileDir)) { using (var stream = File.OpenRead(fileDir)) { using (var reader = new PemReader(stream)) { var rsaParameters = reader.ReadRsaKey(); rsa = RSA.Create(rsaParameters); } } (SingletonDictionary <string, RSA> .Instance).Add(filePath, rsa); } else { rsa = RSA.Create(); (SingletonDictionary <string, RSA> .Instance).Add(filePath, rsa); using (var stream = File.OpenWrite(fileDir)) { using (var writer = new PemWriter(stream)) { writer.WritePrivateKey(rsa); } } using (var stream = File.OpenWrite(Path.Combine(_commonHelpers.DefaultFileProvider.GetParentDirectory(fileDir), _rsaPublicKeyFileName))) { using (var writer = new PemWriter(stream)) { writer.WritePublicKey(rsa); } } } } return(rsa); }
public void SaveRSAs() { using (var fs = File.Create(_privateFileName)) { using (var pem = new PemWriter(fs)) pem.WritePrivateKey(_rsa); } using (var fs = File.Create(_publicFileName)) { using (var pem = new PemWriter(fs)) pem.WritePublicKey(_rsa); } }
public static string GetPrivateKeyFromPkcs12(X509Certificate2 cert) { RSA rsa = cert.GetRSAPrivateKey(); if (rsa != null) { MemoryStream stream = new MemoryStream(); var writer = new PemWriter(stream); writer.WritePrivateKey(rsa); stream.Seek(0, SeekOrigin.Begin); string privateKey = new StreamReader(stream).ReadToEnd(); return(privateKey); } return(null); }
/// <summary> /// Export the private key to a PEM format string /// </summary> /// <param name="rsa">the RSA key</param> /// <returns>A PEM encoded string</returns> public static string ToRSAPrivateKey(this RSA rsa) { var pem = default(string); using (var stream = new MemoryStream()) { using (var writer = new PemWriter(stream)) writer.WritePrivateKey(rsa); stream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(stream, Encoding.UTF8)) { pem = reader.ReadToEnd(); } } return(pem); }
public RsaKeyGenerator() { RSA rsa = RSA.Create(); rsa.KeySize = 2048; MemoryStream stream = new MemoryStream(); var writer = new PemWriter(stream); writer.WritePrivateKey(rsa); stream.Seek(0, SeekOrigin.Begin); this.PrivateKey = new StreamReader(stream).ReadToEnd(); stream.SetLength(0); writer.WritePublicKey(rsa); stream.Seek(0, SeekOrigin.Begin); this.PublicKey = new StreamReader(stream).ReadToEnd(); }
/// <summary> /// Exporta chaves para arquivos PEM /// </summary> /// <param name="rsa">Chave</param> void ExportToPEM(RSACryptoServiceProvider rsa) { var allParams = rsa.ExportParameters(true); var outputPathPrivate = Path.Combine(_pathBase, "key-private.pem"); var outputPathPublic = Path.Combine(_pathBase, "key-public.pem"); using (var stream = File.Create(outputPathPrivate)) using (var writer = new PemWriter(stream)) { writer.WritePrivateKey(allParams); } using (var stream = File.Create(outputPathPublic)) using (var writer = new PemWriter(stream)) { writer.WritePublicKey(allParams); } }
/// <summary>Regenerate the asymmetric service's keys and return the pair in PEM file format.</summary> /// <returns>PEM file variant of <c>KeyPair</c></returns> public KeyPair GetKeysFile() { byte[] privateKeyBytes; byte[] publicKeyBytes; using (var privMemStream = new MemoryStream()) using (var writer = new PemWriter(privMemStream)) { writer.WritePrivateKey(rsa); privateKeyBytes = privMemStream.ToArray(); } using (var pubMemStream = new MemoryStream()) using (var writer = new PemWriter(pubMemStream)) { writer.WritePublicKey(rsa); publicKeyBytes = pubMemStream.ToArray(); } string privateKey = Encoding.UTF8.GetString(privateKeyBytes); string publicKey = Encoding.UTF8.GetString(publicKeyBytes); return(new KeyPair(publicKey, privateKey)); }
// ficheroClavePublica: archivo donde guardara la clave publica // ficheroClavePrivada: archivo donde guardara la clave privada public static Byte[] GenerarClavesRSA(int keySize = 2048) { string directorioTemporal = Path.GetTempPath(); if (string.IsNullOrEmpty(directorioTemporal)) { throw new ArgumentNullException("directorioTemporal {ServicioSeguridad - Path.GetTempPath()}"); } string idOperacion = DateTime.Now.Ticks.ToString(); DirectoryInfo dirTemp = new DirectoryInfo(directorioTemporal); DirectoryInfo dirClaves = dirTemp.CreateSubdirectory(idOperacion); string ficheroClavePublica = Path.Combine(dirClaves.FullName, "publica.key"); string ficheroClavePrivada = Path.Combine(dirClaves.FullName, "privada.key"); string ficheroZip = ""; Byte[] zipClaves = null; var rsa = RSA.Create(); rsa.KeySize = keySize; try { using (var fs = File.Create(ficheroClavePublica)) { using (var pem = new PemWriter(fs)) { pem.WritePublicKey(rsa); } } using (var fs = File.Create(ficheroClavePrivada)) { using (var pem = new PemWriter(fs)) { pem.WritePrivateKey(rsa); } } ficheroZip = Path.Combine(dirTemp.FullName, $"claves_{idOperacion}.zip"); ZipFile.CreateFromDirectory(dirClaves.FullName, ficheroZip); zipClaves = File.ReadAllBytes(ficheroZip); } catch (Exception ex) { //log.Error($"ERR SERVICIOSEGURIDAD (GenerarClaves) - {ex.Message}"); Console.WriteLine($"ERR SERVICIOSEGURIDAD (GenerarClaves) - {ex.Message}"); } finally { if (Directory.Exists(dirClaves.FullName)) { dirClaves.Delete(true); } if (!string.IsNullOrEmpty(ficheroZip) && File.Exists(ficheroZip)) { File.Delete(ficheroZip); } } return(zipClaves); }