C#一個字串的加密與解密

來源:互聯網
上載者:User

     設計應用程式時,為了防止一些敏感資訊的泄露,通常需要對這些資訊進行加密。以使用者的登入密碼為例,如果密碼以明文的形式儲存在資料表中,很容易就會被人發現;相反,如果密碼以密文的形式儲存,即使別人從資料表中發現了密碼,也是加密之後的密碼,根本不能使用。通過對密碼進行加密,能夠極大地提高系統的保密性。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Security.Cryptography;using System.IO;namespace ConsoleApplication1{    class Program    {        static string encryptKey = "Oyea";    //定義密鑰         #region 加密字串         /// <summary> /// 加密字串          /// </summary>         /// <param name="str">要加密的字串</param>         /// <returns>加密後的字串</returns>         static string Encrypt(string str)         {               DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();   //執行個體化加/解密類對象                byte[] key = Encoding.Unicode.GetBytes(encryptKey); //定義位元組數組,用來儲存體金鑰                byte[] data = Encoding.Unicode.GetBytes(str);//定義位元組數組,用來儲存要加密的字串                MemoryStream MStream = new MemoryStream(); //執行個體化記憶體流對象                 //使用記憶體流執行個體化加密流對象              CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);                CStream.Write(data, 0, data.Length);  //向加密流中寫入資料                 CStream.FlushFinalBlock();              //釋放加密流                 return Convert.ToBase64String(MStream.ToArray());//返回加密後的字串         }         #endregion        #region 解密字串          /// <summary>         /// 解密字串          /// </summary>         /// <param name="str">要解密的字串</param>         /// <returns>解密後的字串</returns>         static string Decrypt(string str)         {                 DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();   //執行個體化加/解密類對象                  byte[] key = Encoding.Unicode.GetBytes(encryptKey); //定義位元組數組,用來儲存體金鑰                 byte[] data = Convert.FromBase64String(str);//定義位元組數組,用來儲存要解密的字串                 MemoryStream MStream = new MemoryStream(); //執行個體化記憶體流對象                  //使用記憶體流執行個體化解密流對象                  CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);               CStream.Write(data, 0, data.Length);      //向解密流中寫入資料                CStream.FlushFinalBlock();               //釋放解密流                 return Encoding.Unicode.GetString(MStream.ToArray());       //返回解密後的字串         }         #endregion        static void Main(string[] args)        {            Console.Write("請輸入要加密的字串:");   //提示輸入字串                 Console.WriteLine();                  //換行輸入              string str = Console.ReadLine();     //記錄輸入的字串                 string strNew = Encrypt(str);              //加密字串                 Console.WriteLine("加密後的字串:" + strNew);  //輸出加密後的字串                 Console.WriteLine("解密後的字串:" + Decrypt(strNew)); //解密字串並輸出                Console.ReadLine();        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.