阿牛 - 专注.NET开发

如果梦想与实现之间有一道不可逾越的鸿沟,那么“执行力”就是跨越这道鸿沟的桥梁。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
//或?者?下?面?的?另?一?个?程?序?代?码?:?
using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
 
public class Test
{
    //加?密?
public string DesEncrypt(string strText, string strEncrKey) 
{ 
byte[] byKey=null; 
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
try 
{ 
byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0,8)); 
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(System.Exception error) 
{ 
MessageBox.Show(error.Message); 
return "error:" +error.Message+"\r"; 
} 
}
//解?密?函?数?
public string DesDecrypt(string strText,string sDecrKey) 
{ 
byte[] byKey = null; 
byte[] IV= {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; 
byte[] inputByteArray = new Byte[strText.Length]; 
try 
{ 
byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0,8)); 
DESCryptoServiceProvider des = new DESCryptoServiceProvider(); 
inputByteArray = Convert.FromBase64String(strText); 
MemoryStream ms = new MemoryStream(); 
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); 
cs.Write(inputByteArray, 0, inputByteArray.Length); 
cs.FlushFinalBlock(); 
System.Text.Encoding encoding = new System.Text.UTF8Encoding(); 
return encoding.GetString(ms.ToArray()); 
} 
catch(System.Exception error) 
{ 
MessageBox.Show(error.Message); 
return "error:"+error.Message+"\r"; 
} 
} 
 
 
public class EncryptStringDES 
{
 
    public static void Main(String[] args) {
        if (args.Length < 1) {
            Console.WriteLine("Usage: des_demo ", args[0]);
            return;
        }
 
        // 使?用?UTF8函?数?加?密?输?入?参?数?
        UTF8Encoding utf8Encoding = new UTF8Encoding();
        byte[] inputByteArray = utf8Encoding.GetBytes(args[0].ToCharArray());
 
        // 方?式?一?:?调?用?默?认?的?DES实?现?方?法?DES_CSP.
        DES des = DES.Create();
        // 方?式?二?:?直?接?使?用?DES_CSP()实?现?DES的?实?体?
        //DES_CSP DES = new DES_CSP();
        // 初?始?化?DES加?密?的?密?钥?和?一?个?随?机?的?、?比?特?的?初?始?化?向?量?(IV)
        Byte[] key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
        Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
        des.Key = key;
        des.IV = IV;
 
        // 建?立?加?密?流?
          SymmetricStreamEncryptor sse = des.CreateEncryptor();
 
        // 使?用?CryptoMemoryStream方?法?获?取?加?密?过?程?的?输?出?
        CryptoMemoryStream cms = new CryptoMemoryStream();
 
        // 将?SymmetricStreamEncryptor流?中?的?加?密?数?据?输?出?到?CryptoMemoryStream中?
        sse.SetSink(cms);
 
        // 加?密?完?毕?,?将?结?果?输?出?到?控?制?台?
        sse.Write(inputByteArray);
        sse.CloseStream();
 
        // 获?取?加?密?数?据?
        byte[] encryptedData = cms.Data;
 
        // 输?出?加?密?后?结?果?
        Console.WriteLine("加?密?结?果?:?");
        for (int i = 0; i < encryptedData.Length; i++) {
            Console.Write("{0:X2} ",encryptedData[i]);
        }
        Console.WriteLine();
 
        //上?面?演?示?了?如?何?进?行?加?密?,?下?面?演?示?如?何?进?行?解?密?
        SymmetricStreamDecryptor ssd = des.CreateDecryptor();
        cms = new CryptoMemoryStream();
        ssd.SetSink(cms);
        ssd.Write(encryptedData);
        ssd.CloseStream();
 
        byte[] decryptedData = cms.Data;
        char[] decryptedCharArray = utf8Encoding.GetChars(decryptedData);
        Console.WriteLine("解?密?后?数?据?:?");
        Console.Write(decryptedCharArray);
        Console.WriteLine();
    }
 
 
}
posted on 2009-09-25 05:58  阿牛-专注金融行业开发  阅读(314)  评论(0编辑  收藏  举报