?? program.cs
字號:
?/*
* 數據加密標準(DES)的C#實現(3)
* 將BitConverter.ToString的結果轉回byte[]
*
* 采用隨機的密鑰Key和初始化向量IV加密
* 使用隨機密碼的好處:系統不會產生弱密鑰
* 備注:本例與《數據加密標準(DES)的C#實現(2)》本質相同,只是采用BitConverter.ToString
* 輸出密文、密鑰和初始化向量,而不是采用Base64編碼格式
*
*
* 夏春濤 Email:xChuntao@163.com
* Blog:http://bluesky521.cnblogs.com
* 運行環境:.net2.0 framework
*/
/*
* 關于DES加密中的初始化向量IV:
* 對于給定的密鑰 k,不使用初始化向量的簡單塊密碼將同一個純文本輸入塊加密為
* 同一個密碼文本輸出塊。如果您的純文本流中有重復塊,則您的密碼文本流中也會
* 有重復塊。如果未經授權的用戶知道了您的純文本塊結構的任何信息,他們就可以
* 利用該信息來解密已知的密碼文本塊,并有可能重新獲得您的密鑰。為了防止這個
* 問題,前一個塊中的信息被混合到下一個塊的加密過程中。這樣一來,兩個相同的
* 純文本塊的輸出就變得不一樣了。由于此技術使用前一個塊加密下一個塊,因此需
* 要初始化向量來加密數據的第一個塊。
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace DES_App3
{
class Program
{
static void Main(string[] args)
{
string str_plain_text = "How are you?";
Console.WriteLine("原文:" + str_plain_text);
string KEY_64 = "";
string IV_64 = "";
string str_cypher_text = DES_Encrypt(str_plain_text, out KEY_64, out IV_64);
Console.WriteLine("密文:" + str_cypher_text);
Console.WriteLine("解密:" + DES_Decrypt(str_cypher_text, KEY_64, IV_64));
Console.WriteLine("本次密鑰:" + KEY_64);
Console.WriteLine("本次初始化向量:" + IV_64);
Console.WriteLine();
//-------------------------------
str_cypher_text = DES_Encrypt(str_plain_text, out KEY_64, out IV_64, false);
Console.WriteLine("密文:" + str_cypher_text);
Console.WriteLine("解密:" + DES_Decrypt(str_cypher_text, KEY_64, IV_64));
Console.WriteLine("本次密鑰:" + KEY_64);
Console.WriteLine("本次初始化向量:" + IV_64);
Console.WriteLine();
}
//將BitConverter.ToString字符串,如"98-ED-0S-9A",還原轉換為byte[]
static public byte[] BitStr_ToBytes(string bit_str)
{
string[] arrSplit = bit_str.Split('-');
byte[] byteTemp = new byte[arrSplit.Length];
for (int i = 0; i < byteTemp.Length; i++)
{
byteTemp[i] = byte.Parse(arrSplit[i], System.Globalization.NumberStyles.AllowHexSpecifier);
}
return byteTemp;
}
//將BitConverter.ToString字符串(不含'-'),如"98ED0S9A",還原轉換為byte[]
static public byte[] BitStr_ToBytes2(string bit_str)
{
int n = bit_str.Length / 2 - 1;
for (int i = n; i > 0; i--)
{
bit_str = bit_str.Insert(i * 2, "-");
}
return BitStr_ToBytes(bit_str);
}
//----
#region DES加密/解密
/// <summary>
/// DES加密
/// </summary>
/// <param name="str_plain_text">明文</param>
/// <param name="str_des_key">密鑰,8個字符(64bit)</param>
/// <param name="str_des_iv">初始向量,8個字符(64bit)</param>
/// <param name="hasSubSign">輸出密文、str_des_key和str_des_iv時,是否保留BitConvert.ToString中的減號</param>
/// <returns>密文</returns>
static public string DES_Encrypt(string str_plain_text, out string str_des_key, out string str_des_iv, bool hasSubSign)
{
string str_cypher_text = "";
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cst);
sw.Write(str_plain_text);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
//-----
str_cypher_text = BitConverter.ToString(ms.GetBuffer(),0,(int)ms.Length);//**
str_des_key = BitConverter.ToString(cryptoProvider.Key);//**
str_des_iv = BitConverter.ToString(cryptoProvider.IV); //**
if (!hasSubSign)
{
str_cypher_text = str_cypher_text.Replace("-", "");
str_des_key = str_des_key.Replace("-","");
str_des_iv = str_des_iv.Replace("-","");
}
return str_cypher_text;
}
static public string DES_Encrypt(string str_plain_text, out string str_des_key, out string str_des_iv)
{
return DES_Encrypt(str_plain_text, out str_des_key, out str_des_iv, true);
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="str_cypher_text">密文</param>
/// <param name="str_des_key">密鑰,8個字符(64bit)</param>
/// <param name="str_des_iv">初始向量,8個字符(64bit)</param>
/// <returns>明文</returns>
static public string DES_Decrypt(string str_cypher_text, string str_des_key, string str_des_iv)
{
byte[] byKey;
byte[] byIV;
byte[] byEnc;
try
{
if (str_cypher_text.IndexOf('-') > 0 && str_des_key.IndexOf('-') > 0 && str_des_iv.IndexOf('-') > 0)//有"-"號
{
byKey = BitStr_ToBytes(str_des_key);
byIV = BitStr_ToBytes(str_des_iv);
byEnc = BitStr_ToBytes(str_cypher_text);
}
else//無"-"號
{
byKey = BitStr_ToBytes2(str_des_key);
byIV = BitStr_ToBytes2(str_des_iv);
byEnc = BitStr_ToBytes2(str_cypher_text);
}
}
catch
{
return null;
}
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
string str_plain_text = sr.ReadToEnd();
return str_plain_text;
}
#endregion
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -