// RSA private key'lerin dosyadan okunmasi public RSAParameters GetPrivateKey(string dirName, Form form) { RSAParametersSerializable rsaPrivateKeyInfoSerializable = null; DirectoryInfo dirInfo = new DirectoryInfo(FileDirectory.Create(dirName) + "Keys\\"); FileInfo[] files = dirInfo.GetFiles(); foreach (FileInfo file in files) { if (file.Name.Equals(ID)) { rsaPrivateKeyInfoSerializable = Serialization.ReadFromBinaryFile <RSAParametersSerializable>(FileDirectory.Create(dirName) + "Keys\\" + ID); } } Form8Passphrase formPassphrase = new Form8Passphrase(Name); bool invalidPass = true; int tryCount = 1; while (invalidPass && tryCount <= 3) { formPassphrase.labelTry.Visible = (tryCount == 2 || tryCount == 3) ? true : false; formPassphrase.labelTry.Text = "Yanlış parola. Deneme: " + tryCount + " / 3."; formPassphrase.ShowDialog(form); byte[] passphrase = null; if (formPassphrase.DialogResult == DialogResult.OK) { passphrase = Encoding.ASCII.GetBytes(formPassphrase.textBoxPassphrase.Text); byte[] hashedInputPassphrase = SHA256Salted.GenerateSaltedHash(passphrase, salt); if (SHA256Salted.CompareByteArrays(hashedInputPassphrase, hashedPassphrase)) { rsaPrivateKeyInfoSerializable.Decrypt(passphrase); invalidPass = false; } else { invalidPass = true; tryCount++; } } else { break; } } if (tryCount > 3) { return(new RSAParameters()); } return(rsaPrivateKeyInfoSerializable.RSAParameters); }
public Certificate(string name, string email, string comment, string passphrase) { Name = name; Email = email; Comment = comment; salt = RandomPassphrase(8); hashedPassphrase = SHA256Salted.GenerateSaltedHash(Encoding.ASCII.GetBytes(passphrase), salt); ValidFrom = DateTime.Today; ValidUntil = ValidFrom.AddYears(2); using (RSACryptoServiceProvider cryptoServiceProvider = new RSACryptoServiceProvider(2048)) { rsaPublicKeyInfo = cryptoServiceProvider.ExportParameters(false); rsaPrivateKeyInfo = cryptoServiceProvider.ExportParameters(true); } // https://stackoverflow.com/a/42026123/5589417 ID = Regex.Replace(Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", ""); }