本文整理汇总了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
}
示例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
}
示例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();
}
}
}
示例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();
}
}
示例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;
}
示例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();
}
示例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;
}
}
示例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();
}
}