国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

C# DES加密類,16位的加密。

2019-11-17 02:39:18
字體:
供稿:網(wǎng)友

C# DES加密類,16位的加密。

這個加密類是與java寫的DES加密不同時,自己寫的,最后與Java的加密相同了,解決了加密后不同的問題。

可以直接調(diào)用里面的加密和解密的方法。

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;using System.Security.Cryptography;using System.IO;namespace EallNum.Helper{    public class FI_DesTools    {        PRivate FI_DesTools()         {        }          private static string key = "×××××";          /// <summary>         /// 對稱加密解密的密鑰         /// </summary>         public static string Key        {             get             {                 return key;            }             set             {                 key = value;            }         }          /// <summary>         /// DES加密         /// </summary>         /// <param name="encryptString"></param>         /// <returns></returns>         public static string DesEncrypt(string strEncryptString)         {            StringBuilder strRetValue = new StringBuilder();            try            {                byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));                 byte[] keyIV = keyBytes;                byte[] inputByteArray = Encoding.UTF8.GetBytes(strEncryptString);                 DESCryptoServiceProvider provider = new DESCryptoServiceProvider();                            provider.Mode = CipherMode.ECB;//兼容其他語言的Des加密算法                  provider.Padding = PaddingMode.Zeros;//自動補0                                          MemoryStream mStream = new MemoryStream();                 CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);                 cStream.Write(inputByteArray, 0, inputByteArray.Length);                 cStream.FlushFinalBlock();                 //不使用base64編碼                //return Convert.ToBase64String(mStream.ToArray());                 //組織成16進制字符串                            foreach (byte b in mStream.ToArray())                {                    strRetValue.AppendFormat("{0:X2}", b);                }            }            catch (Exception e)            {                Console.WriteLine(e);            }            return strRetValue.ToString();        }          /// <summary>         /// DES解密         /// </summary>         /// <param name="decryptString"></param>         /// <returns></returns>                 public static string DesDecrypt(string strDecryptString)        {            string strRetValue = "";            try            {                   byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));                byte[] keyIV = keyBytes;                //不使用base64解碼                //byte[] inputByteArray = Convert.FromBase64String(decryptString);                //16進制轉(zhuǎn)換為byte字節(jié)                byte[] inputByteArray = new byte[strDecryptString.Length / 2];                for (int x = 0; x < strDecryptString.Length / 2; x++)                {                    int i = (Convert.ToInt32(strDecryptString.Substring(x * 2, 2), 16));                    inputByteArray[x] = (byte)i;                }                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();                provider.Mode = CipherMode.ECB;//兼容其他語言的Des加密算法                  provider.Padding = PaddingMode.Zeros;//自動補0                  MemoryStream mStream = new MemoryStream();                CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);                cStream.Write(inputByteArray, 0, inputByteArray.Length);                cStream.FlushFinalBlock();                //需要去掉結(jié)尾的null字符                //strRetValue = Encoding.UTF8.GetString(mStream.ToArray());                strRetValue = Encoding.UTF8.GetString(mStream.ToArray()).TrimEnd('/0');            }            catch (Exception e)            {                Console.WriteLine(e);            }                        return strRetValue;        }    }}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 济阳县| 原阳县| 民乐县| 德兴市| 永胜县| 岱山县| 崇信县| 榆社县| 河津市| 昆山市| 行唐县| 翁源县| 辽中县| 庄河市| 乐至县| 吴川市| 延边| 株洲县| 土默特右旗| 章丘市| 马龙县| 依安县| 湄潭县| 英吉沙县| 寿宁县| 冕宁县| 崇文区| 舞阳县| 巴彦淖尔市| 安宁市| 清徐县| 磴口县| 蛟河市| 灌南县| 弋阳县| 维西| 云和县| 南投市| 浮山县| 富锦市| 新安县|