C#DES暗号化クラス、16ビットの暗号化.

10877 ワード

この暗号化クラスは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;

        }

    }

}