一個以前用的DES加密解密類突然解密的時候出現錯誤,怎麼搞都不行,後來找了個這個,雖然大致上差不多,但是這個確實可以的,真為以前的那個類鬱悶啊
using System;<br />using System.Text;<br />using System.Security.Cryptography; </p><p>namespace Test<br />{ </p><p> /// <summary><br /> /// DES加密 解密<br /> /// </summary><br /> public class DES<br /> {<br /> /// <summary><br /> /// 對位元組進行DES加密<br /> /// </summary><br /> /// <param name="p_Value">位元組數組</param><br /> /// <param name="p_Key">鑰匙 可以使用 System.Text.Encoding.AscII.GetBytes("ABVD") 注意必須是8位 </param><br /> /// <param name="p_IV">向量 如果為NULL 向量和鑰匙是一個</param><br /> /// <returns>加密後的BYTE</returns><br /> public static byte[] DESEncoder(byte[] p_Value, byte[] p_Key, byte[] p_IV)<br /> {<br /> byte[] _RgbKey = p_Key;<br /> byte[] _RgbIV = p_IV; </p><p> if (_RgbKey == null || _RgbKey.Length!= 8) _RgbKey = new byte[] { 0x7A, 0x67, 0x6B, 0x65, 0x40, 0x73, 0x69, 0x6E };<br /> if (_RgbIV == null) _RgbIV = _RgbKey; </p><p> DESCryptoServiceProvider _Desc = new DESCryptoServiceProvider();<br /> ICryptoTransform _ICrypto = _Desc.CreateEncryptor(_RgbKey, _RgbIV); </p><p> return _ICrypto.TransformFinalBlock(p_Value, 0, p_Value.Length);<br /> }<br /> /// <summary><br /> /// 對位元組進行DES解密<br /> /// </summary><br /> /// <param name="p_Value">位元組數組</param><br /> /// <param name="p_Key">鑰匙 可以使用 System.Text.Encoding.AscII.GetBytes("ABVD") 注意必須是8位 </param><br /> /// <param name="p_IV">向量 如果為NULL 向量和鑰匙是一個</param><br /> /// <returns>解密後的BYTE</returns><br /> public static byte[] DESDecoder(byte[] p_Value, byte[] p_Key, byte[] p_IV)<br /> {<br /> byte[] _RgbKey = p_Key;<br /> byte[] _RgbIV = p_IV; </p><p> if (_RgbKey == null || _RgbKey.Length != 8) _RgbKey = new byte[] { 0x7A, 0x67, 0x6B, 0x65, 0x40, 0x73, 0x69, 0x6E };<br /> if (_RgbIV == null) _RgbIV = _RgbKey; </p><p> DESCryptoServiceProvider _Desc = new DESCryptoServiceProvider();<br /> ICryptoTransform _ICrypto = _Desc.CreateDecryptor(_RgbKey, _RgbIV); </p><p> return _ICrypto.TransformFinalBlock(p_Value, 0, p_Value.Length);<br /> } </p><p> /// <summary><br /> /// DES加密<br /> /// </summary><br /> /// <param name="enStr">未經處理資料</param><br /> /// <param name="p_TextEncoding">資料編碼</param><br /> /// <param name="p_Key">鑰匙 可以使用 System.Text.Encoding.AscII.GetBytes("ABVD") 注意必須是8位 </param><br /> /// <param name="p_IV">向量 如果為NULL 向量和鑰匙是一個</param><br /> /// <returns>加密後的字元穿 00-00-00</returns><br /> public static string DESEncoder(string p_TextValue, System.Text.Encoding p_TextEncoding,byte[] p_Key,byte[] p_IV)<br /> {<br /> byte[] _DataByte = p_TextEncoding.GetBytes(p_TextValue);<br /> return BitConverter.ToString(DESEncoder(_DataByte,p_Key,p_IV));<br /> } </p><p> /// <summary><br /> /// DES解密<br /> /// </summary><br /> /// <param name="p_TextValue">經過加密資料</param><br /> /// <param name="p_TextEncoding">資料編碼</param><br /> /// <param name="p_Key">鑰匙 可以使用 System.Text.Encoding.AscII.GetBytes("ABVD") 注意必須是8位 </param><br /> /// <param name="p_IV">向量 如果為NULL 向量和鑰匙是一個</param><br /> /// <returns>解密後的字元穿</returns><br /> public static string DESDecoder(string p_TextValue, System.Text.Encoding p_TextEncoding,byte[] p_Key,byte[] p_IV)<br /> { </p><p> string[] _ByteText = p_TextValue.Split('-');<br /> byte[] _DataByte =new byte[_ByteText.Length];<br /> for (int i = 0; i != _ByteText.Length; i++)<br /> {<br /> _DataByte[i] = Convert.ToByte(_ByteText[i], 16);<br /> }<br /> return p_TextEncoding.GetString(DESDecoder(_DataByte,p_Key,p_IV));<br /> }<br /> } </p><p>}
使用方式
string str = "一個abc字串";
string encoder = Test.DES.DESEncoder(str, Encoding.Default, null, null); //加密後的字串
string decoder = Test.DES.DESDecoder(encoder, Encoding.Default, null, null);//解密後的字串