C# Encrypt Text Output

前端 未结 9 1297
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 13:37

I have created a few little programs that export data to a text file using StreamWriter and then I read them back in using StreamReader. This works great and does what I nee

9条回答
  •  北海茫月
    2020-12-14 14:03

    Just to add another implementation of Leon's answer, and following the Microsoft docs

    Here a class example that encrypts and decrypts strings

        public static class EncryptionExample
    {
    
        #region internal consts
    
        internal const string passPhrase = "pass";
        internal const string saltValue = "salt";
        internal const string hashAlgorithm = "MD5";
        internal const int passwordIterations = 3;             // can be any number
        internal const string initVector = "0123456789abcdf"; // must be 16 bytes
        internal const int keySize = 64;                      // can be 192 or 256
    
        #endregion
    
        #region public static Methods
    
        public static string Encrypt(string data)
        {
            string res = string.Empty;
            try
            {
                byte[] bytes = Encoding.ASCII.GetBytes(initVector);
                byte[] rgbSalt = Encoding.ASCII.GetBytes(saltValue);
                byte[] buffer = Encoding.UTF8.GetBytes(data);
                byte[] rgbKey = new PasswordDeriveBytes(passPhrase, rgbSalt, hashAlgorithm, passwordIterations).GetBytes(keySize / 8);
                RijndaelManaged managed = new RijndaelManaged();
                managed.Mode = CipherMode.CBC;
                ICryptoTransform transform = managed.CreateEncryptor(rgbKey, bytes);
    
                byte[] inArray = null;
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, transform, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(buffer, 0, buffer.Length);
                        csEncrypt.FlushFinalBlock();
                        inArray = msEncrypt.ToArray();
                        res = Convert.ToBase64String(inArray);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Encrypt " + ex);
            }
    
            return res;
        }
    
        public static string Decrypt(string data)
        {
            string res = string.Empty;
            try
            {
                byte[] bytes = Encoding.ASCII.GetBytes(initVector);
                byte[] rgbSalt = Encoding.ASCII.GetBytes(saltValue);
                byte[] buffer = Convert.FromBase64String(data);
                byte[] rgbKey = new PasswordDeriveBytes(passPhrase, rgbSalt, hashAlgorithm, passwordIterations).GetBytes(keySize / 8);
                RijndaelManaged managed = new RijndaelManaged();
                managed.Mode = CipherMode.CBC;
                ICryptoTransform transform = managed.CreateDecryptor(rgbKey, bytes);
    
                using (MemoryStream msEncrypt = new MemoryStream(buffer))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msEncrypt, transform, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            res = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
               Console.WriteLine("Decrypt " + ex);
            }
    
            return res;
        }
    }
    

    By the way, here is the "salt value" definition that I had googled to find out what it was.

    Salt value

    If an attacker does not know the password, and is trying to guess it with a brute-force attack, then every password he tries has to be tried with each salt value. So, for a one-bit salt (0 or 1), this makes the encryption twice as hard to break in this way.

提交回复
热议问题