//[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 WritePublicKey_PublicRsaParameters_ShouldWriteCorrectKey() { var expectedPem = "-----BEGIN PUBLIC KEY-----\n" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsiLoIxmXaZAFRBKtHYZh\n" + "iF8m+pYR+xGIpupvsdDEvKO92D6fIccgVLIW6p6sSNkoXx5J6KDSMbA/chy5M6pR\n" + "vJkaCXCI4zlCPMYvPhI8OxN3RYPfdQTLpgPywrlfdn2CAum7o4D8nR4NJacB3NfP\n" + "nS9tsJ2L3p5iHviuTB4xm03IKmPPqsaJy+nXUFC1XS9E/PseVHRuNvKa7WmlwSZn\n" + "gQzKAVSIwqpgCc+oP1pKEeJ0M3LHFo8ao5SuzhfXUIGrPnkUKEE3m7B0b8xXZfP1\n" + "N6ELoonWDK+RMgYIBaZdgBhPfHxF8KfTHvSzcUzWZojuR+ynaFL9AJK+8RiXnB4C\n" + "JwIDAQAB\n" + "-----END PUBLIC KEY-----\n"; // Data has been derived from https://superdry.apphb.com/tools/online-rsa-key-converter var rsa = new RSAParameters { Modulus = Convert.FromBase64String("siLoIxmXaZAFRBKtHYZhiF8m+pYR+xGIpupvsdDEvKO92D6fIccgVLIW6p6sSNkoXx5J6KDSMbA/chy5M6pRvJkaCXCI4zlCPMYvPhI8OxN3RYPfdQTLpgPywrlfdn2CAum7o4D8nR4NJacB3NfPnS9tsJ2L3p5iHviuTB4xm03IKmPPqsaJy+nXUFC1XS9E/PseVHRuNvKa7WmlwSZngQzKAVSIwqpgCc+oP1pKEeJ0M3LHFo8ao5SuzhfXUIGrPnkUKEE3m7B0b8xXZfP1N6ELoonWDK+RMgYIBaZdgBhPfHxF8KfTHvSzcUzWZojuR+ynaFL9AJK+8RiXnB4CJw=="), Exponent = Convert.FromBase64String("AQAB") }; using (var stream = new MemoryStream()) { using (var writer = new PemWriter(stream)) writer.WritePublicKey(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 void WritePublicKey() { using (var fs = File.OpenWrite(Options.KeyName + ".pem")) using (var writer = new PemWriter(fs)) { writer.WritePublicKey(Key); } }
private static string GetPublicKey(RSAParameters publicKey) { var stream = new MemoryStream(); var writer = new PemWriter(stream); writer.WritePublicKey(publicKey); stream.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(stream); return(reader.ReadToEnd()); }
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); } }
/// <summary> /// Export the public key to a PEM format string /// </summary> /// <param name="rsa">the RSA key</param> /// <returns>A PEM encoded string</returns> public static string ToSubjectPublicKeyInfo(this RSA rsa) { var pem = default(string); using (var stream = new MemoryStream()) { using (var writer = new PemWriter(stream)) writer.WritePublicKey(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); }