當前位置:網站首頁>對稱加密--DES加密 c#

對稱加密--DES加密 c#

2022-01-28 05:16:32 畹在水中芷

轉載:From <https://www.cnblogs.com/wifi/articles/2482350.html>

對稱加密--DES加密

需要密鑰

1. 加密字符串     

2. 輸出加密後的字符串     

3. 解密字符串並輸出 

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();
        }
    }
}

 

From <https://www.cnblogs.com/wifi/articles/2482350.html>

版權聲明
本文為[畹在水中芷]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/01/202201280516316357.html

隨機推薦