Пример #1
1
        public static string Encrypt( string strEncryptString, string strEncryptionKey)
        {
            byte[] inputByteArray;

            try
            {
                key = Encoding.UTF8.GetBytes(strEncryptionKey.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Encoding.UTF8.GetBytes(strEncryptString);

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception eX)
            {

                throw eX;
            }
        }
Пример #2
1
        public static string Decrypt(string sourceData)
        {
            // set key and initialization vector values
              byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
              byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
              try
              {
            // convert data to byte array
            byte[] encryptedDataBytes =
               Convert.FromBase64String(sourceData);

            // get source memory stream and fill it
            MemoryStream tempStream =
               new MemoryStream(encryptedDataBytes, 0,
              encryptedDataBytes.Length);

            // get decryptor and decryption stream
            DESCryptoServiceProvider decryptor =
               new DESCryptoServiceProvider();
            CryptoStream decryptionStream =
               new CryptoStream(tempStream,
              decryptor.CreateDecryptor(key, iv),
              CryptoStreamMode.Read);

            // decrypt data
            StreamReader allDataReader =
               new StreamReader(decryptionStream);
            return allDataReader.ReadToEnd();
              }
              catch
              {
            throw new StringEncryptorException(
               "Unable to decrypt data.");
              }
        }
Пример #3
1
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="pToEncrypt">需要加密字符串</param>
        /// <param name="sKey">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string pToEncrypt, string sKey)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中

                //原来使用的UTF8编码,我改成Unicode编码了,不行
                byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);

                //建立加密对象的密钥和偏移量

                //使得输入密码必须输入英文文本
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch (Exception ex)
            {

            }

            return "";
        }
Пример #4
1
 /// <summary>
 /// 解密
 /// </summary>
 /// <param name="text">要被解密字符</param>
 /// <param name="sKey">密钥</param>
 /// <returns></returns>
 public static string Decrypt(this string text, string sKey)
 {
     var provider = new DESCryptoServiceProvider();
     int num = text.Length / 2;
     byte[] buffer = new byte[num];
     try
     {
         for (int i = 0; i < num; i++)
         {
             int num3 = Convert.ToInt32(text.Substring(i * 2, 2), 0x10);
             buffer[i] = (byte)num3;
         }
     }
     catch
     {
         return string.Empty;
     }
     provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
     provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
     MemoryStream stream = new MemoryStream();
     CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
     try
     {
         stream2.Write(buffer, 0, buffer.Length);
         stream2.FlushFinalBlock();
     }
     catch
     {
         return string.Empty;
     }
     return Encoding.Default.GetString(stream.ToArray());
 }
Пример #5
1
        public static string Encode(string str, string key)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            byte[] bytes = Encoding.UTF8.GetBytes(str);

            MemoryStream stream = new MemoryStream();

            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);

            stream2.Write(bytes, 0, bytes.Length);

            stream2.FlushFinalBlock();

            StringBuilder builder = new StringBuilder();

            foreach (byte num in stream.ToArray())
            {

                builder.AppendFormat("{0:X2}", num);

            }

            stream.Close();

            return builder.ToString();
        }
Пример #6
0
Файл: Des.cs Проект: Fun33/code
    ///    <summary>
    /// 字串解密
    /// </summary>
    public static string Decrypt(string pToDecrypt)
    {
        if (pToDecrypt == string.Empty)
        {
            return(string.Empty);
        }

        try
        {
            System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
            //'把字符串放入byte數組
            int len = 0;
            len = pToDecrypt.Length / 2 - 1;
            byte[] inputByteArray = new byte[len + 1];
            int    x = 0;
            int    i = 0;
            for (x = 0; x <= len; x++)
            {
                i = Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16);
                inputByteArray[x] = Convert.ToByte(i);
            }
            //'建立加密對象的密鑰和偏移量,此值重要,不能修改
            des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(skey);
            des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(skey);
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return(System.Text.Encoding.Default.GetString(ms.ToArray()));
        }
        catch (Exception ex)
        {
            return(ex.Message);
        }
    }
Пример #7
0
Файл: Des.cs Проект: Fun33/code
    ///    <summary>
    /// 字串加密
    /// </summary>
    public static string Encrypt(string pToEncrypt)
    {
        if (pToEncrypt == string.Empty)
        {
            return(string.Empty);
        }

        System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
        byte[] inputByteArray = null;
        inputByteArray = System.Text.Encoding.Default.GetBytes(pToEncrypt);
        //'建立加密對象的密鑰和偏移量
        //'原文使用ASCIIEncoding.ASCII方法的GetBytes方法
        //'使得輸入密碼必須輸入英文文本
        des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(skey);
        des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(skey);
        //'寫二進制數組到加密流
        //'(把內存流中的內容全部寫入)
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
        //'寫二進制數組到加密流
        //'(把內存流中的內容全部寫入)
        cs.Write(inputByteArray, 0, inputByteArray.Length);
        cs.FlushFinalBlock();
        //'建立輸出字符串
        System.Text.StringBuilder ret = new System.Text.StringBuilder();
        byte b = 0;

        foreach (byte b_loopVariable in ms.ToArray())
        {
            b = b_loopVariable;
            ret.AppendFormat("{0:X2}", b);
        }
        return(ret.ToString());
    }
Пример #8
0
        public static SymmetricAlgorithm GetAlgorithmByName(String algorithmName)
        {
            SymmetricAlgorithm symmetricAlgorithm     = null;
            String             algorithmNameLoverCase = algorithmName.ToLower();

            switch (algorithmNameLoverCase)
            {
            case "aes":
                symmetricAlgorithm = new System.Security.Cryptography.AesCryptoServiceProvider();
                break;

            case "des":
                symmetricAlgorithm = new System.Security.Cryptography.DESCryptoServiceProvider();
                break;

            case "rc2":
                symmetricAlgorithm = new System.Security.Cryptography.RC2CryptoServiceProvider();
                break;

            case "rijndael":
                symmetricAlgorithm = new System.Security.Cryptography.RijndaelManaged();
                break;
            }
            return(symmetricAlgorithm);
        }
Пример #9
0
        /// <summary> 
        /// �������� 
        /// </summary> 
        /// <param name="Text">ԭ��</param> 
        /// <param name="sKey">��Կ</param> 
        /// <returns>����</returns> 
        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider desKey = new DESCryptoServiceProvider();

            byte[] inputByteArray = Encoding.Default.GetBytes(Text);
            byte[] keyByteArray = Encoding.Default.GetBytes(sKey);

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            md5.ComputeHash(keyByteArray);

            desKey.Key = HalveByteArray(md5.Hash);
            desKey.IV = HalveByteArray(md5.Hash);

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, desKey.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder result = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                result.AppendFormat("{0:X2}", b);
            }

            return result.ToString();
        }
Пример #10
0
            public static string Encrypt(string Text, string sKey)
            {
                string result;

                if (string.IsNullOrEmpty(Text))
                {
                    result = "";
                }
                else
                {
                    string text = SecretCommon.Md5Hex.Encrypt(sKey, false);
                    System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
                    byte[] bytes = System.Text.Encoding.Default.GetBytes(Text);
                    dESCryptoServiceProvider.Key = System.Text.Encoding.ASCII.GetBytes(text.Substring(8, 8));
                    dESCryptoServiceProvider.IV  = System.Text.Encoding.ASCII.GetBytes(text.Substring(0, 8));
                    MemoryStream memoryStream = new MemoryStream();
                    System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                    cryptoStream.Write(bytes, 0, bytes.Length);
                    cryptoStream.FlushFinalBlock();
                    System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
                    byte[] array = memoryStream.ToArray();
                    for (int i = 0; i < array.Length; i++)
                    {
                        byte b = array[i];
                        stringBuilder.AppendFormat("{0:X2}", b);
                    }
                    result = stringBuilder.ToString();
                }
                return(result);
            }
Пример #11
0
        /// <解密>
        /// 解密
        /// </解密>
        /// <param name="data"></param>
        /// <param name="byKey"></param>
        /// <param name="byIv"></param>
        /// <returns></returns>
        public static string Decode(string data, byte[] byKey, byte[] byIv)
        {
            try
            {
                byte[] byEnc;

                byEnc = Convert.FromBase64String(data); //把需要解密的字符串转为8位无符号数组

                System.Security.Cryptography.DESCryptoServiceProvider cryptoProvider = new System.Security.Cryptography.DESCryptoServiceProvider();

                MemoryStream ms = new MemoryStream(byEnc);

                System.Security.Cryptography.CryptoStream cst = new System.Security.Cryptography.CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIv)
                                                                                                              , System.Security.Cryptography.CryptoStreamMode.Read);

                StreamReader sr = new StreamReader(cst);

                return(sr.ReadToEnd());
            }

            catch (Exception x)
            {
                return(x.Message);
            }
        }
        public static string EncryptString(string _inputString,
           string sKey)
        {
            string sInputFilename = string.Empty;
            string sOutputFilename = string.Empty;
            FileStream fsInput = new FileStream(_inputString,
               FileMode.Open,
               FileAccess.Read);

            FileStream fsEncrypted = new FileStream(sOutputFilename,
               FileMode.Create,
               FileAccess.Write);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            CryptoStream cryptostream = new CryptoStream(fsEncrypted,
               desencrypt,
               CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[_inputString.Length];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Close();
            fsInput.Close();
            fsEncrypted.Close();
            return bytearrayinput.ToString();
        }
Пример #13
0
    /// <summary>
    /// 加密文件
    /// </summary>
    public static void EncryptFile(string Value, string OuputFileName)
    {
        try
        {
            // Must be 64 bits, 8 bytes.
            // Distribute this key to the user who will decrypt this file.
            //Get the Key for the file to Encrypt.
            string sKey = GenerateKey();

            byte[] b = ASCIIEncoding.ASCII.GetBytes(Value);

            System.IO.FileStream fsEncrypted = new System.IO.FileStream(OuputFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);

            System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV  = ASCIIEncoding.ASCII.GetBytes(sKey);

            System.Security.Cryptography.ICryptoTransform desencrypt = DES.CreateEncryptor();
            System.Security.Cryptography.CryptoStream     cs         = new System.Security.Cryptography.CryptoStream(fsEncrypted, desencrypt, System.Security.Cryptography.CryptoStreamMode.Write);

            cs.Write(b, 0, b.Length);
            cs.Close();

            fsEncrypted.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
 /// <summary>
 /// 加密字符串
 /// </summary>
 /// <param name="encryptString"></param>
 /// <returns></returns>
 public static string Encrypt(string encryptString)
 {
     System.IO.MemoryStream mStream = null;
     System.Security.Cryptography.CryptoStream cStream = null;
     try
     {
         EncryptUtil des            = new EncryptUtil();
         byte[]      rgbKey         = Encoding.UTF8.GetBytes(des.encryptKey.Substring(0, 8));
         byte[]      rgbIV          = Encoding.UTF8.GetBytes(des.encryptIV.Substring(0, 8));
         byte[]      inputByteArray = Encoding.UTF8.GetBytes(encryptString);
         System.Security.Cryptography.DESCryptoServiceProvider dCSP = new System.Security.Cryptography.DESCryptoServiceProvider();
         mStream = new System.IO.MemoryStream();
         cStream = new System.Security.Cryptography.CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write);
         cStream.Write(inputByteArray, 0, inputByteArray.Length);
         cStream.FlushFinalBlock();
         return(Convert.ToBase64String(mStream.ToArray()));
     }
     catch (Exception)
     {
         return(encryptString);
     }
     finally
     {
         if (mStream != null)
         {
             mStream.Close(); mStream.Dispose();
         }
         if (cStream != null)
         {
             cStream.Close(); cStream.Dispose();
         }
     }
 }
Пример #15
0
        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="pToDecrypt">需要解密的字符串</param>
        /// <param name="sKey">密匙</param>
        /// <returns>解密后的字符串</returns>
        public static string Decrypt(string pToDecrypt, string sKey)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                //建立加密对象的密钥和偏移量,此值重要,不能修改
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
                StringBuilder ret = new StringBuilder();
                return System.Text.Encoding.Default.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {

            }
            return "";
        }
Пример #16
0
        /// <summary>
        /// 解密字符串
        /// </summary>
        public static string Decrypt(string CypherText)
        {
            System.Security.Cryptography.DESCryptoServiceProvider key = new System.Security.Cryptography.DESCryptoServiceProvider();
            byte[] bk = System.Text.Encoding.Unicode.GetBytes(CryKey);
            byte[] bs = new byte[8];
            for (int i = 0; i < bs.Length; i++)
            {
                bs[i] = bk[i];
            }
            key.Key = bs;
            key.IV  = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };

            byte[] bc = new byte[CypherText.Length / 2];
            for (int i = 0; i < bc.Length; i++)
            {
                try
                {
                    bc[i] = Convert.ToByte(CypherText.Substring(2 * i, 2), 16);
                }
                catch { }
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream(bc);
            System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(ms, key.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Read);
            System.IO.StreamReader sr = new System.IO.StreamReader(encStream);

            string val = sr.ReadLine();

            sr.Close();
            encStream.Close();
            ms.Close();

            return(val);
        }
Пример #17
0
        private static string CryKey = "Xky_Lq_Py_Hu_Lp_Jhj_Zxt";//密钥
        /// <summary>
        /// 加密字符串
        /// </summary>
        public static string Encrypt(string PlainText)
        {
            System.Security.Cryptography.DESCryptoServiceProvider key = new System.Security.Cryptography.DESCryptoServiceProvider();
            byte[] bk = System.Text.Encoding.Unicode.GetBytes(CryKey);
            byte[] bs = new byte[8];
            for (int i = 0; i < bs.Length; i++)
            {
                bs[i] = bk[i];
            }
            key.Key = bs;
            key.IV  = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };

            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            System.Security.Cryptography.CryptoStream encStream = new System.Security.Cryptography.CryptoStream(ms, key.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);

            System.IO.StreamWriter sw = new System.IO.StreamWriter(encStream);
            sw.WriteLine(PlainText);
            sw.Close();
            encStream.Close();

            byte[] buffer = ms.ToArray();
            ms.Close();

            string s = "";

            for (int i = 0; i < buffer.Length; i++)
            {
                s += buffer[i].ToString("X2");
            }
            return(s);
        }
Пример #18
0
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sIn"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public string Encrypt(string sIn, string sKey = "ColtSmart")
        {
            byte[] inputByteArray = System.Text.Encoding.ASCII.GetBytes(sIn);
            using (System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider())
            {
                des.Mode    = CipherMode.ECB;
                des.Padding = PaddingMode.Zeros;
                byte[] keyByteArray      = new byte[8];
                byte[] inputKeyByteArray = ASCIIEncoding.ASCII.GetBytes(sKey);
                for (int i = 0; i < 8; i++)
                {
                    if (inputKeyByteArray.Length > i)
                    {
                        keyByteArray[i] = inputKeyByteArray[i];
                    }
                    else
                    {
                        keyByteArray[i] = 0;
                    }
                }

                des.Key = keyByteArray;
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return(str);
            }
        }
Пример #19
0
 //The function used to encrypt the text
 private static string Encrypt(string strText, string strEncrKey)
 {
     byte[] byKey =
     {
     };
     byte[] IV =
     {
         0x12,
         0x34,
         0x56,
         0x78,
         0x90,
         0xab,
         0xcd,
         0xef
     };
     try
     {
         byKey = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8));
         System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
         byte[]       inputByteArray = Encoding.UTF8.GetBytes(strText);
         MemoryStream ms             = new MemoryStream();
         CryptoStream cs             = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
         cs.Write(inputByteArray, 0, inputByteArray.Length);
         cs.FlushFinalBlock();
         return(Convert.ToBase64String(ms.ToArray()));
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
Пример #20
0
        public static string Decode(string str, string key)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

            provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));

            byte[] buffer = new byte[str.Length / 2];

            for (int i = 0; i < (str.Length / 2); i++)
            {

                int num2 = Convert.ToInt32(str.Substring(i * 2, 2), 0x10);

                buffer[i] = (byte)num2;

            }

            MemoryStream stream = new MemoryStream();

            CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);

            stream2.Write(buffer, 0, buffer.Length);

            stream2.FlushFinalBlock();

            stream.Close();

            return Encoding.GetEncoding("UTF-8").GetString(stream.ToArray());
        }
Пример #21
0
        public static string Decrypt(string strStringDecrypt, string strEncryptionKey)
        {
            byte[] inputByteArray = new byte[strStringDecrypt.Length];

            try
            {
                key = Encoding.UTF8.GetBytes(strEncryptionKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                inputByteArray = Convert.FromBase64String(strStringDecrypt.Replace(" ", "+"));

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                Encoding encoding = Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception eX)
            {

                throw eX;
            }
        }
Пример #22
0
 public static string Decrypt(string strText)
 {
     string text1;
     if (strText == String.Empty)
     {
         return strText;
     }
     byte[] buffer1;
     byte[] buffer2;
     byte[] buffer3 = GetCryptArr();
     try
     {
         buffer1 = Encoding.UTF8.GetBytes(GetCryptKey().Substring(0,8));
         DESCryptoServiceProvider provider1 =  new DESCryptoServiceProvider();
         buffer2 = Convert.FromBase64String(strText);
         MemoryStream stream2 = new MemoryStream();
         using (CryptoStream stream1 = new CryptoStream(stream2, provider1.CreateDecryptor(buffer1, buffer3), CryptoStreamMode.Write))
         {
             stream1.Write(buffer2, 0, buffer2.Length);
             stream1.FlushFinalBlock();
             text1 = Encoding.UTF8.GetString(stream2.ToArray());
         }
     }
     catch
     {
         return "Error";
     }
     return text1;
 }
Пример #23
0
        /// <summary>
        /// DES 加密算法
        /// </summary>
        /// <param name="str">待加密字符串</param>
        /// <returns>加密后字符串</returns>
        public static string EncodeStr(string str)
        {
            try
            {
                byte[] temp = Encoding.UTF8.GetBytes(str);

                using (DESCryptoServiceProvider desc = new DESCryptoServiceProvider())
                {
                    desc.Key = ASCIIEncoding.ASCII.GetBytes(key); desc.IV = ASCIIEncoding.ASCII.GetBytes(iv);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(temp, 0, temp.Length);

                            cs.FlushFinalBlock();
                        }

                        StringBuilder sb = new StringBuilder();

                        foreach (byte b in ms.ToArray())
                        {
                            sb.AppendFormat("{0:X2}", b);
                        }
                        return sb.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
 /// <summary>
 /// DES加密
 /// </summary>
 /// <param name="str"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string DESEncrypt(string str, string key)
 {
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     //把字符串放到byte数组中 
     byte[] inputByteArray = Encoding.Default.GetBytes(str);
     //建立加密对象的密钥和偏移量  
     //原文使用ASCIIEncoding.ASCII方法的GetBytes方法  
     //使得输入密码必须输入英文文本  
     des.Key = ASCIIEncoding.ASCII.GetBytes(key);
     des.IV = ASCIIEncoding.ASCII.GetBytes(key);
     MemoryStream ms = new MemoryStream();
     CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
     //Write  the  byte  array  into  the  crypto  stream  
     //(It  will  end  up  in  the  memory  stream)  
     cs.Write(inputByteArray, 0, inputByteArray.Length);
     cs.FlushFinalBlock();
     //Get  the  data  back  from  the  memory  stream,  and  into  a  string  
     StringBuilder ret = new StringBuilder();
     foreach (byte b in ms.ToArray())
     {
         //Format  as  hex  
         ret.AppendFormat("{0:X2}", b);
     }
     ret.ToString();
     return ret.ToString();
 }
Пример #25
0
        /// <summary>
        /// 用给定的Key进行解密
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="decryptString">要解密的字符串</param>
        /// <returns>解密后的字符串</returns>
        public static string Decrypt(string decryptString, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                key = _Key;
            else
                key += _Key;

            if (string.IsNullOrWhiteSpace(decryptString))
                decryptString = string.Empty;

            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;

            byte[] inputByteArray = new byte[decryptString.Length / 2];
            for (int x = 0; x < decryptString.Length / 2; x++)
            {
                int i = (Convert.ToInt32(decryptString.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                using (MemoryStream mStream = new MemoryStream())
                {
                    using (CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write))
                    {
                        cStream.Write(inputByteArray, 0, inputByteArray.Length);
                        cStream.FlushFinalBlock();
                        return Encoding.UTF8.GetString(mStream.ToArray());
                    }
                }
            }
        }
Пример #26
0
        private static string DESEncrypt(string MainKey, string strPlain)
        {
            string g_strDESKey = ComputeMD5HashHEX(MainKey + g_strDESIV).Substring(0, 8);
            string p_strReturn = string.Empty;

            try
            {
                string strDESKey   = g_strDESKey;
                string strDESIV    = g_strDESIV;
                byte[] bytesDESKey = System.Text.ASCIIEncoding.ASCII.GetBytes(strDESKey);
                byte[] bytesDESIV  = System.Text.ASCIIEncoding.ASCII.GetBytes(strDESIV);
                System.Security.Cryptography.DESCryptoServiceProvider desEncrypt = new System.Security.Cryptography.DESCryptoServiceProvider();
                System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream();
                System.Security.Cryptography.CryptoStream csEncrypt = new System.Security.Cryptography.CryptoStream(msEncrypt, desEncrypt.CreateEncryptor(bytesDESKey, bytesDESIV), System.Security.Cryptography.CryptoStreamMode.Write);
                System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt);
                swEncrypt.WriteLine(strPlain);
                swEncrypt.Close();
                csEncrypt.Close();
                byte[] bytesCipher = msEncrypt.ToArray();
                msEncrypt.Close();
                p_strReturn = ConvertByteArrayToHex(bytesCipher);
            }
            catch (System.Exception) { }
            return(p_strReturn);
        }
Пример #27
0
        public void EncryptFile(string sInputFilename,string sOutputFilename,string sKey)
        {
            var fsInput = new FileStream(sInputFilename,
                FileMode.Open,
                FileAccess.Read);

            var fsEncrypted = new FileStream(sOutputFilename,
                            FileMode.Create,
                            FileAccess.Write);
            var DES = new DESCryptoServiceProvider();
            
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            ICryptoTransform desencrypt = DES.CreateEncryptor();
            var cryptostream = new CryptoStream(fsEncrypted,
                                desencrypt,
                                CryptoStreamMode.Write);

            byte[] bytearrayinput = new byte[fsInput.Length - 1];
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

            cryptostream.Close();
            fsInput.Close();
            fsEncrypted.Close();

        }
Пример #28
0
        public static string Encrypt(string text)
        {

            byte[] encodedkey;
            byte[] iv = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A, 0x78, 0xA7 };
            byte[] bytes;

            encodedkey = Encoding.UTF8.GetBytes(EncryptKey);
            DESCryptoServiceProvider csp = new DESCryptoServiceProvider();

            bytes = Encoding.UTF8.GetBytes(text);
            MemoryStream ms = new MemoryStream();

            try
            {
                CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(encodedkey, iv), CryptoStreamMode.Write);

                cs.Write(bytes, 0, bytes.Length);
                cs.FlushFinalBlock();

            }
            catch (Exception ex)
            {
                return "";
            }

            return Convert.ToBase64String(ms.ToArray());

        }
Пример #29
0
        /// <summary>
        /// 对流进行加密或解密
        /// </summary>
        /// <param name="inS">输入流</param>
        /// <param name="outS">输出流</param>
        /// <param name="desKey">密钥</param>
        /// <param name="desIV">向量</param>
        /// <param name="isEncrypt">是否加密</param>
        /// <returns>成功返回true,失败返回false</returns>
        private static bool cryptIt(Stream inS, Stream outS, byte[] desKey, byte[] desIV, bool isEncrypt)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                ICryptoTransform mode = null;
                if (isEncrypt)
                    mode = des.CreateEncryptor(desKey, desIV);
                else
                    mode = des.CreateDecryptor(desKey, desIV);
                CryptoStream cs = new CryptoStream(inS, mode, CryptoStreamMode.Read);

                byte[] bin = new byte[100];
                int len;
                while ((len = cs.Read(bin, 0, bin.Length)) != 0)
                {
                    outS.Write(bin, 0, len);
                }

                //流位置重置
                inS.Position = 0;
                outS.Position = 0;
                return true;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "错误");
                return false;
            }
        }
Пример #30
0
        /// <summary>
        /// 用给定的Key进行加密
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="encryptString">要加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string encryptString, string key)
        {
            if (string.IsNullOrWhiteSpace(key))
                key = _Key;
            else
                key += _Key;

            if (string.IsNullOrWhiteSpace(encryptString))
                encryptString = string.Empty;

            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
            byte[] keyIV = keyBytes;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
            {
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();

                //Get the data back from the memory stream, and into a string
                StringBuilder ret = new StringBuilder();
                foreach (byte b in mStream.ToArray())
                {
                    //Format as hex
                    ret.AppendFormat("{0:X2}", b);
                }
                return ret.ToString();
            }
        }
Пример #31
0
        public static string DESDeCode(string pToDecrypt, string sKey)
        {
            //    HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);   
            //    HttpContext.Current.Response.End();   
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();

            // return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
            return System.Text.Encoding.Default.GetString(ms.ToArray());
        }
Пример #32
0
        public static string Decrypt(string text)
        {
            try
            {
                key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] byteArray = Convert.FromBase64String(text);

                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream,
                    des.CreateDecryptor(key, IV), CryptoStreamMode.Write);

                cryptoStream.Write(byteArray, 0, byteArray.Length);
                cryptoStream.FlushFinalBlock();

                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            catch (Exception ex)
            {
                // Handle Exception Here
            }

            return string.Empty;
        }
Пример #33
0
 /// <summary>
 /// 加密字符串
 /// </summary>
 /// <param name="inputStr">输入字符串</param>
 /// <param name="keyStr">密码,可以为“”</param>
 /// <returns>输出加密后字符串</returns>
 public string EncryptString(string inputStr, string keyStr)
 {
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     if (keyStr == "")
         keyStr = key;
     byte[] inputByteArray = Encoding.Default.GetBytes(inputStr);
     byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
     SHA1 ha = new SHA1Managed();
     byte[] hb = ha.ComputeHash(keyByteArray);
     sKey = new byte[8];
     sIV = new byte[8];
     for (int i = 0; i < 8; i++)
         sKey[i] = hb[i];
     for (int i = 8; i < 16; i++)
         sIV[i - 8] = hb[i];
     des.Key = sKey;
     des.IV = sIV;
     MemoryStream ms = new MemoryStream();
     CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
     cs.Write(inputByteArray, 0, inputByteArray.Length);
     cs.FlushFinalBlock();
     StringBuilder ret = new StringBuilder();
     foreach (byte b in ms.ToArray())
     {
         ret.AppendFormat("{0:X2}", b);
     }
     cs.Close();
     ms.Close();
     return ret.ToString();
 }
Пример #34
0
        public static string DESEnCode(string pToEncrypt, string sKey)
        {
            // string pToEncrypt1 = HttpContext.Current.Server.UrlEncode(pToEncrypt);   
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);

            //建立加密对象的密钥和偏移量    
            //原文使用ASCIIEncoding.ASCII方法的GetBytes方法    
            //使得输入密码必须输入英文文本    
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();
        }
Пример #35
0
        /// <summary>
        /// encrypt a file
        /// </summary>
        /// <param name="unencryptedFileName">name of the plain text file</param>
        /// <param name="encryptedFileName">name of the encrypted file to create and write to</param>
        /// <param name="key">the key to use to encrypt the file</param>
        public static void Encrypt(string unencryptedFileName, string encryptedFileName, string key)
        {
            // create file IO streams
            FileStream openStream = new FileStream(unencryptedFileName, FileMode.Open, FileAccess.Read);
            FileStream writeStream = new FileStream(encryptedFileName, FileMode.Create, FileAccess.Write);

            // create the crypto service provider
            DESCryptoServiceProvider crypto = new DESCryptoServiceProvider();

            // set the key and initialization vector
            byte[] keyBytes = ASCIIEncoding.ASCII.GetBytes(key);
            crypto.Key = keyBytes;
            crypto.IV = keyBytes;

            // create the CtryptoStream object to transform plain text to encrypted and write it to disk
            ICryptoTransform transform = crypto.CreateEncryptor();
            CryptoStream cryptoStream = new CryptoStream(writeStream, transform, CryptoStreamMode.Write);
            byte[] unencryptedBytes = new byte[openStream.Length];
            openStream.Read(unencryptedBytes, 0, unencryptedBytes.Length);

            // write to disk
            cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);

            // close connections
            openStream.Close();
            cryptoStream.Close();
            writeStream.Close();
        }
Пример #36
0
		/// <summary> 
		/// 解密数据 
		/// </summary> 
		/// <param name="Text"></param> 
		/// <param name="sKey"></param> 
		/// <returns></returns> 
		public static string Decrypt(string Text,string sKey) 
		{
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                int len;
                len = Text.Length / 2;
                byte[] inputByteArray = new byte[len];
                int x, i;
                for (x = 0; x < len; x++)
                {
                    i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
                    inputByteArray[x] = (byte)i;
                }

                //.net framework 4.5方法过期
                //des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); 
                //des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                MD5 md5Hash = MD5.Create();
                des.Key = ASCIIEncoding.ASCII.GetBytes(GetMd5Hash(sKey, md5Hash).Substring(0, 8));
                des.IV = ASCIIEncoding.ASCII.GetBytes(GetMd5Hash(sKey, md5Hash).Substring(0, 8));

                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Encoding.Default.GetString(ms.ToArray());
            }
            catch 
            {
                return null;
            }
		} 
Пример #37
0
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="pToEncrypt">需要加密字符串</param>
        /// <param name="sKey">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string DESEncrypt(string pToEncrypt)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                //把字符串放到byte数组中

                //原来使用的UTF8编码,我改成Unicode编码了,不行
                byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);

                //建立加密对象的密钥和偏移量

                //使得输入密码必须输入英文文本
                des.Key = Encoding.UTF8.GetBytes(sKey);
                des.IV = Encoding.UTF8.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                StringBuilder ret = new StringBuilder();
                foreach (byte b in ms.ToArray())
                {
                    ret.AppendFormat("{0:X2}", b);
                }
                ret.ToString();
                return ret.ToString();
            }
            catch (Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write("写入配置信息失败,详细信息:" + ex.Message.Replace("\r\n", "").Replace("'", ""));
            }

            return "";
        }
        public static string DESDecrypt(string Value, string Key)
        {
            if (Value.Trim().Equals(string.Empty))
                return string.Empty;

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = new byte[Value.Length / 2];
            for (int x = 0; x < Value.Length / 2; x++)
            {
                int i = (Convert.ToInt32(Value.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(Key);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder sb = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                sb.Append((char)b);
            }
            return sb.ToString();
        }
Пример #39
0
        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="pToDecrypt">需要解密的字符串</param>
        /// <param name="sKey">密匙</param>
        /// <returns>解密后的字符串</returns>
        public static string DESDecrypt(string pToDecrypt)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
                for (int x = 0; x < pToDecrypt.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                //建立加密对象的密钥和偏移量,此值重要,不能修改
                des.Key = Encoding.UTF8.GetBytes(sKey);
                des.IV = Encoding.UTF8.GetBytes(sKey);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
                StringBuilder ret = new StringBuilder();
                return System.Text.Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                System.Web.HttpContext.Current.Response.Write("读取配置信息失败,详细信息:" + ex.Message.Replace("\r\n", "").Replace("'", ""));
            }
            return "";
        }
Пример #40
0
		public override void Load()
		{
			if ((File.Exists(DataFilename)) && (new FileInfo(DataFilename).Length != 0))
			{
				try
				{
					using (Stream stream = File.Open(DataFilename, FileMode.Open))
					using (DESCryptoServiceProvider crypt = new DESCryptoServiceProvider())
					{
						BinaryFormatter bin = new BinaryFormatter();


						crypt.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");
						crypt.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");

						CryptoStream crStream = new CryptoStream(stream,
	crypt.CreateDecryptor(), CryptoStreamMode.Read);

						_list = (List<MB>)bin.Deserialize(crStream);

						crStream.Close();
					}
				}
				catch (Exception)
				{
				}
			}
		}
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DESDecrypt(string str, string key)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            //Put  the  input  string  into  the  byte  array  //把pToDecrypt中的32位字符每两位以
            //16进制转换为十进制的数字(如:DE = 222)
            //然后存放在inputByteArray数组里面
            byte[] inputByteArray = new byte[str.Length / 2];
            for (int x = 0; x < str.Length / 2; x++)
            {
                int i = (Convert.ToInt32(str.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            //建立加密对象的密钥和偏移量,此值重要,不能修改  
            des.Key = ASCIIEncoding.ASCII.GetBytes(key);
            des.IV = ASCIIEncoding.ASCII.GetBytes(key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            //Flush  the  data  through  the  crypto  stream  into  the  memory  stream  
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            //Get  the  decrypted  data  back  from  the  memory  stream  
            //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象  
            StringBuilder ret = new StringBuilder();

            return System.Text.Encoding.Default.GetString(ms.ToArray());

        }
Пример #42
0
        /// <summary>
        /// DES 解密算法
        /// </summary>
        /// <param name="str">待解密字符串</param>
        /// <returns>解密后字符串</returns>
        public static string DecodeStr(string str)
        {
            try
            {
                byte[] temp = new byte[str.Length / 2];

                using (DESCryptoServiceProvider desc = new DESCryptoServiceProvider())
                {
                    for (int i = 0; i < str.Length / 2; i++)
                    {
                        int j = Convert.ToInt32(str.Substring(i * 2, 2), 16);

                        temp[i] = (byte)j;
                    }

                    desc.Key = ASCIIEncoding.ASCII.GetBytes(key); desc.IV = ASCIIEncoding.ASCII.GetBytes(iv);

                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(temp, 0, temp.Length);

                            cs.FlushFinalBlock();
                        }
                        return System.Text.Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #43
0
        public static string DecryptString(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return("");
            }

            //DESCryptoServiceProviderオブジェクトの作成
            using (var des = new System.Security.Cryptography.DESCryptoServiceProvider())
            {
                //共有キーと初期化ベクタを決定
                //パスワードをバイト配列にする
                var bytesKey = Encoding.UTF8.GetBytes("_tween_encrypt_key_");
                //共有キーと初期化ベクタを設定
                des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
                des.IV  = ResizeBytesArray(bytesKey, des.IV.Length);

                //Base64で文字列をバイト配列に戻す
                var bytesIn = Convert.FromBase64String(str);

                MemoryStream     msIn        = null;
                ICryptoTransform desdecrypt  = null;
                CryptoStream     cryptStreem = null;

                try
                {
                    //暗号化されたデータを読み込むためのMemoryStream
                    msIn = new MemoryStream(bytesIn);
                    //DES復号化オブジェクトの作成
                    desdecrypt = des.CreateDecryptor();
                    //読み込むためのCryptoStreamの作成
                    cryptStreem = new CryptoStream(msIn, desdecrypt, CryptoStreamMode.Read);

                    //Disposeが重複して呼ばれないようにする
                    msIn       = null;
                    desdecrypt = null;

                    //復号化されたデータを取得するためのStreamReader
                    using (StreamReader srOut = new StreamReader(cryptStreem, Encoding.UTF8))
                    {
                        //Disposeが重複して呼ばれないようにする
                        cryptStreem = null;

                        //復号化されたデータを取得する
                        var result = srOut.ReadToEnd();

                        return(result);
                    }
                }
                finally
                {
                    msIn?.Dispose();
                    desdecrypt?.Dispose();
                    cryptStreem?.Dispose();
                }
            }
        }
Пример #44
0
 public static byte[] DESDecrypt(byte[] data, byte[] cryptKey = null, byte[] cryptIV = null)
 {
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = EncryptHelper.CreateDESCrypto();
     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
     System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(cryptKey, cryptIV), System.Security.Cryptography.CryptoStreamMode.Write);
     cryptoStream.Write(data, 0, data.Length);
     cryptoStream.FlushFinalBlock();
     return(memoryStream.ToArray());
 }
Пример #45
0
 public static byte[] smethod_22(byte[] byte_0, string string_0)
 {
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     dESCryptoServiceProvider.Key     = System.Text.Encoding.UTF8.GetBytes(string_0);
     dESCryptoServiceProvider.Mode    = System.Security.Cryptography.CipherMode.ECB;
     dESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cryptoTransform = dESCryptoServiceProvider.CreateDecryptor();
     byte[] array = cryptoTransform.TransformFinalBlock(byte_0, 0, byte_0.Length);
     dESCryptoServiceProvider.Clear();
     System.Array.Resize <byte>(ref array, array.Length - 1);
     return(array);
 }
Пример #46
0
 public static byte[] smethod_21(byte[] byte_0, string string_0)
 {
     byte[] array = byte_0;
     System.Array.Resize <byte>(ref array, array.Length + 1);
     array[array.Length - 1] = (byte)new System.Random().Next(0, 255);
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     dESCryptoServiceProvider.Key     = System.Text.Encoding.UTF8.GetBytes(string_0);
     dESCryptoServiceProvider.Mode    = System.Security.Cryptography.CipherMode.ECB;
     dESCryptoServiceProvider.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
     System.Security.Cryptography.ICryptoTransform cryptoTransform = dESCryptoServiceProvider.CreateEncryptor();
     byte[] result = cryptoTransform.TransformFinalBlock(array, 0, array.Length);
     dESCryptoServiceProvider.Clear();
     return(result);
 }
Пример #47
0
 public static string Encrypt(string encryptString, string encryptKey)
 {
     encryptKey = TextUtility.CutLeft(encryptKey, 8);
     encryptKey = encryptKey.PadRight(8, ' ');
     byte[] bytes  = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
     byte[] keys   = DES.Keys;
     byte[] bytes2 = System.Text.Encoding.UTF8.GetBytes(encryptString);
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
     System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(bytes, keys), System.Security.Cryptography.CryptoStreamMode.Write);
     cryptoStream.Write(bytes2, 0, bytes2.Length);
     cryptoStream.FlushFinalBlock();
     return(System.Convert.ToBase64String(memoryStream.ToArray()));
 }
Пример #48
0
 //加密的代码:
 /// <summary>
 /// DES加密
 /// </summary>
 /// <param name="str">要加密字符串</param>
 /// <returns>返回加密后字符串</returns>
 public static String Encrypt_DES(String str)
 {
     System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
     Byte[] inputByteArray = System.Text.Encoding.Default.GetBytes(str);
     des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strDesKey, "md5").Substring(0, 8)); //加密对象的密钥
     des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strDesKey, "md5").Substring(0, 8)); //加密对象的偏移量,此两个值重要不能修改
     System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     cs.Write(inputByteArray, 0, inputByteArray.Length);
     cs.FlushFinalBlock();
     System.Text.StringBuilder sb = new System.Text.StringBuilder();
     foreach (Byte b in ms.ToArray())
     {
         sb.AppendFormat("{0:X2}", b);
     }
     return(sb.ToString());
 }
Пример #49
0
        private const string IV_64  = "5C83B05C"; //"#CoolSc#";
        public static string EncodeStatic(string data)
        {
            byte[] bytes  = System.Text.Encoding.ASCII.GetBytes(KEY_64);
            byte[] bytes2 = System.Text.Encoding.ASCII.GetBytes(IV_64);
            System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
            int keySize = dESCryptoServiceProvider.KeySize;

            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(bytes, bytes2), System.Security.Cryptography.CryptoStreamMode.Write);
            System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(cryptoStream);
            streamWriter.Write(data);
            streamWriter.Flush();
            cryptoStream.FlushFinalBlock();
            streamWriter.Flush();
            return(System.Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length));
        }
Пример #50
0
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="str">要解密字符串</param>
        /// <returns>返回解密后字符串</returns>
        public static String Decrypt_DES_Old(String str)
        {
            System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
            Int32 x;

            Byte[] inputByteArray = new Byte[str.Length / 2];
            for (x = 0; x < str.Length / 2; x++)
            {
                inputByteArray[x] = (Byte)(Convert.ToInt32(str.Substring(x * 2, 2), 16));
            }
            des.Key = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey);
            des.IV  = System.Text.ASCIIEncoding.ASCII.GetBytes(strDesKey);
            System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return(System.Text.Encoding.Default.GetString(ms.ToArray()));
        }
Пример #51
0
 public static string Encrypt(string Text, string sKey)
 {
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     byte[] bytes = System.Text.Encoding.Default.GetBytes(Text);
     dESCryptoServiceProvider.Key = System.Text.Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
     dESCryptoServiceProvider.IV  = System.Text.Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
     System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     cryptoStream.Write(bytes, 0, bytes.Length);
     cryptoStream.FlushFinalBlock();
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     byte[] array = memoryStream.ToArray();
     for (int i = 0; i < array.Length; i++)
     {
         byte b = array[i];
         stringBuilder.AppendFormat("{0:X2}", b);
     }
     return(stringBuilder.ToString());
 }
Пример #52
0
        public static string Decrypt(string Text, string sKey)
        {
            System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
            int num = Text.Length / 2;

            byte[] array = new byte[num];
            for (int i = 0; i < num; i++)
            {
                int num2 = System.Convert.ToInt32(Text.Substring(i * 2, 2), 16);
                array[i] = (byte)num2;
            }
            dESCryptoServiceProvider.Key = System.Text.Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            dESCryptoServiceProvider.IV  = System.Text.Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
            cryptoStream.Write(array, 0, array.Length);
            cryptoStream.FlushFinalBlock();
            return(System.Text.Encoding.Default.GetString(memoryStream.ToArray()));
        }
Пример #53
0
 public static string DecodeStatic(string pToDecrypt, string sKey = KEY_64)
 {
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     byte[] array = new byte[pToDecrypt.Length / 2];
     for (int i = 0; i < pToDecrypt.Length / 2; i++)
     {
         int num = System.Convert.ToInt32(pToDecrypt.Substring(i * 2, 2), 16);
         array[i] = (byte)num;
     }
     sKey = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8);
     dESCryptoServiceProvider.Key = System.Text.Encoding.ASCII.GetBytes(sKey);
     dESCryptoServiceProvider.IV  = System.Text.Encoding.ASCII.GetBytes(sKey);
     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
     System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     cryptoStream.Write(array, 0, array.Length);
     cryptoStream.FlushFinalBlock();
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     return(HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(memoryStream.ToArray())));
 }
Пример #54
0
 /// <summary>
 /// Encrypt(strText, "&%#@?,:*");
 /// </summary>
 /// <param name="str"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string Encryptnew(this string str, string key)
 {
     byte[] byKey;
     byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
     try
     {
         byKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
         System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
         byte[] inputByteArray     = System.Text.Encoding.UTF8.GetBytes(str);
         System.IO.MemoryStream ms = new System.IO.MemoryStream();
         System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(byKey, IV), System.Security.Cryptography.CryptoStreamMode.Write);
         cs.Write(inputByteArray, 0, inputByteArray.Length);
         cs.FlushFinalBlock();
         return(Convert.ToBase64String(ms.ToArray()));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Пример #55
0
        /// <summary>
        /// 暗号化された文字列を復号化する
        /// </summary>
        /// <param name="str">暗号化された文字列</param>
        /// <returns>復号化された文字列</returns>
        public static string DecryptString(string str)
        {
            //DESCryptoServiceProviderオブジェクトの作成
            System.Security.Cryptography.DESCryptoServiceProvider des =
                new System.Security.Cryptography.DESCryptoServiceProvider();

            //共有キーと初期化ベクタを決定
            //パスワードをバイト配列にする
            byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(_key);
            //共有キーと初期化ベクタを設定
            des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
            des.IV  = ResizeBytesArray(bytesKey, des.IV.Length);

            //Base64で文字列をバイト配列に戻す
            byte[] bytesIn = System.Convert.FromBase64String(str);
            //暗号化されたデータを読み込むためのMemoryStream
            System.IO.MemoryStream msIn =
                new System.IO.MemoryStream(bytesIn);
            //DES復号化オブジェクトの作成
            System.Security.Cryptography.ICryptoTransform desdecrypt =
                des.CreateDecryptor();
            //読み込むためのCryptoStreamの作成
            System.Security.Cryptography.CryptoStream cryptStreem =
                new System.Security.Cryptography.CryptoStream(msIn,
                                                              desdecrypt,
                                                              System.Security.Cryptography.CryptoStreamMode.Read);

            //復号化されたデータを取得するためのStreamReader
            System.IO.StreamReader srOut =
                new System.IO.StreamReader(cryptStreem,
                                           System.Text.Encoding.UTF8);
            //復号化されたデータを取得する
            string result = srOut.ReadToEnd();

            //閉じる
            srOut.Close();
            cryptStreem.Close();
            msIn.Close();

            return(result);
        }
Пример #56
0
    public static string DecryptFile(string FileName)
    {
        System.IO.FileStream fs = null;
        string s = string.Empty;

        try
        {
            string sKey = GenerateKey();
            System.Security.Cryptography.DESCryptoServiceProvider DES = new System.Security.Cryptography.DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV  = ASCIIEncoding.ASCII.GetBytes(sKey);

            System.IO.FileInfo dirFile = new System.IO.FileInfo(FileName);
            if (!dirFile.Exists)
            {
                return(string.Empty);
            }

            fs = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            System.Security.Cryptography.ICryptoTransform desdecrypt = DES.CreateDecryptor();
            System.Security.Cryptography.CryptoStream     cs         = new System.Security.Cryptography.CryptoStream(fs, desdecrypt, System.Security.Cryptography.CryptoStreamMode.Read);

            s = new System.IO.StreamReader(cs).ReadToEnd();
            //Return
            fs.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if ((fs != null))
            {
                fs.Close();
            }
        }
        return(s);
    }
Пример #57
0
 private const string IV_64  = "ksax123"; //"#CoolSc#";
 public static string EncodeStatic(string pToEncrypt, string sKey = KEY_64)
 {
     pToEncrypt = HttpContext.Current.Server.UrlEncode(pToEncrypt);
     System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
     byte[] bytes = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
     sKey = FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8);
     dESCryptoServiceProvider.Key = System.Text.Encoding.ASCII.GetBytes(sKey);
     dESCryptoServiceProvider.IV  = System.Text.Encoding.ASCII.GetBytes(sKey);
     System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
     System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, dESCryptoServiceProvider.CreateEncryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
     cryptoStream.Write(bytes, 0, bytes.Length);
     cryptoStream.FlushFinalBlock();
     System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
     byte[] array = memoryStream.ToArray();
     for (int i = 0; i < array.Length; i++)
     {
         byte b = array[i];
         stringBuilder.AppendFormat("{0:X2}", b);
     }
     stringBuilder.ToString();
     return(stringBuilder.ToString());
 }
Пример #58
0
        /// <summary>
        /// 文字列を暗号化する
        /// </summary>
        /// <param name="str">暗号化する文字列</param>
        /// <returns>暗号化された文字列</returns>
        public static string EncryptString(string str)
        {
            //文字列をバイト型配列にする
            byte[] bytesIn = System.Text.Encoding.UTF8.GetBytes(str);

            //DESCryptoServiceProviderオブジェクトの作成
            System.Security.Cryptography.DESCryptoServiceProvider des =
                new System.Security.Cryptography.DESCryptoServiceProvider();

            //共有キーと初期化ベクタを決定
            //パスワードをバイト配列にする
            byte[] bytesKey = System.Text.Encoding.UTF8.GetBytes(_key);
            //共有キーと初期化ベクタを設定
            des.Key = ResizeBytesArray(bytesKey, des.Key.Length);
            des.IV  = ResizeBytesArray(bytesKey, des.IV.Length);

            //暗号化されたデータを書き出すためのMemoryStream
            System.IO.MemoryStream msOut = new System.IO.MemoryStream();
            //DES暗号化オブジェクトの作成
            System.Security.Cryptography.ICryptoTransform desdecrypt =
                des.CreateEncryptor();
            //書き込むためのCryptoStreamの作成
            System.Security.Cryptography.CryptoStream cryptStreem =
                new System.Security.Cryptography.CryptoStream(msOut,
                                                              desdecrypt,
                                                              System.Security.Cryptography.CryptoStreamMode.Write);
            //書き込む
            cryptStreem.Write(bytesIn, 0, bytesIn.Length);
            cryptStreem.FlushFinalBlock();
            //暗号化されたデータを取得
            byte[] bytesOut = msOut.ToArray();

            //閉じる
            cryptStreem.Close();
            msOut.Close();

            //Base64で文字列に変更して結果を返す
            return(System.Convert.ToBase64String(bytesOut));
        }
Пример #59
0
        public static string DecodeStatic(string data)
        {
            byte[] bytes  = System.Text.Encoding.ASCII.GetBytes(KEY_64);
            byte[] bytes2 = System.Text.Encoding.ASCII.GetBytes(IV_64);
            byte[] buffer;
            string result;

            try
            {
                buffer = System.Convert.FromBase64String(data);
            }
            catch
            {
                result = null;
                return(result);
            }
            System.Security.Cryptography.DESCryptoServiceProvider dESCryptoServiceProvider = new System.Security.Cryptography.DESCryptoServiceProvider();
            System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer);
            System.Security.Cryptography.CryptoStream stream2 = new System.Security.Cryptography.CryptoStream(stream, dESCryptoServiceProvider.CreateDecryptor(bytes, bytes2), System.Security.Cryptography.CryptoStreamMode.Read);
            System.IO.StreamReader streamReader = new System.IO.StreamReader(stream2);
            result = streamReader.ReadToEnd();
            return(result);
        }
Пример #60
0
        private static string DESDecrypt(string MainKey, string strCipher)
        {
            string g_strDESKey  = ComputeMD5HashHEX(MainKey + g_strDESIV).Substring(0, 8);
            string strPlainText = string.Empty;

            try
            {
                string strDESKey   = g_strDESKey;
                string strDESIV    = g_strDESIV;
                byte[] bytesDESKey = System.Text.ASCIIEncoding.ASCII.GetBytes(strDESKey);
                byte[] bytesDESIV  = System.Text.ASCIIEncoding.ASCII.GetBytes(strDESIV);
                byte[] bytesCipher = ConvertHexToByteArray(strCipher);
                System.Security.Cryptography.DESCryptoServiceProvider desDecrypt = new System.Security.Cryptography.DESCryptoServiceProvider();
                System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(bytesCipher);
                System.Security.Cryptography.CryptoStream csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, desDecrypt.CreateDecryptor(bytesDESKey, bytesDESIV), System.Security.Cryptography.CryptoStreamMode.Read);
                System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt);
                strPlainText = srDecrypt.ReadLine();
                srDecrypt.Close();
                csDecrypt.Close();
                msDecrypt.Close();
            }
            catch (System.Exception) { }
            return(strPlainText);
        }