What is the easiest way to encrypt a password when I save it to the registry?

后端 未结 12 1925
小鲜肉
小鲜肉 2020-11-30 18:13

Currently I\'m writing it in clear text oops!, it\'s an in house program so it\'s not that bad but I\'d like to do it right. How should I go about encrypting this w

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 18:32

    This is what you would like to do:

    OurKey.SetValue("Password", StringEncryptor.EncryptString(textBoxPassword.Text));
    OurKey.GetValue("Password", StringEncryptor.DecryptString(textBoxPassword.Text));
    

    You can do that with this the following classes. This class is a generic class is the client endpoint. It enables IOC of various encryption algorithms using Ninject.

    public class StringEncryptor
    {
        private static IKernel _kernel;
    
        static StringEncryptor()
        {
            _kernel = new StandardKernel(new EncryptionModule());
        }
    
        public static string EncryptString(string plainText)
        {
            return _kernel.Get().EncryptString(plainText);
        }
    
        public static string DecryptString(string encryptedText)
        {
            return _kernel.Get().DecryptString(encryptedText);
        }
    }
    

    This next class is the ninject class that allows you to inject the various algorithms:

    public class EncryptionModule : StandardModule
    {
        public override void Load()
        {
            Bind().To();
        }
    }
    

    This is the interface that any algorithm needs to implement to encrypt/decrypt strings:

    public interface IStringEncryptor
    {
        string EncryptString(string plainText);
        string DecryptString(string encryptedText);
    }
    

    This is a implementation using the TripleDES algorithm:

    public class TripleDESStringEncryptor : IStringEncryptor
    {
        private byte[] _key;
        private byte[] _iv;
        private TripleDESCryptoServiceProvider _provider;
    
        public TripleDESStringEncryptor()
        {
            _key = System.Text.ASCIIEncoding.ASCII.GetBytes("GSYAHAGCBDUUADIADKOPAAAW");
            _iv = System.Text.ASCIIEncoding.ASCII.GetBytes("USAZBGAW");
            _provider = new TripleDESCryptoServiceProvider();
        }
    
        #region IStringEncryptor Members
    
        public string EncryptString(string plainText)
        {
            return Transform(plainText, _provider.CreateEncryptor(_key, _iv));
        }
    
        public string DecryptString(string encryptedText)
        {
            return Transform(encryptedText, _provider.CreateDecryptor(_key, _iv));
        }
    
        #endregion
    
        private string Transform(string text, ICryptoTransform transform)
        {
            if (text == null)
            {
                return null;
            }
            using (MemoryStream stream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(stream, transform, CryptoStreamMode.Write))
                {
                    byte[] input = Encoding.Default.GetBytes(text);
                    cryptoStream.Write(input, 0, input.Length);
                    cryptoStream.FlushFinalBlock();
    
                    return Encoding.Default.GetString(stream.ToArray());
                }
            }
        }
    }
    

    You can watch my video and download the code for this at : http://www.wrightin.gs/2008/11/how-to-encryptdecrypt-sensitive-column-contents-in-nhibernateactive-record-video.html

提交回复
热议问题