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

來源:互聯網
上載者:User

標籤:

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

可以直接調用裡面的加密和解密的方法。

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進位轉換為byte位元組                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();                //需要去掉結尾的null字元                //strRetValue = Encoding.UTF8.GetString(mStream.ToArray());                strRetValue = Encoding.UTF8.GetString(mStream.ToArray()).TrimEnd(‘\0‘);            }            catch (Exception e)            {                Console.WriteLine(e);            }                        return strRetValue;        }    }}

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.