设计应用程序时,为了防止一些敏感信息的泄露,通常需要对这些信息进行加密。以用户的登录密码为例,如果密码以明文的形式存储在数据表中,很容易就会被人发现;相反,如果密码以密文的形式储存,即使别人从数据表中发现了密码,也是加密之后的密码,根本不能使用。通过对密码进行加密,能够极大地提高系统的保密性。

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();
        }
    }
}
 posted on 2012-05-04 10:59  纳米程序员  阅读(22338)  评论(1编辑  收藏  举报