本文整理汇总了C#中System.Security.Cryptography.CryptoStream.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# CryptoStream.Dispose方法的具体用法?C# CryptoStream.Dispose怎么用?C# CryptoStream.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.CryptoStream
的用法示例。
在下文中一共展示了CryptoStream.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteCryptoServiceProvider
protected static byte[] ExecuteCryptoServiceProvider(byte[] data, ICryptoTransform cryptoTransform)
{
byte[] inArray;
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream,
cryptoTransform, CryptoStreamMode.Write))
{
try
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
}
catch (Exception ex)
{
throw new CustomException<CryptoServiceExceptionArgs>(
new CryptoServiceExceptionArgs("Error while writing encrypted data to the stream: \n" +
ex.Message));
}
finally
{
cryptoStream.Dispose();
}
}
inArray = memoryStream.ToArray();
}
return inArray;
}
示例2: Decrypt
public string Decrypt(string text)
{
byte[] textBytes = StringToBytes(text);
DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider();
MemoryStream writeCryp = new MemoryStream();
CryptoStream crypto = new CryptoStream(writeCryp, desProvider.CreateDecryptor(key, key2), CryptoStreamMode.Write);
crypto.Write(textBytes, 0, textBytes.Length);
crypto.FlushFinalBlock();
byte[] resultBytes = writeCryp.GetBuffer();
int len = Convert.ToInt32(writeCryp.Length);
writeCryp.Close();
crypto.Close();
try
{
crypto.Dispose();
}
catch { }
string result = "";
for (int i = 0; i < len; i++)
result = result.Insert(result.Length, Convert.ToChar(resultBytes[i]).ToString());
return result;
}
示例3: decrypt
public static string decrypt(string input)
{
try
{
string key = Settings.edkey;
RijndaelManaged rj = new RijndaelManaged();
rj.Padding = PaddingMode.Zeros;
rj.Mode = CipherMode.CBC;
rj.KeySize = 256;
rj.BlockSize = 256;
byte[] ky = Encoding.ASCII.GetBytes(key);
byte[] inp = Convert.FromBase64String(input);
byte[] res = new byte[inp.Length];
ICryptoTransform dec = rj.CreateDecryptor(ky, ky);
using (MemoryStream ms = new MemoryStream(inp))
{
using (CryptoStream cs = new CryptoStream(ms, dec, CryptoStreamMode.Read))
{
cs.Read(res, 0, res.Length);
cs.Close();
cs.Dispose();
}
ms.Close();
ms.Dispose();
}
return Encoding.UTF8.GetString(res).Trim().Replace("\0", "");
}
catch (Exception ex) { return ex.Message; }
}
示例4: Decryption
public string Decryption() {
try {
byte[] encrypted=Convert.FromBase64String(textInput.Text);
MemoryStream ms=null;
CryptoStream cs=null;
StreamReader sr=null;
Aes aes=new AesManaged();
aes.Key=key;
aes.IV=new byte[16];
ICryptoTransform decryptor=aes.CreateDecryptor(aes.Key,aes.IV);
ms=new MemoryStream(encrypted);
cs=new CryptoStream(ms,decryptor,CryptoStreamMode.Read);
sr=new StreamReader(cs);
string decrypted=sr.ReadToEnd();
ms.Dispose();
cs.Dispose();
sr.Dispose();
if(aes!=null) {
aes.Clear();
}
return decrypted;
}
catch {
MessageBox.Show("Text entered was not valid encrypted text.");
return"";
}
}
示例5: Decrypt
public static string Decrypt(string str,byte[] key)
{
//No need to check RemotingRole; no call to db.
if(str==""){
return "";
}
try {
byte[] encrypted=Convert.FromBase64String(str);
MemoryStream ms=null;
CryptoStream cs=null;
StreamReader sr=null;
Aes aes=new AesManaged();
aes.Key=key;
aes.IV=new byte[16];
ICryptoTransform decryptor=aes.CreateDecryptor(aes.Key,aes.IV);
ms=new MemoryStream(encrypted);
cs=new CryptoStream(ms,decryptor,CryptoStreamMode.Read);
sr=new StreamReader(cs);
string decrypted=sr.ReadToEnd();
ms.Dispose();
cs.Dispose();
sr.Dispose();
if(aes!=null) {
aes.Clear();
}
return decrypted;
}
catch {
//MessageBox.Show("Text entered was not valid encrypted text.");
return"";
}
}
示例6: encrypt
public static string encrypt(string input)
{
try
{
string key = Settings.edkey;
RijndaelManaged rj = new RijndaelManaged();
rj.Padding = PaddingMode.Zeros;
rj.Mode = CipherMode.CBC;
rj.KeySize = 256;
rj.BlockSize = 256;
byte[] ky = Encoding.ASCII.GetBytes(key);
byte[] inp = Encoding.ASCII.GetBytes(input);
byte[] res;
ICryptoTransform enc = rj.CreateEncryptor(ky, ky);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, enc, CryptoStreamMode.Write))
{
cs.Write(inp, 0, inp.Length);
cs.FlushFinalBlock();
cs.Close();
cs.Dispose();
}
res = ms.ToArray();
ms.Close();
ms.Dispose();
}
return Convert.ToBase64String(res).Replace("+", "%");
}
catch { return null; }
}
示例7: Decrypto
/// <summary>
/// 解密方法
/// </summary>
/// <param name="Source">待解密的串</param>
/// <returns>经过解密的串</returns>
public string Decrypto(string Source)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Source.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(Source.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
try
{
des.Key = ASCIIEncoding.ASCII.GetBytes(Key.Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(IV.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();
string estring = Encoding.Default.GetString(ms.ToArray());
ms.Dispose();
cs.Dispose();
return estring;
}
catch
{
return "";
}
}
示例8: AESDecrypt
/// <summary>
/// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
/// </summary>
/// <param name="decryptString">待解密密文</param>
/// <param name="decryptKey">解密密钥</param>
/// <returns></returns>
public static string AESDecrypt(string decryptString, string decryptKey)
{
if (string.IsNullOrEmpty(decryptString)) { throw (new Exception("密文不得为空")); }
if (string.IsNullOrEmpty(decryptKey)) { throw (new Exception("密钥不得为空")); }
string m_strDecrypt = "";
Rijndael m_AESProvider = Rijndael.Create();
try
{
byte[] m_btDecryptString = Convert.FromBase64String(decryptString);
MemoryStream m_stream = new MemoryStream();
CryptoStream m_csstream = new CryptoStream(m_stream, m_AESProvider.CreateDecryptor(Encoding.Default.GetBytes(decryptKey), m_btIV), CryptoStreamMode.Write);
m_csstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); m_csstream.FlushFinalBlock();
m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray());
m_stream.Close(); m_stream.Dispose();
m_csstream.Close(); m_csstream.Dispose();
}
catch (IOException ex) { throw ex; }
catch (CryptographicException ex) { throw ex; }
catch (ArgumentException ex) { throw ex; }
catch (Exception ex) { throw ex; }
finally { m_AESProvider.Clear(); }
return m_strDecrypt;
}
示例9: Encode
public Stream Encode(string key, Stream dataStream)
{
SymmetricAlgorithm provider = null;
ICryptoTransform encryptor = null;
Stream stream = null;
try
{
provider = GetCryptoProvider(null);
encryptor = provider.CreateEncryptor();
stream = new CryptoStream(dataStream, encryptor, CryptoStreamMode.Write);
return stream.WriteSalt(key).DisposeTogetherWith(provider, encryptor);
}
catch
{
try
{
if (provider != null)
provider.Dispose();
}
catch { }
try
{
if (encryptor != null)
encryptor.Dispose();
}
catch { }
try
{
if (stream != null)
stream.Dispose();
}
catch { }
throw;
}
}
示例10: Encrypt
///<summary>Encrypts signature text and returns a base 64 string so that it can go directly into the database.</summary>
public static string Encrypt(string str,byte[] key){
//No need to check RemotingRole; no call to db.
if(str==""){
return "";
}
byte[] ecryptBytes=Encoding.UTF8.GetBytes(str);
MemoryStream ms=new MemoryStream();
CryptoStream cs=null;
Aes aes=new AesManaged();
aes.Key=key;
aes.IV=new byte[16];
ICryptoTransform encryptor=aes.CreateEncryptor(aes.Key,aes.IV);
cs=new CryptoStream(ms,encryptor,CryptoStreamMode.Write);
cs.Write(ecryptBytes,0,ecryptBytes.Length);
cs.FlushFinalBlock();
byte[] encryptedBytes=new byte[ms.Length];
ms.Position=0;
ms.Read(encryptedBytes,0,(int)ms.Length);
cs.Dispose();
ms.Dispose();
if(aes!=null) {
aes.Clear();
}
return Convert.ToBase64String(encryptedBytes);
}
示例11: SyncCrypt
/// <summary>
/// Metoda za simetrično kriptiranje
/// </summary>
/// <param name="file"></param>
public void SyncCrypt(string file)
{
string zapis;
zapis = file + ".ecb";
FileStream fstreamU = File.OpenRead(file),
fstreamO = File.OpenWrite(zapis);
long lSize = fstreamU.Length;
byte[] bytes = new byte[BufferSize];
int read = -1;
Rijndael rijndaelAlg = Rijndael.Create();
rijndaelAlg.Mode = CipherMode.ECB;
TextReader streamreader = new StreamReader("tajni_kljuc.txt");
string secretKey = streamreader.ReadLine();
rijndaelAlg.Key = Convert.FromBase64String(secretKey);
streamreader.Close();
CryptoStream cout = new CryptoStream(fstreamO, rijndaelAlg.CreateEncryptor(), CryptoStreamMode.Write);
BinaryWriter bw = new BinaryWriter(cout);
bw.Write(lSize);
while ((read = fstreamU.Read(bytes, 0, bytes.Length)) != 0)
{
cout.Write(bytes, 0, read);
}
cout.Flush();
cout.Close();
cout.Dispose();
fstreamU.Flush();
fstreamU.Close();
fstreamU.Dispose();
}
示例12: DecryptFile
public PasswordSheet DecryptFile()
{
PasswordSheet decryptedData = null;
FileStream stream = null;
PasswordDeriveBytes secretKey = null;
RijndaelManaged rijndaelCipher = null;
ICryptoTransform decryptor = null;
CryptoStream cryptoStream = null;
try
{
rijndaelCipher = new RijndaelManaged();
// Making of the key for decryption
secretKey = GetPasswordBytes();
// Creates a symmetric Rijndael decryptor object.
decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read);
// Defines the cryptographics stream for decryption.THe stream contains decrpted data
cryptoStream = new CryptoStream(stream, decryptor, CryptoStreamMode.Read);
XmlSerializer serializer = new XmlSerializer(typeof(PasswordSheet));
decryptedData = (PasswordSheet)serializer.Deserialize(cryptoStream);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
finally
{
if (stream != null)
{
stream.Dispose();
}
if (secretKey != null)
{
secretKey.Dispose();
}
if (rijndaelCipher != null)
{
rijndaelCipher.Dispose();
}
if (cryptoStream != null)
{
cryptoStream.Dispose();
}
}
return decryptedData;
}
示例13: Decrypt
/// <summary>
/// Decrypts string data. Uses specific password as a key.
/// </summary>
/// <param name="dataToDecrypt">Encrypted data.</param>
/// <param name="password">Password used as a key to decryption.</param>
/// <param name="salt">String used to generate key based on password. Must be at least 8 bytes long.</param>
/// <returns>Decrypted string data.</returns>
public string Decrypt(string dataToDecrypt, string password, string salt)
{
AesManaged aes = null;
MemoryStream memoryStream = null;
try
{
// Generate a Key based on a Password and HMACSHA1 pseudo-random number generator
// Salt must be at least 8 bytes long
// Use an iteration count of at least 1000
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);
// Create AES algorithm
aes = new AesManaged();
// Key derived from byte array with 32 pseudo-random key bytes
aes.Key = rfc2898.GetBytes(32);
// IV derived from byte array with 16 pseudo-random key bytes
aes.IV = rfc2898.GetBytes(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();
}
}
}
示例14: EncryptFile
public void EncryptFile(PasswordSheet sheet)
{
FileStream stream = null;
PasswordDeriveBytes secretKey = null;
RijndaelManaged rijndaelCipher = null;
ICryptoTransform encryptor = null;
CryptoStream cryptoStream = null;
try
{
stream = new FileStream(_fileName, FileMode.OpenOrCreate, FileAccess.Write);
stream.SetLength(0);
secretKey = GetPasswordBytes();
rijndaelCipher = new RijndaelManaged();
encryptor = rijndaelCipher.CreateEncryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
// Defines a stream that links data streams to cryptographic transformations
cryptoStream = new CryptoStream(stream, encryptor, CryptoStreamMode.Write);
byte[] data = null;
using (var sheetStream = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(PasswordSheet));
serializer.Serialize(sheetStream, sheet);
data = sheetStream.GetBuffer();
}
cryptoStream.Write(data, 0, data.Length);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
finally
{
if (secretKey != null)
{
secretKey.Dispose();
}
if (rijndaelCipher != null)
{
rijndaelCipher.Dispose();
}
if (cryptoStream != null)
{
cryptoStream.Dispose();
}
}
}
示例15: Decrypt
/// <summary>
/// 解密字符串
/// </summary>
public static string Decrypt(string ciphertext)
{
string password2 = "Ahbool";
string cipher = string.Empty;
try
{
char[] key = new char[8];
if (password.Length > 8)
{
password = password.Remove(8);
}
password.CopyTo(0, key, 0, password.Length);
char[] iv = new char[8];
if (password2.Length > 8)
{
password2 = password2.Remove(8);
}
password2.CopyTo(0, iv, 0, password2.Length);
if (ciphertext == null)
{
return cipher;
}
SymmetricAlgorithm serviceProvider = new DESCryptoServiceProvider();
serviceProvider.Key = Encoding.ASCII.GetBytes(key);
serviceProvider.IV = Encoding.ASCII.GetBytes(iv);
byte[] contentArray = Convert.FromBase64String(ciphertext);
MemoryStream memoryStream = new MemoryStream(contentArray);
CryptoStream cryptoStream = new CryptoStream(memoryStream, serviceProvider.CreateDecryptor(), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cryptoStream);
cipher = streamReader.ReadToEnd();
streamReader.Dispose();
cryptoStream.Dispose();
memoryStream.Dispose();
serviceProvider.Clear();
}
catch
{
cipher = "123";
}
return cipher;
}