CreateDecryptor() private method

private CreateDecryptor ( ) : ICryptoTransform
return ICryptoTransform
Beispiel #1
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());
 }
        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.");
              }
        }
Beispiel #3
0
 /// <summary>
 /// DES解密 Encoding.Default
 /// </summary>
 /// <param name="source">密文</param>
 /// <param name="key">密钥</param>
 /// <returns>明文</returns>
 public static string DES_Decrypt(string source,string key)
 {
     if(string.IsNullOrEmpty(source)){
         return null;
     }
     DESCryptoServiceProvider des = new DESCryptoServiceProvider();
     byte[] inputByteArray = new byte[source.Length/2];
     for(int x = 0; x < source.Length/2; x++){
         int i = Convert.ToInt32(source.Substring(x*2,2),16);
         inputByteArray[x] = (byte) i;
     }
     if(key == null){
         key = Key;
     }
     else{
         int len = key.Length;
         if(len > 8){
             key = key.Substring(0,8);
         }
         else{
             if(len < 8){
                 key = key.PadRight(8,'.');
             }
         }
     }
     des.Key = Encoding.UTF8.GetBytes(key);
     des.IV = Encoding.UTF8.GetBytes(key);
     MemoryStream ms = new MemoryStream();
     CryptoStream cs = new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write);
     cs.Write(inputByteArray,0,inputByteArray.Length);
     cs.FlushFinalBlock();
     return Encoding.Default.GetString(ms.ToArray());
 }
Beispiel #4
0
 private static ICryptoTransform GetDesTransform(byte[] key, byte[] iv, bool decrypt)
 {
     using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
     {
         return (decrypt ? provider.CreateDecryptor(key, iv) : provider.CreateEncryptor(key, iv));
     }
 }
        /// <summary>
        /// Decrypt input string by DES algorithm.
        /// </summary>
        /// <param name="inputString">Input string is for decryption.</param>
        /// <returns>Decrypted string.</returns>
        public static string DESDecrypt(string inputString)
        {
            MemoryStream ms = null;
            CryptoStream cs = null;
            StreamReader sr = null;

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                ms = new MemoryStream(Convert.FromBase64String(inputString));
                cs = new CryptoStream(ms, des.CreateDecryptor(key, iv), CryptoStreamMode.Read);
                sr = new StreamReader(cs);
                return sr.ReadToEnd();
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }

                if (cs != null)
                {
                    cs.Close();
                }

                if (ms != null)
                {
                    ms.Close();
                }
            }
        }
Beispiel #6
0
        //Decrytping instead of encrypting
        public static void decrypt(FileStream inputStream, FileStream decryptedStream, byte[] key)
        {
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            ICryptoTransform decryptor;

            DES.Key = key;
            DES.IV = key;
            decryptor = DES.CreateDecryptor(DES.Key, DES.IV);

            try
            {
                CryptoStream cryptostream = new CryptoStream(decryptedStream, decryptor, CryptoStreamMode.Write);
                byte[] byteArrayInput = new byte[inputStream.Length];

                inputStream.Read(byteArrayInput, 0, byteArrayInput.Length);

                cryptostream.Write(byteArrayInput, 0, byteArrayInput.Length);

                cryptostream.Close();
                inputStream.Close();
                decryptedStream.Close();
            }
            catch (System.IO.IOException e)
            {
                MessageBox.Show("Could not open source or destination file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            catch (System.Security.Cryptography.CryptographicException e)
            {
                MessageBox.Show("Bad key or file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
        //Decrypt the content
        public static string DESDecrypt(string stringToDecrypt)
        {
            byte[] key;
            byte[] IV;

            byte[] inputByteArray;
            try
            {

                key = Convert2ByteArray(DESKey);

                IV = Convert2ByteArray(DESIV);

                int len = stringToDecrypt.Length; inputByteArray = Convert.FromBase64String(stringToDecrypt);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                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 (System.Exception ex)
            {

                throw ex;
            }
        }
Beispiel #8
0
        /// <summary>
        /// 使用DES解密(Added by niehl 2005-4-6)
        /// </summary>
        /// <param name="encryptedValue">待解密的字符串</param>
        /// <param name="key">密钥(最大长度8)</param>
        /// <param name="IV">m初始化向量(最大长度8)</param>
        /// <returns>解密后的字符串</returns>
        public string DESDecrypt(string encryptedValue, string key, string IV)
        {
            //将key和IV处理成8个字符
            key += "12345678";
            IV += "12345678";
            key = key.Substring(0, 8);
            IV = IV.Substring(0, 8);

            SymmetricAlgorithm sa;
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;

            sa = new DESCryptoServiceProvider();
            sa.Key = System.Text.Encoding.UTF8.GetBytes(key);
            sa.IV = System.Text.Encoding.UTF8.GetBytes(IV);
            ct = sa.CreateDecryptor();

            byt = Convert.FromBase64String(encryptedValue);

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();

            cs.Close();

            return System.Text.Encoding.UTF8.GetString(ms.ToArray());
        }
Beispiel #9
0
        public string Decode( string data ){
            if (String.IsNullOrEmpty(data)) return String.Empty;

            data = SelfDecode( data );

            var byKey = Encoding.ASCII.GetBytes( Key64 );
            var byIv = Encoding.ASCII.GetBytes( Iv64 );

            byte[] byEnc;
            try{
                byEnc = Convert.FromBase64String( data );
            }
            catch{
                return null;
            }

            var cryptoProvider = new DESCryptoServiceProvider();

            var ms = new MemoryStream( byEnc );
            var cst = new CryptoStream( ms, cryptoProvider.CreateDecryptor( byKey,
                                                                            byIv ), CryptoStreamMode.Read );
            var sr = new StreamReader( cst );

            return sr.ReadToEnd();
        }
        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();
        }
Beispiel #11
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;
 }
Beispiel #12
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;
            }
        }
Beispiel #13
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 "";
        }
Beispiel #14
0
        static void DecryptFile( string sInputFilename, 
                                 string sOutputFilename,
                                 string sKey )
        {
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            //A 64 bit key and IV is required for this provider.
            //Set secret key For DES algorithm.
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            //Set initialization vector.
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

            //Create a file stream to read the encrypted file back.
            FileStream fsread = new FileStream( sInputFilename,
                                                FileMode.Open,
                                                FileAccess.Read );
            //Create a DES decryptor from the DES instance.
            ICryptoTransform desdecrypt = DES.CreateDecryptor();
            //Create crypto stream set to read and do a
            //DES decryption transform on incoming bytes.
            CryptoStream cryptostreamDecr = new CryptoStream( fsread,
                                                              desdecrypt,
                                                              CryptoStreamMode.Read);
            //Print the contents of the decrypted file.
            StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
            fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
            fsDecrypted.Flush();
            fsDecrypted.Close();
        }
Beispiel #15
0
        public static string Decrypt(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 = Convert.FromBase64String(text);

            MemoryStream ms = new MemoryStream();

            try
            {
                CryptoStream cs = new CryptoStream(ms, csp.CreateDecryptor(encodedkey, iv), CryptoStreamMode.Write);
                cs.Write(bytes, 0, bytes.Length);
                cs.FlushFinalBlock();
            }
            catch (Exception ex)
            {
                
            }

            return Encoding.UTF8.GetString(ms.ToArray());

        }
Beispiel #16
0
        public static IEnumerable<byte> Decrypt(IEnumerable<byte> data, IEnumerable<byte> key, int repeatCount = 1)
        {
            var keyEnumerable = key as byte[] ?? key.ToArray();
            var dataEnumerable = data as byte[] ?? data.ToArray();

            if(keyEnumerable.IsEmpty() || dataEnumerable.IsEmpty())
            {
                return dataEnumerable;
            }

            DES desDecrypt = new DESCryptoServiceProvider();
            desDecrypt.Mode = CipherMode.ECB;

            desDecrypt.Key = keyEnumerable.ToArray();
            var decryptedStream = new MemoryStream();
            var cryptoStream = new CryptoStream(decryptedStream, desDecrypt.CreateDecryptor(), CryptoStreamMode.Write);

            cryptoStream.Write(dataEnumerable.ToArray(), 0, dataEnumerable.Length);
            cryptoStream.FlushFinalBlock();

            IEnumerable<byte> result = decryptedStream.ToArray();
            if(repeatCount > 1)
            {
                result = Decrypt(result, keyEnumerable, repeatCount - 1);
            }
            return result;
        }
Beispiel #17
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;
            }
        }
Beispiel #18
0
        /// <summary>
        /// 使用DES以及固定的密码解密字符串
        /// </summary>
        /// <param name="pToDecrypt">待解密字符串</param>
        /// <returns></returns>
        public static string DecryptString(string pToDecrypt)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            try
            {
                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.Default.GetBytes(DESKEY);
                des.IV = Encoding.Default.GetBytes(DESKEY);
                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 System.Text.Encoding.Default.GetString(ms.ToArray());
            }
            catch
            {
                return pToDecrypt;
            }
            finally
            {
                des = null;
            }
        }
        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;
        }
Beispiel #20
0
 public static ICryptoTransform CreateDecryptor(string key, CryptionType type)
 {
     ICryptoTransform transform;
     SHA512 sha512 = new SHA512CryptoServiceProvider();
     var bytes = sha512.ComputeHash(Sha1(key).ToAsciiBytes());
     switch (type)
     {
         case CryptionType.Aes:
             var aes = Rijndael.Create();
             aes.Mode = CipherMode.CBC;
             transform = aes.CreateDecryptor(bytes.Skip(17).Take(32).ToArray(), bytes.Skip(17).Take(16).ToArray());
             aes.Clear();
             break;
         case CryptionType.Des:
             var des = new DESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = des.CreateDecryptor(bytes.Skip(17).Take(8).ToArray(), bytes.Skip(17).Take(16).ToArray());
             des.Clear();
             break;
         default:
             var tripleDes = new TripleDESCryptoServiceProvider { Mode = CipherMode.CBC };
             transform = tripleDes.CreateDecryptor(bytes.Skip(17).Take(24).ToArray(), bytes.Skip(17).Take(16).ToArray());
             tripleDes.Clear();
             break;
     }
     return transform;
 }
 /// <summary>
 /// 解密
 /// </summary>
 /// <param name="decryptString"></param>
 /// <returns></returns>
 public static string Decrypt(string decryptString)
 {
     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 = Convert.FromBase64String(decryptString);
         System.Security.Cryptography.DESCryptoServiceProvider DCSP = new System.Security.Cryptography.DESCryptoServiceProvider();
         mStream = new System.IO.MemoryStream();
         cStream = new System.Security.Cryptography.CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), System.Security.Cryptography.CryptoStreamMode.Write);
         cStream.Write(inputByteArray, 0, inputByteArray.Length);
         cStream.FlushFinalBlock();
         return(Encoding.UTF8.GetString(mStream.ToArray()));
     }
     catch
     {
         return(decryptString);
     }
     finally
     {
         if (mStream != null)
         {
             mStream.Close(); mStream.Dispose();
         }
         if (cStream != null)
         {
             cStream.Close(); cStream.Dispose();
         }
     }
 }
Beispiel #22
0
        /// <summary>
        /// DES���� Encoding.Default
        /// </summary>
        /// <param name="source">����</param>
        /// <param name="key">��Կ</param>
        /// <returns>����</returns>
        public static string DES_Decrypt(string source, string key)
        {
            if (string.IsNullOrEmpty(source))
                return null;

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

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

            //�������ܶ������Կ��ƫ��������ֵ��Ҫ�������޸�
            des.Key = UTF8Encoding.UTF8.GetBytes(key);
            des.IV = UTF8Encoding.UTF8.GetBytes(key);

            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());
        }
Beispiel #23
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());
        }
Beispiel #24
0
 public string DecryptDES(string encrypt, string iv, string decryptKey)
 {
     string[] sInput = encrypt.Split("-".ToCharArray());
     byte[] data = new byte[sInput.Length];
     for (int i = 0; i < sInput.Length; i++)
     {
         data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
     }
     try
     {
         byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
         byte[] rgbIV = Encoding.UTF8.GetBytes(iv);
         byte[] inputByteArray = data;
         DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
         MemoryStream mStream = new MemoryStream();
         CryptoStream cStream = new CryptoStream(
             mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
         cStream.Write(inputByteArray, 0, inputByteArray.Length);
         cStream.FlushFinalBlock();
         return Encoding.UTF8.GetString(mStream.ToArray());
     }
     catch
     {
         return null;
     }
 }
Beispiel #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());
                    }
                }
            }
        }
        //Descriptografa o cookie
        public static string Descriptografar(string valor)
        {
            DESCryptoServiceProvider des;
            MemoryStream ms;
            CryptoStream cs; byte[] input;

            try
            {
                des = new DESCryptoServiceProvider();
                ms = new MemoryStream();

                input = new byte[valor.Length];
                input = Convert.FromBase64String(valor.Replace(" ", "+"));

                chave = Encoding.UTF8.GetBytes(chaveCriptografia.Substring(0, 8));

                cs = new CryptoStream(ms, des.CreateDecryptor(chave, iv), CryptoStreamMode.Write);
                cs.Write(input, 0, input.Length);
                cs.FlushFinalBlock();

                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #27
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 "";
        }
Beispiel #28
0
        public static string GetDecryptedText(string EncryptedString)
        {
            string sdec = "";
            try
            {
                string smsg = EncryptedString;

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                System.Text.Encoding utf = new System.Text.UTF8Encoding();

                byte[] key = utf.GetBytes("12348765");
                byte[] iv ={ 1, 2, 3, 4, 8, 7, 6, 5 };

                ICryptoTransform decryptor = des.CreateDecryptor(key, iv);

                byte[] bmsg = utf.GetBytes(smsg);

                byte[] benc1 = System.Convert.FromBase64String(EncryptedString);
                byte[] bdec = decryptor.TransformFinalBlock(benc1, 0, benc1.Length);
                sdec = utf.GetString(bdec);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
            return sdec;
        }
Beispiel #29
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)
				{
				}
			}
		}
Beispiel #30
0
        //解密方法
        public static string DesDecrypt(string pToDecrypt, string key, string iv)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            //Put  the  input  string  into  the  byte  array
            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.Default.GetBytes(key);
            des.IV = Encoding.Default.GetBytes(iv);
            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 Encoding.Default.GetString(ms.ToArray());
        }
Beispiel #31
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());
        }
Beispiel #32
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();
                }
            }
        }
Beispiel #33
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);
 }
Beispiel #34
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);
        }
Beispiel #35
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);
    }
Beispiel #36
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()));
        }
Beispiel #37
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);
        }
Beispiel #38
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);
            }
        }
Beispiel #39
0
            public static string Decrypt(string Text, string sKey)
            {
                string result;

                if (string.IsNullOrEmpty(Text))
                {
                    result = "";
                }
                else
                {
                    try
                    {
                        string text = SecretCommon.Md5Hex.Encrypt(sKey, false);
                        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 = Convert.ToInt32(Text.Substring(i * 2, 2), 16);
                            array[i] = (byte)num2;
                        }
                        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.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write);
                        cryptoStream.Write(array, 0, array.Length);
                        cryptoStream.FlushFinalBlock();
                        result = System.Text.Encoding.Default.GetString(memoryStream.ToArray());
                    }
                    catch (System.Exception ex)
                    {
                        result = ex.Message;
                    }
                }
                return(result);
            }
Beispiel #40
0
        public static string Decrypt(string decryptString, string decryptKey)
        {
            string result;

            try
            {
                decryptKey = TextUtility.CutLeft(decryptKey, 8);
                decryptKey = decryptKey.PadRight(8, ' ');
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(decryptKey);
                byte[] keys  = DES.Keys;
                byte[] array = System.Convert.FromBase64String(decryptString);
                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.CreateDecryptor(bytes, keys), System.Security.Cryptography.CryptoStreamMode.Write);
                cryptoStream.Write(array, 0, array.Length);
                cryptoStream.FlushFinalBlock();
                result = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            catch
            {
                result = "";
            }
            return(result);
        }
Beispiel #41
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 = 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));
                MemoryStream memoryStream = new 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()));
            }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="sIn"></param>
        /// <param name="sKey"></param>
        /// <returns></returns>
        public string UnEncrypt(string sIn, string sKey = "ColtSmart")
        {
            byte[] inputByteArray = Convert.FromBase64String(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.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                //string str = System.Text.Encoding.Default.GetString(ms.ToArray());
                string str = System.Text.Encoding.GetEncoding("GB2312").GetString(ms.ToArray());
                //string str = System.Text.Encoding.UTF32.GetString(ms.ToArray());
                str = str.TrimEnd('\0');
                ms.Close();
                return(str);
            }
        }
Beispiel #43
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);
        }
Beispiel #44
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())));
 }
Beispiel #45
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());
 }
Beispiel #46
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);
        }
Beispiel #47
0
Datei: Des.cs Projekt: 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);
        }
    }
 /// <summary>
 /// Decrypt(strText, "&%#@?,:*");
 /// </summary>
 /// <param name="str"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string Decryptnew(this string str, string key)
 {
     byte[] byKey;
     byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
     byte[] inputByteArray;
     // inputByteArray.Length = strText.Length;
     try
     {
         byKey = System.Text.Encoding.UTF8.GetBytes(key.Substring(0, 8));
         System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider();
         inputByteArray = Convert.FromBase64String(str);
         System.IO.MemoryStream ms = new System.IO.MemoryStream();
         System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(byKey, IV), System.Security.Cryptography.CryptoStreamMode.Write);
         cs.Write(inputByteArray, 0, inputByteArray.Length);
         cs.FlushFinalBlock();
         System.Text.Encoding encoding = System.Text.Encoding.UTF8;
         return(encoding.GetString(ms.ToArray()));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }