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


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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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();
         }
     }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM