public SecureString ReadLine() { // make sure Ctrl+C can abort editing _mainThread = Thread.CurrentThread; Console.CancelKeyPress += InterruptEdit; SecureString buffer = new SecureString(); var finished = false; while (!finished) { try { finished = ReadKey(buffer); } catch (ThreadAbortException) { // thrown on Ctrl+C Thread.ResetAbort(); buffer.Clear(); buffer = null; break; } } Console.WriteLine(); // reset Ctrl+C handling Console.CancelKeyPress -= InterruptEdit; return buffer; }
public unsafe void UnsafeConstructor () { try { SecureString ss = null; char[] data = new char[] { 'a', 'b', 'c' }; fixed (char* p = &data[0]) { ss = new SecureString (p, data.Length); } Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly"); Assert.AreEqual (3, ss.Length, "3"); ss.AppendChar ('a'); Assert.AreEqual (4, ss.Length, "4"); ss.Clear (); Assert.AreEqual (0, ss.Length, "0b"); ss.InsertAt (0, 'b'); Assert.AreEqual (1, ss.Length, "1b"); ss.SetAt (0, 'c'); Assert.AreEqual (1, ss.Length, "1c"); ss.RemoveAt (0); Assert.AreEqual (0, ss.Length, "0c"); ss.Dispose (); } catch (NotSupportedException) { Assert.Ignore (NotSupported); } }
public static void ComposeFile(BinaryWriter writer, string username, SecureString password, long IP, ushort port, string databaseName) { byte[] encodedDbName = UTF8.GetBytes(databaseName); byte[] encodedPassword = UTF8.GetBytes(ConvertToUnsecureString(password)); byte[] encodedUsername = UTF8.GetBytes(username); byte[] publicKey = GenerateRandomASCIChars(ENCODING_LENGTH); byte[] encryptedPassword; using (SHA1 sha = new SHA1CryptoServiceProvider()) { encryptedPassword = AESEncryption.Encrypt(sha.ComputeHash(encodedPassword), publicKey, PRIVATE_VECTOR, PRIVATE_KEY); } password.Clear(); DestroyData(encodedPassword); writer.Write(IP); writer.Write(port); writer.Write(encodedUsername.Length); writer.Write(encodedUsername); writer.Write(encodedDbName.Length); writer.Write(encodedDbName); writer.Write(publicKey.Length); writer.Write(publicKey); writer.Write(encryptedPassword.Length); writer.Write(encryptedPassword); DestroyData(encryptedPassword); }
public static void ToSecureString(this String value, SecureString secureString) { secureString.Clear(); if (value != null) { Array.ForEach(value.ToArray(), secureString.AppendChar); secureString.MakeReadOnly(); } }
public static SecureString SetValue(this SecureString ss, string str) { if (ss != null) { ss.Clear(); if (str != null) { foreach (var c in str) { ss.AppendChar(c); } } } return(ss); }
private static SecureString GetPasswordFromConsole() { SecureString password = new SecureString(); bool readingPassword = true; Console.Write("Enter password: "******" "); Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop); } break; default: if (userInput.KeyChar != 0) { password.AppendChar(userInput.KeyChar); Console.Write("*"); } break; } } Console.WriteLine(); password.MakeReadOnly(); return password; }
public RenameComputer(string computerName, string newComputerName) { ps = PowerShell.Create(); domainUName = System.Configuration.ConfigurationManager.AppSettings["AddCUser"]; domainPWord = System.Configuration.ConfigurationManager.AppSettings["AddCPW"]; localUName = System.Configuration.ConfigurationManager.AppSettings["AdminUser"]; localPWord = System.Configuration.ConfigurationManager.AppSettings["AdminPW"]; securestring = new System.Security.SecureString(); foreach (char c in localPWord) { securestring.AppendChar(c); } localCred = new PSCredential(domainUName, securestring); securestring.Clear(); foreach (char c in domainPWord) { securestring.AppendChar(c); } domainCred = new PSCredential(domainUName, securestring); parameters = new List<string>(); this.computerName = computerName; this.newComputerName = newComputerName; }
public static string Sellar(string strPathLlave, string strLlavePwd, string strCadenaOriginal) { try { string strSello = string.Empty; SecureString passwordSeguro = new System.Security.SecureString(); passwordSeguro.Clear(); foreach (char c in strLlavePwd.ToCharArray()) { passwordSeguro.AppendChar(c); } byte[] llavePrivadaBytes = System.IO.File.ReadAllBytes(strPathLlave); RSACryptoServiceProvider rsa = opensslkey.DecodeEncryptedPrivateKeyInfo(llavePrivadaBytes, passwordSeguro); if (rsa == null) { byte[] bytes = new byte[llavePrivadaBytes.Length - 3]; for (int i = 3; i < llavePrivadaBytes.Length; i++) { bytes[i - 3] = llavePrivadaBytes[i]; } llavePrivadaBytes = bytes; rsa = opensslkey.DecodeEncryptedPrivateKeyInfo(llavePrivadaBytes, passwordSeguro); } SHA1CryptoServiceProvider hasher = new SHA1CryptoServiceProvider(); byte[] bytesFirmados = rsa.SignData(System.Text.Encoding.UTF8.GetBytes(strCadenaOriginal), hasher); strSello = Convert.ToBase64String(bytesFirmados); return(strSello); } catch (Exception ex) { return(""); } }
public void DefaultConstructor () { try { SecureString ss = new SecureString (); Assert.IsFalse (ss.IsReadOnly (), "IsReadOnly"); Assert.AreEqual (0, ss.Length, "0"); ss.AppendChar ('a'); Assert.AreEqual (1, ss.Length, "1"); ss.Clear (); Assert.AreEqual (0, ss.Length, "0b"); ss.InsertAt (0, 'b'); Assert.AreEqual (1, ss.Length, "1b"); ss.SetAt (0, 'c'); Assert.AreEqual (1, ss.Length, "1c"); Assert.AreEqual ("System.Security.SecureString", ss.ToString (), "ToString"); ss.RemoveAt (0); Assert.AreEqual (0, ss.Length, "0c"); ss.Dispose (); } catch (NotSupportedException) { Assert.Ignore (NotSupported); } }