Skip to content

Instantly share code, notes, and snippets.

@MorbosVermin
Last active December 6, 2017 15:19
Show Gist options
  • Save MorbosVermin/ec1dc9b74ed233767ac40a8cf63c4729 to your computer and use it in GitHub Desktop.
Save MorbosVermin/ec1dc9b74ed233767ac40a8cf63c4729 to your computer and use it in GitHub Desktop.
General Utils
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Com.WaitWha.Utils
{
/// <summary>
/// Class for protecting strings
///
/// Example:
/// byte[] salt = ...;
/// string value = StringSec.GetUnSecureString(StringSec.DecryptString(Properties.Settings.Default.value, salt));
/// Properties.Settings.Default.value = StringSec.EncryptString(StringSec.GetSecureString(value), salt);
/// Properties.Settings.Default.Save();
/// </summary>
public class StringSec
{
/// <summary>
/// Converts the given string input to a SecureString
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static SecureString GetSecureString(string input)
{
SecureString s = new SecureString();
foreach (char c in input)
s.AppendChar(c);
s.MakeReadOnly();
return s;
}
/// <summary>
/// Converts the given SecureString to a less secure string.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string GetUnsecureString(SecureString s)
{
string r = string.Empty;
IntPtr ptr = Marshal.SecureStringToBSTR(s);
try
{
r = Marshal.PtrToStringBSTR(ptr);
}
finally
{
Marshal.ZeroFreeBSTR(ptr);
}
return r;
}
/// <summary>
/// Encrypts the given SecureString for more permenant storage.
/// </summary>
/// <param name="input">SecureString to protect</param>
/// <param name="salt">Salt</param>
/// <returns>string</returns>
public static string EncryptString(SecureString input, byte[] salt)
{
byte[] d =
ProtectedData.Protect(
Encoding.Unicode.GetBytes(GetUnsecureString(input)),
salt,
DataProtectionScope.CurrentUser);
return Convert.ToBase64String(d);
}
/// <summary>
/// Decrypts a given string using the given salt.
/// </summary>
/// <param name="input">string to decrypt</param>
/// <returns>SecureString</returns>
public static SecureString DecryptString(string input, byte[] salt)
{
byte[] d = ProtectedData.Unprotect(Convert.FromBase64String(input), salt, DataProtectionScope.CurrentUser);
return GetSecureString(Encoding.Unicode.GetString(d));
}
/// <summary>
/// Returns a SecureString from the Console entered by the user.
/// </summary>
/// <returns>SecureString</returns>
public static SecureString GetPasswordFromConsole()
{
SecureString password = new SecureString();
ConsoleKeyInfo keypress = Console.ReadKey(true);
while(keypress.Key != ConsoleKey.Enter)
{
if(keypress.Key == ConsoleKey.Backspace && password.Length > 0)
{
password.RemoveAt(password.Length - 1);
Console.Write("\b \b");
}
else
{
password.AppendChar(keypress.KeyChar);
Console.Write("*");
}
}
return password;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment