//'加密函数 
private String Encrypt(String strText, String strEncrKey) 

    Byte[] byKey 
= {}; 
    Byte[] IV 
= {0x120x340x560x780x900xAB0xCD0xEF}; 
    
try 
    { 
        byKey 
= System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(08)); 
        DESCryptoServiceProvider des 
= new DESCryptoServiceProvider(); 
        Byte[] inputByteArray 
= Encoding.UTF8.GetBytes(strText); 
        MemoryStream ms 
= new MemoryStream(); 
        CryptoStream cs 
= new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); 
        cs.Write(inputByteArray, 
0, inputByteArray.Length); 
        cs.FlushFinalBlock(); 
        
return Convert.ToBase64String(ms.ToArray()); 
    } 
    
catch(Exception ex) 
    { 
        
return ex.Message; 
    } 
}
解密算法
 1 
 2 
 3 //'解密函数 
 4 private String Decrypt(String strText, String sDecrKey) 
 5 
 6     Byte[] byKey = {}; 
 7     Byte[] IV = {0x120x340x560x780x900xAB0xCD0xEF}; 
 8     Byte[] inputByteArray = new byte[strText.Length]; 
 9     try 
10     { 
11         byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(08)); 
12         DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
13         inputByteArray = Convert.FromBase64String(strText); 
14         MemoryStream ms = new MemoryStream(); 
15         CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); 
16         cs.Write(inputByteArray, 0, inputByteArray.Length); 
17         cs.FlushFinalBlock(); 
18         System.Text.Encoding encoding = System.Text.Encoding.UTF8; 
19         return encoding.GetString(ms.ToArray()); 
20     } 
21     catch(Exception ex) 
22     { 
23         return ex.Message; 
24     } 
25 }
引自http://dotnet.aspx.cc/ShowDetail.aspx?id=7AE7D20A-A5DA-4303-AC2D-32046BE4D086

例子源码https://files.cnblogs.com/Inertial83/Cryptography.rar
posted on 2006-06-05 14:36  冰羽  阅读(204)  评论(0)    收藏  举报