当前位置: 首页>>代码示例>>C#>>正文


C# CryptoStream.Dispose方法代码示例

本文整理汇总了C#中CryptoStream.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.Dispose方法的具体用法?C# CryptoStream.Dispose怎么用?C# CryptoStream.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CryptoStream的用法示例。


在下文中一共展示了CryptoStream.Dispose方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Decrypt

    /// <summary>
    /// ถอดรหัสข้อมูล
    /// </summary>
    /// <param name="Value">ข้อมูลที่ต้องการให้ถอดรหัส</param>
    /// <returns>ข้อมูลหลังจากถอดรหัส</returns>
    /// <example>
    /// clsSecurity.Decrypt("e0NDKIlUhHF3qcIdkmGpZw==");
    /// </example>
    public string Decrypt(string Value)
    {
        #region Variable
        SymmetricAlgorithm mCSP;
        ICryptoTransform ct = null;
        MemoryStream ms = null;
        CryptoStream cs = null;
        byte[] byt;
        byte[] result;
        #endregion
        #region Procedure
        mCSP = new RijndaelManaged();

        try
        {
            mCSP.Key = _key;
            mCSP.IV = _initVector;
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);

            byt = Convert.FromBase64String(Value);

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

            cs.Close();
            result = ms.ToArray();
        }
        catch
        {
            result = null;
        }
        finally
        {
            if (ct != null)
                ct.Dispose();
            if (ms != null)
                if (ms.CanRead)
                {
                    ms.Dispose();
                }
            if (cs != null)
                if (cs.CanRead)
                {
                    cs.Dispose();
                }
        }
        try
        {
            return ASCIIEncoding.UTF8.GetString(result);
        }
        catch (Exception)
        {
            return "";
        }
        #endregion
    }
开发者ID:oofdui,项目名称:RemoteDesktopCenter,代码行数:66,代码来源:clsSecurity.cs

示例2: MultipleDispose

        public static void MultipleDispose()
        {
            ICryptoTransform encryptor = new IdentityTransform(1, 1, true);

            using (MemoryStream output = new MemoryStream())
            {
                using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
                {
                    encryptStream.Dispose();
                }

                Assert.Equal(false, output.CanRead);
            }

            #if netcoreapp11
            using (MemoryStream output = new MemoryStream())
            {
                using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write, leaveOpen: false))
                {
                    encryptStream.Dispose();
                }

                Assert.Equal(false, output.CanRead);
            }

            using (MemoryStream output = new MemoryStream())
            {
                using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write, leaveOpen: true))
                {
                    encryptStream.Dispose();
                }

                Assert.Equal(true, output.CanRead);
            }
            #endif
        }
开发者ID:dotnet,项目名称:corefx,代码行数:36,代码来源:CryptoStream.cs

示例3: WriteMemoryStreamBytes

        private static byte[] WriteMemoryStreamBytes(string plainText, ref byte[] saltBytes, ICryptoTransform encryptor, ref MemoryStream memoryStream, ref CryptoStream cryptoStream)
        {
            try
            {
                memoryStream = new MemoryStream();

                try
                {
                    cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);

                    using (var streamWriter = new StreamWriter(cryptoStream))
                    {
                        streamWriter.Write(plainText);
                    }
                }
                finally
                {
                    if (cryptoStream != null)
                    {
                        cryptoStream.Dispose();
                    }
                }

                var cipherTextBytes = memoryStream.ToArray();
                Array.Resize(ref saltBytes, saltBytes.Length + cipherTextBytes.Length);
                Array.Copy(cipherTextBytes, 0, saltBytes, _saltSize, cipherTextBytes.Length);

                return saltBytes;
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Dispose();
                }
            }
        }
开发者ID:CatBayun,项目名称:WCF.First-Steps,代码行数:37,代码来源:SQLExtentions.cs

示例4: MultipleDispose

 public static void MultipleDispose()
 {
     ICryptoTransform encryptor = new IdentityTransform(1, 1, true);
     using (MemoryStream output = new MemoryStream())
     using (CryptoStream encryptStream = new CryptoStream(output, encryptor, CryptoStreamMode.Write))
     {
         encryptStream.Dispose();
     }
 }
开发者ID:noahfalk,项目名称:corefx,代码行数:9,代码来源:CryptoStream.cs

示例5: decryptContent

            private byte[] decryptContent(byte[] content, int contentIndex, Ticket tik, TMD tmd)
            {
                Array.Resize(ref content, Shared.AddPadding(content.Length, 16));
                byte[] titleKey = tik.TitleKey;
                byte[] iv = new byte[16];

                byte[] tmp = BitConverter.GetBytes(tmd.Contents[contentIndex].Index);
                iv[0] = tmp[1];
                iv[1] = tmp[0];

                RijndaelManaged rm = new RijndaelManaged();
                rm.Mode = CipherMode.CBC;
                rm.Padding = PaddingMode.None;
                rm.KeySize = 128;
                rm.BlockSize = 128;
                rm.Key = titleKey;
                rm.IV = iv;

                ICryptoTransform decryptor = rm.CreateDecryptor();

                MemoryStream ms = new MemoryStream(content);
                CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);

                byte[] decCont = new byte[content.Length];
                cs.Read(decCont, 0, decCont.Length);

                cs.Dispose();
                ms.Dispose();

                return decCont;
            }
开发者ID:nastys,项目名称:Uwizard,代码行数:31,代码来源:NUSD.cs

示例6: encryptTitleKey

            private void encryptTitleKey()
            {
                commonKeyIndex = newKeyIndex;
                byte[] ckey = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                byte[] iv = BitConverter.GetBytes(Shared.Swap(titleId));
                Array.Resize(ref iv, 16);

                RijndaelManaged rm = new RijndaelManaged();
                rm.Mode = CipherMode.CBC;
                rm.Padding = PaddingMode.None;
                rm.KeySize = 128;
                rm.BlockSize = 128;
                rm.Key = ckey;
                rm.IV = iv;

                ICryptoTransform encryptor = rm.CreateEncryptor();

                MemoryStream ms = new MemoryStream(decryptedTitleKey);
                CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Read);

                cs.Read(encryptedTitleKey, 0, encryptedTitleKey.Length);

                cs.Dispose();
                ms.Dispose();
                encryptor.Dispose();
                rm.Clear();
            }
开发者ID:nastys,项目名称:Uwizard,代码行数:27,代码来源:NUSD.cs

示例7: RC2_CBC_Symmetry_Decode_Byte

        static public bool RC2_CBC_Symmetry_Decode_Byte(byte[] decryptByte, uint startPos, uint inLen, ref byte[] outBytes, byte[] rgbKey)
        {
            try
            {
                RC2CryptoServiceProvider m_RC2Provider = new RC2CryptoServiceProvider();
                MemoryStream m_stream = new MemoryStream();
                CryptoStream m_cstream = new CryptoStream(m_stream, m_RC2Provider.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                m_cstream.Write(decryptByte, (int)startPos, (int)inLen);
                m_cstream.FlushFinalBlock();
                outBytes = m_stream.ToArray();

                m_stream.Close(); 
                m_stream.Dispose();

                m_cstream.Close(); 
                m_cstream.Dispose();

                return true;
            }
            catch
            {
                return false;
            }
        }
开发者ID:zhutaorun,项目名称:unitygame,代码行数:24,代码来源:Crypt.cs

示例8: Decrypt

    public static string Decrypt(string dataToDecrypt, string key)
    {
        AesManaged aes = null;
        MemoryStream memoryStream = null;

        try
        {
            //Create AES algorithm
            aes = new AesManaged();
            //Key derived from byte array with 32 pseudo-random key bytes
            aes.Key = Encoding.UTF8.GetBytes(key);
            //IV derived from byte array with 16 pseudo-random key bytes
            aes.IV = new byte[16];

            //Create Memory and Crypto Streams
            memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

            //Decrypt Data
            byte[] data = Convert.FromBase64String(dataToDecrypt);
            cryptoStream.Write(data, 0, data.Length);
            cryptoStream.FlushFinalBlock();

            //Return Decrypted String
            byte[] decryptBytes = memoryStream.ToArray();

            //Dispose
            if (cryptoStream != null)
                cryptoStream.Dispose();

            //Retval
            return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
        }
        finally
        {
            if (memoryStream != null)
                memoryStream.Dispose();

            if (aes != null)
                aes.Clear();
        }
    }
开发者ID:masterjay,项目名称:BankofTaiwanActive,代码行数:42,代码来源:EncryptHelper.cs


注:本文中的CryptoStream.Dispose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。